Chapter 8 · Simulation

Stack, CALL and RET

← Chapter 8
Source code · relative offset
    Stack (SS = 0700h)

    Stack, CALL and RET

    Goal: see, instruction by instruction, how the 8086 stack is managed with SS:SP. The stack grows downward: every PUSH decrements SP by 2 and writes the word at SS:SP; every POP reads the word at SS:SP and increments SP by 2. CALL pushes the return address onto the stack, RET takes it back into IP.

    How to use it

    1. Choose a program on the left; some programs let you change the start values.
    2. Step ▶ / ◀ Back (→ / ← on the keyboard), Play, Reset. With Micro steps on, PUSH, POP, CALL and RET are split into their inner steps (first SP decreases, then the word is written…).
    3. In the stack on the right, small addresses are at the top. SP→ marks the top of the stack, BP→ the frame pointer. Cells with a dashed outline have an address below SP: their values are not erased, but they no longer belong to the stack.
    4. Try it yourself: before a stack instruction, predict what SP, the value taken off, or the return address will be.

    Reminder

    InstructionSPWhat it does
    PUSH source−2SP ← SP − 2, [SS:SP] ← source
    POP destination+2destination ← [SS:SP], SP ← SP + 2
    PUSHF / POPF−2 / +2FLAGS to / from the stack
    CALL (near)−2return IP onto the stack, IP ← target
    RET / RET n+2 / +2+nIP ← [SS:SP]; RET n also discards n bytes of parameters
    • When parameters are passed on the stack, the procedure sets up a fixed frame with PUSH BP / MOV BP, SP: [BP+0] is the old BP, [BP+2] the return address, [BP+4] the last pushed parameter.
    • On the 8086 bits 12–15 of the FLAGS register always read as 1; that is why PUSHF writes a value of the form F2xxh onto the stack.
    • When a .COM program starts, DOS pushes 0000h onto the stack; the program's final RET takes this value into IP, and the INT 20h in the PSP ends the program.

    The programs are written in emu8086 syntax; the relative offsets, register, flag and stack values are computed with the 8086 rules and were checked against the shared 8086 interpreter.

    Related slides: Chapter 8.1 · The Stack, Chapter 8.2 · Procedures.