Chapter 8.1: The Stack
Introduction
Learning Outcomes
Explain how the 8086 stack is addressed through SS:SP and why it grows downward.
Know the valid operand types and trace how SP changes with each instruction.
Use the stack in the right order to save, restore and swap register values.
Save the FLAGS register on the stack and restore flags that an operation has changed.
Recognize and avoid unbalanced PUSH/POP, overflow and wrong-order bugs.
Read the register and stack windows while single-stepping a program.
The Stack Concept
The Stack
- The stack is a memory area set aside for temporary data.
CALLpushes the procedure's return address;RETpops it and jumps back to that address.INTdoes something similar when it calls an interrupt: the FLAGS register, the code segment (CS) and the offset (IP) are saved on the stack;IRETreturns from the interrupt.PUSH: stores a 16-bit value on the stack.POP: takes a 16-bit value off the stack.
PUSH AL is invalid.The Stack Concept
LIFO Order
- LIFO stands for Last In First Out.
- The last item pushed is the first item popped.
- If 1, 2, 3, 4, 5 are pushed in that order, the first value popped is 5, then 4, 3, 2 and finally 1.
The stack starts empty.
PUSH 1: 1 goes on top of the stack.
PUSH 2: 2 goes on top of the stack.
PUSH 3: 3 goes on top of the stack.
PUSH 4: 4 goes on top of the stack.
PUSH 5: 5 goes on top of the stack.
POP → 5 comes off. Output so far: 5
POP → 4 comes off. Output so far: 5, 4
POP → 3 comes off. Output so far: 5, 4, 3
POP → 2 comes off. Output so far: 5, 4, 3, 2
POP → 1 comes off. Output so far: 5, 4, 3, 2, 1
The Stack Concept
Stack Memory: SS and SP
- The stack uses the SS (Stack Segment) and SP (Stack Pointer) registers.
- SS:SP always points to the word on top of the stack (the one pushed last).
- The initial values are usually set by the operating system (loader): for .COM, SS = CS and SP = FFFEh; for .EXE, the values in the header.
- BP also uses SS by default, which makes it ideal for accessing data on the stack.
Stack segment SS = 0700h (in emu8086 all segment registers of a .COM program are 0700h).
The segment is multiplied by 16 (in hex, append a 0): 07000h.
The stack pointer SP = FFFEh is added as the offset.
The top of the stack is at physical address 16FFEh (20 bits).
The Stack Concept
The Stack Grows Downward
- In a .COM program, code, data and stack share the same 64 KB segment.
- The program is loaded upward from 0100h and the stack downward from FFFEh; the free space between serves both.
- Each PUSH decrements SP by 2, each POP increments it by 2.
- Too many PUSHes can make the stack overwrite code or data: a stack overflow.
PUSH and POP
The PUSH Instruction
| Form | Valid operands | Example |
|---|---|---|
PUSH REG | AX, BX, CX, DX, DI, SI, BP, SP | PUSH AX |
PUSH SREG | DS, ES, SS, CS | PUSH DS |
PUSH memory | [BX], [BX+SI+7], a 16-bit variable… | PUSH [BX], PUSH num |
PUSH immediate | 5, −24, 3Fh, 10001101b… | PUSH 3Fh |
PUSH immediate does not exist on the 8086/8088; it came with the 80186. emu8086 accepts it, but for a real 8086 write MOV AX, 5 + PUSH AX.PUSH AL and PUSH byte_var are errors. Flags have their own instruction: PUSHF.Detail: on the 8086, PUSH SP pushes the value of SP after the decrement; the 80286 and later push the value before it.
PUSH and POP
The POP Instruction
| Form | Valid operands | Example |
|---|---|---|
POP REG | AX, BX, CX, DX, DI, SI, BP, SP | POP BX |
POP SREG | DS, ES, SS (not CS) | POP DS |
POP memory | [BX], [BX+SI+7], a 16-bit variable… | POP result |
RETF, IRET, far JMP/CALL.POP 5 is meaningless: the popped value has to be stored somewhere.POPF loads the whole FLAGS register from the stack.Common idiom: PUSH CS + POP DS → copies CS into DS (there is no direct MOV between segment registers).
PUSH and POP
How Do PUSH and POP Work?
- SP is decremented by 2.
- The source value is written to SS:SP.
- The value at SS:SP is written to the destination.
- SP is incremented by 2.
Order matters: PUSH decrements, then writes; POP reads, then increments. So SP always points to the topmost occupied cell.
Start: SP = FFFEh, AX = 1234h. (In a .COM program, FFFEh holds the 0000h placed by DOS.)
PUSH AX ① SP ← SP − 2 = FFFCh. The new top cell is not written yet.
② The source value is written to SS:SP: [FFFCh] ← 1234h.
POP BX ① The value at SS:SP goes to the destination: BX ← 1234h.
② SP ← SP + 2 = FFFEh. The 1234h at FFFCh is not erased; it is just outside the stack now (faded).
PUSH and POP
Byte Order on the Stack (Little-Endian)
- The 8086 stores a word in memory low byte first: after
PUSH AX(AX = 1234h), [FFFCh] = 34h and [FFFDh] = 12h. - emu8086's stack window shows words; the memory window shows bytes. Both present the same data in different forms.
- Keep SP even at all times; on the 8086 a word access at an odd address costs an extra bus cycle.
PUSH and POP
What Changes, What Doesn't?
| Instruction | SP | Memory | Destination | Flags |
|---|---|---|---|---|
PUSH source | −2 | [SS:SP] ← source | — | unchanged |
POP dest | +2 | read, not erased | dest ← [SS:SP] | unchanged |
PUSHF | −2 | [SS:SP] ← FLAGS | — | unchanged |
POPF | +2 | read | FLAGS ← [SS:SP] | all loaded |
CALL (near) | −2 | [SS:SP] ← return IP | IP ← target | unchanged |
RET (near) | +2 | read | IP ← [SS:SP] | unchanged |
INT n | −6 | FLAGS, CS, IP | CS:IP ← vector | IF = TF = 0 |
IRET | +6 | read | IP, CS, FLAGS | all loaded |
So the result of a comparison (the flags) survives PUSH/POP safely; to save the flags themselves, use PUSHF/POPF.
Examples · Step through the code
First Example: PUSH AX / POP BX
emu8086 .COM start-up: CS = DS = SS = ES = 0700h, IP = 0100h, SP = FFFEh.
MOV AX, 42: 42 is decimal → AX = 002Ah (not 42h!).
PUSH AX: SP = FFFEh − 2 = FFFCh; [FFFCh] ← 002Ah.
POP BX: BX ← [FFFCh] = 002Ah; SP is back to FFFEh. The value was copied from AX to BX through the stack.
RET: pops the 0000h on the stack, IP = 0000h → the INT 20h in the PSP ends the program.
ORG 100h MOV AX, 42 ; AX = 42 PUSH AX ; push AX POP BX ; pop into BX RET ; return to the OS END
Examples · Step through the code
LIFO Order: 1, 2, 3 In → 3, 2, 1 Out
Start: SP = FFFEh.
AX = 0001h.
PUSH: SP = FFFCh, [FFFCh] = 0001h.
AX = 0002h (the old value of AX is safe on the stack).
PUSH: SP = FFFAh, [FFFAh] = 0002h.
AX = 0003h.
PUSH: SP = FFF8h; three values on the stack.
POP BX → 3 (last in, first out). SP = FFFAh.
POP BX → 2. SP = FFFCh.
POP BX → 1. SP = FFFEh; the stack is back to its initial state.
MOV AX, 1 ; AX = 1 PUSH AX ; push 1 MOV AX, 2 ; AX = 2 PUSH AX ; push 2 MOV AX, 3 ; AX = 3 PUSH AX ; push 3 POP BX ; BX ← 3 POP BX ; BX ← 2 POP BX ; BX ← 1
Examples · Step through the code
Saving a Register's Value
Goal: borrow AX for another job, then restore its old value.
AX = 1234h.
AX saved on the stack: [FFFCh] = 1234h.
AX used for another job: AX = 5678h.
POP AX: AX gets its old value back → 1234h.
RET: return to 0000h, the program ends.
ORG 100h MOV AX, 1234h PUSH AX ; save AX MOV AX, 5678h ; change AX POP AX ; restore old value RET END
Procedures follow exactly this pattern: PUSH the registers they use on entry, POP them on exit.
Examples · Step through the code
Swapping Two Registers with the Stack
In emu8086 after loading: CX = 000Bh (program size in bytes: 3+3+1+1+1+1+1 = 11), SP = FFFEh.
AX = 1212h.
BX = 3434h.
PUSH AX: [FFFCh] = 1212h, SP = FFFCh.
PUSH BX: [FFFAh] = 3434h, SP = FFFAh.
POP AX: the top value 3434h goes to AX.
POP BX: the next value 1212h goes to BX. Swapped!
RET: end of program.
ORG 100h MOV AX, 1212h ; AX ← 1212h MOV BX, 3434h ; BX ← 3434h PUSH AX ; push AX PUSH BX ; push BX POP AX ; AX ← old BX POP BX ; BX ← old AX RET END
Popping in mirror order (BX first) would leave the values where they were. LIFO is the whole trick.
Examples · Step through the code
Stack Usage: Three Registers
Loaded as .BIN: CS = SS = 0100h, IP = 0000h, SP = FFFEh. The stack starts empty.
AX = 0002h.
BX = 0004h.
CX = 0008h.
push ax: SP = FFFCh, [FFFCh] = 0002h.
push bx: SP = FFFAh, [FFFAh] = 0004h.
push cx: SP = FFF8h, [FFF8h] = 0008h.
pop ax: AX ← 0008h (the top one).
pop bx: BX ← 0004h (the middle one; unchanged).
pop cx: CX ← 0002h. Result: AX and CX have swapped values.
mov ax, 2 mov bx, 4 mov cx, 8 push ax push bx push cx pop ax pop bx pop cx
Source: single-stepping in emu8086. The stack window shows the area around SS:SP.
Examples · Step through the code
Reversing a String with the Stack
msg = 'ABC' is at address 0117h. Goal: print it in reverse order.
SI = 0117h (address of the msg string).
Loop counter CX = 3.
AL ← [SI] = 'A' (41h).
PUSH AX: 'A' goes on the stack as 0041h (AH = 00h).
SI = 0118h: next character.
LOOP: CX = 2 → jump back.
AL ← [SI] = 'B' (42h).
PUSH AX: 'B' goes on the stack as 0042h (AH = 00h).
SI = 0119h: next character.
LOOP: CX = 1 → jump back.
AL ← [SI] = 'C' (43h).
PUSH AX: 'C' goes on the stack as 0043h (AH = 00h).
SI = 011Ah: next character.
LOOP: CX = 0 → the loop ends.
CX = 3 for the second loop.
POP DX: DX = 0043h → DL = 'C' (last in, first out).
AH = 02h: DOS character output function.
INT 21h: prints C; AL = DL = 43h.
LOOP: CX = 2 → jump back.
POP DX: DX = 0042h → DL = 'B' (last in, first out).
AH = 02h: DOS character output function.
INT 21h: prints B; AL = DL = 42h.
LOOP: CX = 1 → jump back.
POP DX: DX = 0041h → DL = 'A' (last in, first out).
AH = 02h: DOS character output function.
INT 21h: prints A; AL = DL = 41h.
LOOP: CX = 0 → the loop ends.
The screen shows CBA: the stack reversed the string. RET ends the program.
ORG 100h MOV SI, OFFSET msg MOV CX, 3 put: MOV AL, [SI] PUSH AX ; save the char INC SI LOOP put MOV CX, 3 prt: POP DX ; DL = char MOV AH, 2 INT 21h ; print DL LOOP prt RET msg DB 'ABC' ; 0117h
Examples
What Happens to the Registers?
After the code runs, what are AX, BX, CX and SP? (Start: SP = FFFEh)
MOV AX, 1111h MOV BX, 2222h MOV CX, 3333h PUSH AX PUSH BX PUSH CX POP AX POP CX PUSH AX POP BX POP BX
After the PUSHes the stack holds (top to bottom): 3333h, 2222h, 1111h; SP = FFF8h.
POP AX → AX = 3333h; POP CX → CX = 2222h; SP = FFFCh.
PUSH AX (3333h), POP BX → BX = 3333h; then POP BX → BX = 1111h.
Result: AX = 3333h, BX = 1111h, CX = 2222h, SP = FFFEh (4 PUSHes, 4 POPs → balanced).
Examples
Comparing Ways to Swap
PUSH AX PUSH BX POP AX POP BX4 bytes · 4 memory accesses. Works for any register pair, segment registers included.
MOV CX, AX MOV AX, BX MOV BX, CX6 bytes · no memory access, but CX is clobbered.
XCHG AX, BX1 byte (93h) · the shortest and fastest way. Cannot be used with segment registers.
PUSH DS / POP ES).PUSH and POP · Step through the code
Saving the Flags: PUSHF / POPF
Start: flags clear (IF = 1).
AX = FFFFh.
FFFFh + 1 = 1 0000h → AX = 0000h, CF = 1, ZF = 1.
PUSHF: the FLAGS word (F257h: CF, PF, AF, ZF, IF and the 8086's always-1 bits) is pushed.
BX = 5.
5 + 3 = 8 → CF = 0, ZF = 0. The earlier result flags are lost…
…until POPF brings them back: CF = 1, ZF = 1.
The program ends.
ORG 100h MOV AX, 0FFFFh ADD AX, 1 ; CF=1, ZF=1 PUSHF ; save flags MOV BX, 5 ADD BX, 3 ; CF=0, ZF=0 POPF ; restore flags RET
Interrupt routines need not save the flags themselves; INT does it automatically. In ordinary code, PUSHF/POPF is needed to preserve a result.
Stack Safety
Stack Safety
- PUSH and POP hold temporary data while the program runs.
- Keep the stack intact with equal numbers of PUSHes and POPs (on every path, in every branch!).
- If PUSHes and POPs are unbalanced, the stack is corrupted and the program may not return to the operating system.
RETexpects a return address on top of the stack. In a .COM program DOS pushes 0000h at start-up: RET goes to CS:0000h (the PSP), where anINT 20hends the program.
Stack Safety · Step through the code
Bug Example: Unbalanced PUSH
The program is expected to return to DOS with RET at the end.
AX = 1234h.
AX is pushed but never popped.
RET takes the top word as the return address: IP = 1234h. The CPU starts executing random bytes at 0700:1234h → the program crashes or hangs.
ORG 100h MOV AX, 1234h PUSH AX ; no POP! RET ; IP ← ?
Fix: add POP AX before RET, or don't push at all. Count the PUSHes and POPs of every branch (JMP/Jcc) separately.
Stack Safety
Stack Overflow and Underflow
- Runaway recursion, or a PUSH without a POP in a loop.
- As SP falls, the stack moves down and overwrites code and data (same segment in .COM).
- The 8086 does not check for this; there is no error, only corruption.
- Popping a value that was never pushed.
- POP with SP = FFFEh → SP = 0000h (wraps around in 16 bits); the next PUSH writes to FFFEh.
- The value read is garbage (or DOS's 0000h).
.STACK 100h.Stack Safety
Good Practice
MOV BP, SP.Stack Safety
Tracing the Stack in emu8086
B8 12 12 = MOV AX, 1212h. Little-endian order is visible here.Lab · 8086 instructions step by step
Lab: The Stack
Summary
Summary
| Concept | Key point |
|---|---|
| Address | Top of stack = SS:SP; physical address SS × 16 + SP. |
| Growth | Downward: PUSH SP −= 2, POP SP += 2. The unit is always a word. |
| PUSH / POP | REG, SREG, memory (no POP CS). PUSH immediate only on 80186+ / emu8086. |
| LIFO | Values come back in reverse → mirror order to save, same order to swap. |
| PUSHF / POPF | Saves and restores the FLAGS register; PUSH and POP leave the flags alone. |
| Safety | Equal numbers of PUSH/POP; in .COM the final RET pops DOS's 0000h. |
Summary
Exercises
- With SS = 2000h and SP = 0100h, what are the physical addresses of the bytes written by
PUSH AX? - What are AX, BX, CX after
MOV AX,1 / MOV BX,2 / MOV CX,3 / PUSH AX / PUSH BX / PUSH CX / POP BX / POP CX / POP AX? - Copy DS into ES using only PUSH/POP.
- With SP = FFFEh,
PUSH AX / PUSH BX / PUSHF / POP CXruns. What is SP, and what ends up in CX? - Write a PUSH/POP sequence that preserves AX and the flags across an operation.
Answers
- SP = 00FEh → 20000h + 00FEh = 200FEh (AL) and 200FFh (AH).
- AX = 1, BX = 3, CX = 2: AX stays put, BX and CX are swapped.
PUSH DS+POP ES.- SP = FFFAh; CX = the FLAGS register at the time of PUSHF. Copies of AX and BX stay on the stack.
PUSH AX,PUSHF…POPF,POP AX(mirror order).
Summary
References
- Intel, The 8086 Family User's Manual, 1979 — definitions of PUSH, POP, CALL, RET, INT, IRET.
- B. B. Brey, The Intel Microprocessors, 8th ed., Pearson, 2009 — Chapters 4 and 6.
- K. R. Irvine, Assembly Language for x86 Processors, 7th ed., Pearson, 2014 — stack operations and procedures.
- emu8086 help: 8086 Assembler Tutorial, Part 9 (The Stack).