Can somebody solve this ques in assembly language?

Write a function switch_stack meant to change the current stack and

will be called as below. The function should destroy no registers.

push word [new_stack_segment]

push word [new_stack_offset]

call switch_stack

1 Answer

Relevance
  • 1 month ago
    Best Answer

    I'm assuming this is 16-bit x86 coding, since there is very little use of segment registers in 32-bit mode.  Most 32-bit systems use flat memory.  This is important because you'll need to manually push some information onto the new stack to hold register values you'll restore on return to the caller.

    You didn't say if this is a near or far call.  That's important, since you need to know how many return address bytes to copy to the new stack.

    Warning: Interrupts must be disabled while monkeying with the x86 stack!  It takes two instructions to modify both SS and SP, and an interrupt between those two operations is a disaster.  So:

    1. Use PUSHF to save flags, then push registers you will use; all onto the old stack.

    2. Disable interrupts (CLI). 

    3. Copy those values from the old stack to the new one, manually adjusting the new SP value downward so these can be popped off the stack on return.

    Notes: Set or clear the direction bit (CLD or STD) if you plan to use LODS, STOS, or MOVS instructions. Don't assume anything about the D flag the caller was using.

    Also , be sure to copy the return address.  I don't think you need to copy the arguments.  The caller can't be using a normal C calling convention, since there are no provisions for relocating the stack frame pointer in BP.

    4. Get new SS, SP values in general registers.

    Note: You can't safely push or pop the SS:SP values, so these don't go onto either stack for popping later.

    5. Copy those step 4 values to SS and SP

    6. Pop registers (reverse order!), POPF to restore flags and return.

Still have questions? Get your answers by asking now.