Chapter 8.1: The Stack

Microprocessors
Stack and LIFOSS:SPPUSH / POPByte orderSaving registersSwappingPUSHF / POPFStack safety
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Stack model

Explain how the 8086 stack is addressed through SS:SP and why it grows downward.

PUSH / POP

Know the valid operand types and trace how SP changes with each instruction.

Saving registers

Use the stack in the right order to save, restore and swap register values.

PUSHF / POPF

Save the FLAGS register on the stack and restore flags that an operation has changed.

Stack safety

Recognize and avoid unbalanced PUSH/POP, overflow and wrong-order bugs.

Tracing in emu8086

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.
  • CALL pushes the procedure's return address; RET pops it and jumps back to that address.
  • INT does something similar when it calls an interrupt: the FLAGS register, the code segment (CS) and the offset (IP) are saved on the stack; IRET returns from the interrupt.
  • PUSH: stores a 16-bit value on the stack.
  • POP: takes a 16-bit value off the stack.
Who uses it?The programmer (PUSH/POP), the CPU (CALL, INT), the operating system and BIOS interrupt routines.
UnitThe 8086 always writes and reads the stack one word (2 bytes) at a time; PUSH AL is invalid.
DirectionThe stack grows from high addresses toward low ones, i.e. downward.

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.
Result: the stack returns values in reverse order. To restore registers, POP them in the reverse of the PUSH order.
stackPUSHPOP

The stack starts empty.

1stackPUSHPOP

PUSH 1: 1 goes on top of the stack.

12stackPUSHPOP

PUSH 2: 2 goes on top of the stack.

123stackPUSHPOP

PUSH 3: 3 goes on top of the stack.

1234stackPUSHPOP

PUSH 4: 4 goes on top of the stack.

12345stackPUSHPOP

PUSH 5: 5 goes on top of the stack.

1234stackPUSHPOP5popped

POP → 5 comes off. Output so far: 5

123stackPUSHPOP4popped

POP → 4 comes off. Output so far: 5, 4

12stackPUSHPOP3popped

POP → 3 comes off. Output so far: 5, 4, 3

1stackPUSHPOP2popped

POP → 2 comes off. Output so far: 5, 4, 3, 2

stackPUSHPOP1popped

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.
SS0700h

Stack segment SS = 0700h (in emu8086 all segment registers of a .COM program are 0700h).

SS0700hSS × 1607000h

The segment is multiplied by 16 (in hex, append a 0): 07000h.

SS0700hSS × 1607000h+ SPFFFEh

The stack pointer SP = FFFEh is added as the offset.

SS0700hSS × 1607000h+ SPFFFEhPhysical address16FFEh

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.
PSP (0000–00FFh)Code + datastarts at ORG 100hprogram growsStackSP starts at FFFEhstack grows (SP falls)free spaceFFFFh0000h0100hCS = DS = SS = ES= 0700h(one 64 KB segment)

PUSH and POP

The PUSH Instruction

FormValid operandsExample
PUSH REGAX, BX, CX, DX, DI, SI, BP, SPPUSH AX
PUSH SREGDS, ES, SS, CSPUSH DS
PUSH memory[BX], [BX+SI+7], a 16-bit variable…PUSH [BX], PUSH num
PUSH immediate5, −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.
Only 16-bit operands are allowed: 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

FormValid operandsExample
POP REGAX, BX, CX, DX, DI, SI, BP, SPPOP BX
POP SREGDS, ES, SS (not CS)POP DS
POP memory[BX], [BX+SI+7], a 16-bit variable…POP result
Why not CS?Changing CS alone, while IP stays the same, sends execution to an arbitrary place. CS only changes together with IP: RETF, IRET, far JMP/CALL.
No immediatesPOP 5 is meaningless: the popped value has to be stored somewhere.
FlagsPUSH/POP do not change the flags; only 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?

PUSH source
  1. SP is decremented by 2.
  2. The source value is written to SS:SP.
POP destination
  1. The value at SS:SP is written to the destination.
  2. SP is incremented by 2.

Order matters: PUSH decrements, then writes; POP reads, then increments. So SP always points to the topmost occupied cell.

····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

Start: SP = FFFEh, AX = 1234h. (In a .COM program, FFFEh holds the 0000h placed by DOS.)

····FFF8····FFFA····FFFCSP0000hFFFESS:offset · low address at topPUSH AX · 1

PUSH AX ① SP ← SP − 2 = FFFCh. The new top cell is not written yet.

····FFF8····FFFA1234hFFFCSP0000hFFFESS:offset · low address at topPUSH AX · 2

② The source value is written to SS:SP: [FFFCh] ← 1234h.

····FFF8····FFFA1234hFFFCSP0000hFFFESS:offset · low address at topPOP BX · 1

POP BX ① The value at SS:SP goes to the destination: BX ← 1234h.

····FFF8····FFFA1234hFFFC0000hFFFESPSS:offset · low address at topPOP BX · 2

② 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)

Word view1234hSS:FFFCByte view (memory)34hFFFC12hFFFD00hFFFE00hFFFF← low byte (AL)← high byte (AH)DOS’s 0000h word
  • 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?

InstructionSPMemoryDestinationFlags
PUSH source−2[SS:SP] ← source—unchanged
POP dest+2read, not eraseddest ← [SS:SP]unchanged
PUSHF−2[SS:SP] ← FLAGS—unchanged
POPF+2readFLAGS ← [SS:SP]all loaded
CALL (near)−2[SS:SP] ← return IPIP ← targetunchanged
RET (near)+2readIP ← [SS:SP]unchanged
INT n−6FLAGS, CS, IPCS:IP ← vectorIF = TF = 0
IRET+6readIP, CS, FLAGSall 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

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

emu8086 .COM start-up: CS = DS = SS = ES = 0700h, IP = 0100h, SP = FFFEh.

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

MOV AX, 42: 42 is decimal → AX = 002Ah (not 42h!).

····FFF6····FFF8····FFFA002AhFFFCSP0000hFFFESS:offset · low address at top

PUSH AX: SP = FFFEh − 2 = FFFCh; [FFFCh] ← 002Ah.

····FFF6····FFF8····FFFA002AhFFFC0000hFFFESPSS:offset · low address at top

POP BX: BX ← [FFFCh] = 002Ah; SP is back to FFFEh. The value was copied from AX to BX through the stack.

····FFF6····FFF8····FFFA002AhFFFC0000hFFFESS:offset · low address at top

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

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

Start: SP = FFFEh.

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

AX = 0001h.

····FFF6····FFF8····FFFA0001hFFFCSP0000hFFFESS:offset · low address at top

PUSH: SP = FFFCh, [FFFCh] = 0001h.

····FFF6····FFF8····FFFA0001hFFFCSP0000hFFFESS:offset · low address at top

AX = 0002h (the old value of AX is safe on the stack).

····FFF6····FFF80002hFFFASP0001hFFFC0000hFFFESS:offset · low address at top

PUSH: SP = FFFAh, [FFFAh] = 0002h.

····FFF6····FFF80002hFFFASP0001hFFFC0000hFFFESS:offset · low address at top

AX = 0003h.

····FFF60003hFFF8SP0002hFFFA0001hFFFC0000hFFFESS:offset · low address at top

PUSH: SP = FFF8h; three values on the stack.

····FFF60003hFFF80002hFFFASP0001hFFFC0000hFFFESS:offset · low address at top

POP BX → 3 (last in, first out). SP = FFFAh.

····FFF60003hFFF80002hFFFA0001hFFFCSP0000hFFFESS:offset · low address at top

POP BX → 2. SP = FFFCh.

····FFF60003hFFF80002hFFFA0001hFFFC0000hFFFESPSS:offset · low address at top

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

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

Goal: borrow AX for another job, then restore its old value.

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

AX = 1234h.

····FFF6····FFF8····FFFA1234hFFFCSP0000hFFFESS:offset · low address at top

AX saved on the stack: [FFFCh] = 1234h.

····FFF6····FFF8····FFFA1234hFFFCSP0000hFFFESS:offset · low address at top

AX used for another job: AX = 5678h.

····FFF6····FFF8····FFFA1234hFFFC0000hFFFESPSS:offset · low address at top

POP AX: AX gets its old value back → 1234h.

····FFF6····FFF8····FFFA1234hFFFC0000hFFFESS:offset · low address at top

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

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

In emu8086 after loading: CX = 000Bh (program size in bytes: 3+3+1+1+1+1+1 = 11), SP = FFFEh.

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

AX = 1212h.

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

BX = 3434h.

····FFF6····FFF8····FFFA1212hFFFCSP0000hFFFESS:offset · low address at top

PUSH AX: [FFFCh] = 1212h, SP = FFFCh.

····FFF6····FFF83434hFFFASP1212hFFFC0000hFFFESS:offset · low address at top

PUSH BX: [FFFAh] = 3434h, SP = FFFAh.

····FFF6····FFF83434hFFFA1212hFFFCSP0000hFFFESS:offset · low address at top

POP AX: the top value 3434h goes to AX.

····FFF6····FFF83434hFFFA1212hFFFC0000hFFFESPSS:offset · low address at top

POP BX: the next value 1212h goes to BX. Swapped!

····FFF6····FFF83434hFFFA1212hFFFC0000hFFFESS:offset · low address at top

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

····FFF6····FFF8····FFFA····FFFC····FFFESPSS:offset · low address at top

Loaded as .BIN: CS = SS = 0100h, IP = 0000h, SP = FFFEh. The stack starts empty.

····FFF6····FFF8····FFFA····FFFC····FFFESPSS:offset · low address at top

AX = 0002h.

····FFF6····FFF8····FFFA····FFFC····FFFESPSS:offset · low address at top

BX = 0004h.

····FFF6····FFF8····FFFA····FFFC····FFFESPSS:offset · low address at top

CX = 0008h.

····FFF6····FFF8····FFFA0002hFFFCSP····FFFESS:offset · low address at top

push ax: SP = FFFCh, [FFFCh] = 0002h.

····FFF6····FFF80004hFFFASP0002hFFFC····FFFESS:offset · low address at top

push bx: SP = FFFAh, [FFFAh] = 0004h.

····FFF60008hFFF8SP0004hFFFA0002hFFFC····FFFESS:offset · low address at top

push cx: SP = FFF8h, [FFF8h] = 0008h.

····FFF60008hFFF80004hFFFASP0002hFFFC····FFFESS:offset · low address at top

pop ax: AX ← 0008h (the top one).

····FFF60008hFFF80004hFFFA0002hFFFCSP····FFFESS:offset · low address at top

pop bx: BX ← 0004h (the middle one; unchanged).

····FFF60008hFFF80004hFFFA0002hFFFC····FFFESPSS:offset · low address at top

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

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

msg = 'ABC' is at address 0117h. Goal: print it in reverse order.

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

SI = 0117h (address of the msg string).

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

Loop counter CX = 3.

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

AL ← [SI] = 'A' (41h).

····FFF6····FFF8····FFFA0041hFFFCSP0000hFFFESS:offset · low address at top

PUSH AX: 'A' goes on the stack as 0041h (AH = 00h).

····FFF6····FFF8····FFFA0041hFFFCSP0000hFFFESS:offset · low address at top

SI = 0118h: next character.

····FFF6····FFF8····FFFA0041hFFFCSP0000hFFFESS:offset · low address at top

LOOP: CX = 2 → jump back.

····FFF6····FFF8····FFFA0041hFFFCSP0000hFFFESS:offset · low address at top

AL ← [SI] = 'B' (42h).

····FFF6····FFF80042hFFFASP0041hFFFC0000hFFFESS:offset · low address at top

PUSH AX: 'B' goes on the stack as 0042h (AH = 00h).

····FFF6····FFF80042hFFFASP0041hFFFC0000hFFFESS:offset · low address at top

SI = 0119h: next character.

····FFF6····FFF80042hFFFASP0041hFFFC0000hFFFESS:offset · low address at top

LOOP: CX = 1 → jump back.

····FFF6····FFF80042hFFFASP0041hFFFC0000hFFFESS:offset · low address at top

AL ← [SI] = 'C' (43h).

····FFF60043hFFF8SP0042hFFFA0041hFFFC0000hFFFESS:offset · low address at top

PUSH AX: 'C' goes on the stack as 0043h (AH = 00h).

····FFF60043hFFF8SP0042hFFFA0041hFFFC0000hFFFESS:offset · low address at top

SI = 011Ah: next character.

····FFF60043hFFF8SP0042hFFFA0041hFFFC0000hFFFESS:offset · low address at top

LOOP: CX = 0 → the loop ends.

····FFF60043hFFF8SP0042hFFFA0041hFFFC0000hFFFESS:offset · low address at top

CX = 3 for the second loop.

····FFF60043hFFF80042hFFFASP0041hFFFC0000hFFFESS:offset · low address at top

POP DX: DX = 0043h → DL = 'C' (last in, first out).

····FFF60043hFFF80042hFFFASP0041hFFFC0000hFFFESS:offset · low address at top

AH = 02h: DOS character output function.

····FFF60043hFFF80042hFFFASP0041hFFFC0000hFFFESS:offset · low address at top

INT 21h: prints C; AL = DL = 43h.

····FFF60043hFFF80042hFFFASP0041hFFFC0000hFFFESS:offset · low address at top

LOOP: CX = 2 → jump back.

····FFF60043hFFF80042hFFFA0041hFFFCSP0000hFFFESS:offset · low address at top

POP DX: DX = 0042h → DL = 'B' (last in, first out).

····FFF60043hFFF80042hFFFA0041hFFFCSP0000hFFFESS:offset · low address at top

AH = 02h: DOS character output function.

····FFF60043hFFF80042hFFFA0041hFFFCSP0000hFFFESS:offset · low address at top

INT 21h: prints B; AL = DL = 42h.

····FFF60043hFFF80042hFFFA0041hFFFCSP0000hFFFESS:offset · low address at top

LOOP: CX = 1 → jump back.

····FFF60043hFFF80042hFFFA0041hFFFC0000hFFFESPSS:offset · low address at top

POP DX: DX = 0041h → DL = 'A' (last in, first out).

····FFF60043hFFF80042hFFFA0041hFFFC0000hFFFESPSS:offset · low address at top

AH = 02h: DOS character output function.

····FFF60043hFFF80042hFFFA0041hFFFC0000hFFFESPSS:offset · low address at top

INT 21h: prints A; AL = DL = 41h.

····FFF60043hFFF80042hFFFA0041hFFFC0000hFFFESPSS:offset · low address at top

LOOP: CX = 0 → the loop ends.

····FFF60043hFFF80042hFFFA0041hFFFC0000hFFFESS:offset · low address at top

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

With the stack
PUSH AX
PUSH BX
POP  AX
POP  BX
4 bytes · 4 memory accesses. Works for any register pair, segment registers included.
With a temp register
MOV CX, AX
MOV AX, BX
MOV BX, CX
6 bytes · no memory access, but CX is clobbered.
With XCHG
XCHG AX, BX
1 byte (93h) · the shortest and fastest way. Cannot be used with segment registers.
stack4 bytestemp register6 bytesXCHG1 byte
The real value of the stack is not swapping but saving: preserving registers during a procedure or interrupt, holding intermediate results, copying segment registers (PUSH DS / POP ES).

PUSH and POP · Step through the code

Saving the Flags: PUSHF / POPF

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

Start: flags clear (IF = 1).

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

AX = FFFFh.

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

FFFFh + 1 = 1 0000h → AX = 0000h, CF = 1, ZF = 1.

····FFF6····FFF8····FFFAF257hFFFCSP0000hFFFESS:offset · low address at top

PUSHF: the FLAGS word (F257h: CF, PF, AF, ZF, IF and the 8086's always-1 bits) is pushed.

····FFF6····FFF8····FFFAF257hFFFCSP0000hFFFESS:offset · low address at top

BX = 5.

····FFF6····FFF8····FFFAF257hFFFCSP0000hFFFESS:offset · low address at top

5 + 3 = 8 → CF = 0, ZF = 0. The earlier result flags are lost…

····FFF6····FFF8····FFFAF257hFFFC0000hFFFESPSS:offset · low address at top

…until POPF brings them back: CF = 1, ZF = 1.

····FFF6····FFF8····FFFAF257hFFFC0000hFFFESS:offset · low address at top

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.
  • RET expects 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 an INT 20h ends the program.
Extra PUSHRET takes the last pushed data instead of the return address → a jump to a random address.
Extra POPThe return address is “eaten” by a register; RET returns to the wrong place.
Wrong orderValues get mixed up (as in a swap) and the program silently misbehaves.
FFFAAX valueFFFCreturn IPFFFE0000hRET loads this into IPPUSH AX never popped

Stack Safety · Step through the code

Bug Example: Unbalanced PUSH

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

The program is expected to return to DOS with RET at the end.

····FFF6····FFF8····FFFA····FFFC0000hFFFESPSS:offset · low address at top

AX = 1234h.

····FFF6····FFF8····FFFA1234hFFFCSP0000hFFFESS:offset · low address at top

AX is pushed but never popped.

····FFF6····FFF8····FFFA1234hFFFC0000hFFFESPSS:offset · low address at top

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

Overflow
  • 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.
Underflow
  • 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).
PSP0000hcode + data0100hfree spacestack ↓FFFEhoverflow: SP falls into codeunderflow: SP = FFFEh + 2 → 0000h (wraps)
Rule: size the stack for the deepest call chain plus the most PUSHes. .EXE programs reserve a separate, large enough stack segment with a directive such as .STACK 100h.

Stack Safety

Good Practice

1. Mirror orderPUSH AX, BX, CX → POP CX, BX, AX. The code reads like a mirror image.
2. Single exitOne exit label instead of several RETs: the POPs are written once.
3. Leave SP aloneChange SP directly only to drop parameters (ADD SP, n) or reserve locals (SUB SP, n).
4. BP frameNo [SP+n] addressing (SP is not a base register on the 8086); use MOV BP, SP.
5. Preserve segmentsRoutines that change DS/ES should PUSH/POP them too.
6. Check in emu8086The stack button opens the stack window; before RET, check SP is back to its entry value.
PUSH AXPOP CXPUSH BXPOP BXPUSH CXPOP AXmirror image

Stack Safety

Tracing the Stack in emu8086

1. Compile and loadcompile → emulate. The register panel shows the initial CS, IP, SS, SP (.COM: 0700h, 0100h, 0700h, FFFEh).
2. Single stepEach press of single step runs one instruction; changed registers are highlighted. step back undoes it.
3. Stack windowThe stack button at the bottom shows the words around SS:SP; the line SP points to is marked.
4. Memory viewThe middle list shows bytes (hex, decimal, ASCII): B8 12 12 = MOV AX, 1212h. Little-endian order is visible here.
After loading, CX holds the program size in bytes (e.g. 000Bh = 11 bytes in the swap example). This is a convention inherited from DOS DEBUG; programs should not rely on CX.

Lab · 8086 instructions step by step

Lab: The Stack

Summary

Summary

ConceptKey point
AddressTop of stack = SS:SP; physical address SS × 16 + SP.
GrowthDownward: PUSH SP −= 2, POP SP += 2. The unit is always a word.
PUSH / POPREG, SREG, memory (no POP CS). PUSH immediate only on 80186+ / emu8086.
LIFOValues come back in reverse → mirror order to save, same order to swap.
PUSHF / POPFSaves and restores the FLAGS register; PUSH and POP leave the flags alone.
SafetyEqual numbers of PUSH/POP; in .COM the final RET pops DOS's 0000h.

Summary

Exercises

  1. With SS = 2000h and SP = 0100h, what are the physical addresses of the bytes written by PUSH AX?
  2. 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?
  3. Copy DS into ES using only PUSH/POP.
  4. With SP = FFFEh, PUSH AX / PUSH BX / PUSHF / POP CX runs. What is SP, and what ends up in CX?
  5. Write a PUSH/POP sequence that preserves AX and the flags across an operation.
Answers
  1. SP = 00FEh → 20000h + 00FEh = 200FEh (AL) and 200FFh (AH).
  2. AX = 1, BX = 3, CX = 2: AX stays put, BX and CX are swapped.
  3. PUSH DS + POP ES.
  4. SP = FFFAh; CX = the FLAGS register at the time of PUSHF. Copies of AX and BX stay on the stack.
  5. 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).

The End

Chapter 8.1: The Stack
Previous: Chapter 7: Flow Control · Next: Chapter 8.2: Procedures
1 / 1 Sercan KÜLCÜ. All rights reserved.