The Stack
The Stack
SS
Stack is used by CALL instruction to keep return address for procedure, RET instruction gets this value from
the stack and returns to that offset.
Quite the same thing happens when INT instruction calls an interrupt, it stores in stack flag register, code
segment and offset. IRET instruction is used to return from interrupt call.
We can also use the stack to keep any other data, there are two instructions that work with the stack:
PUSH REG
PUSH SREG
PUSH memory
PUSH immediate
REG: AX, BX, CX, DX, DI, SI, BP, SP.
POP REG
POP SREG
POP memory
REG: AX, BX, CX, DX, DI, SI, BP, SP.
It is very important to do equal number of PUSHs and POPs, otherwise the stack maybe corrupted and it
will be impossible to return to operating system.
As you already know we use RET instruction to return to operating system, so when program starts there is
a return address in stack (generally it's 0000h).
PUSH and POP instruction are especially useful because we don't have too much registers to operate with,
so here is a trick:
Restore the original value of the register from stack (using POP).
Here is an example:
ORG 100h
RET
END
Another use of the stack is for exchanging the values,
here is an example:
ORG 100h
RET
END
The exchange happens because stack uses LIFO (Last In First Out) algorithm, so when we push 1212h and
then 3434h, on pop we will first get 3434h and only after it 1212h.
The stack memory area is set by SS (Stack Segment) register, and SP (Stack Pointer) register.
Add 2 to SP register.
The current address pointed by SS:SP is called the top of the stack.
For COM files stack segment is generally the code segment, and stack pointer is set to value of 0FFFEh. At the
address SS:0FFFEh stored a return address for RETinstruction that is executed in the end of the program.
You can visually see the stack operation by clicking on [Stack] button on emulator window. The top of the
stack is marked with "<" sign.
8086 - Stack Instructions
Algorithm:
REG
POP SREG Example:
memory MOV AX, 1234h
PUSH AX
POP DX ; DX = 1234h
RET
C Z S O P A
unchanged
Pop all general purpose registers DI, SI, BP, SP, BX, DX, CX, AX from the stack.
SP value is ignored, it is Popped but not set to SP register).
Algorithm:
POP DI
POP SI
POP BP
POP xx (SP value ignored)
POP BX
POP DX
POP CX
POP AX
POPA No operands C Z S O P A
unchanged
Get flags register from the stack.
Algorithm:
C Z S O P A
popped
Algorithm:
SP = SP - 2
SS:[SP] (top of the stack) = operand
REG
SREG
PUSH
memory
immediate Example:
MOV AX, 1234h
PUSH AX
POP DX ; DX = 1234h
RET
C Z S O P A
unchanged
Push all general purpose registers AX, CX, DX, BX, SP, BP, SI, DI in the stack.
Original value of SP register (before PUSHA) is used.
Algorithm:
PUSH AX
PUSH CX
PUSH DX
PUSH BX
PUSHA No operands
PUSH SP
PUSH BP
PUSH SI
PUSH DI
C Z S O P A
unchanged
Store flags register in the stack.
Algorithm:
SP = SP - 2
PUSHF No operands SS:[SP] (top of the stack) = flags
C Z S O P A
unchanged
#make_exe# -
more advanced format of an executable file.
you may select exe template from the new menu in to create a simple exe program with pre-defined data, stack,
and code segments.
Example:
ADD AX, 1
p1 ENDP
C Z S O P A
unchanged
Algorithm:
Example:
CALL p1
No
operands
RET ADD AX, 1
or even
immediate
p1 ENDP
C Z S O P A
unchanged
Interrupt numbered by immediate byte (0..255).
Algorithm:
Push to stack:
o flags register
o CS
o IP
IF = 0
Transfer control to interrupt procedure
immediate Example:
INT
byte
MOV AH, 0Eh ; teletype.
RET
C Z S O P A I
unchanged 0
Interrupt Return.
Algorithm:
o IP
No
IRET o CS
operands
o flags register
C Z S O P A
popped
Algorithm:
No
operands Pop from stack:
RETF
or even o IP and CS
immediate if immediate operand is present: SP = SP + operand
C Z S O P A
unchanged
1. Swap_Using_Stack.asm
ORG 100h
RET
END