Chapter 8.1: The Stack

Chapter 8.1: The Stack · Study Questions
SS:SPLIFOPUSH / POPByte orderSaving and swappingPUSHF / POPFStack safety
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · The Stack Concept

Question 1: Where Does PUSH Write?

Where does PUSH AX copy the word in AX, and in what order?

A: PUSH src: SP ← SP − 2, then [SS:SP] ← src; the source (AX) is unchanged. The stack grows downward (B has the wrong direction). The stack lives in the SS segment (C); BX has nothing to do with it (D). POP, by contrast, reads first and then increases SP by 2.

Multiple Choice · The Stack Concept

Question 2: The Stack Pointer

Which statement about the stack pointer (SP) is correct?

D: SS:SP always points to the word on top of the stack (the most recently pushed); the physical address is SS × 10h + SP. SP is 16 bits; it decreases by 2 on PUSH and increases by 2 on POP. The stack is set up before use (in a .COM program the loader makes SP = FFFEh). The stack is temporary storage.

Multiple Choice · The Stack Concept

Question 3: Last In, First Out

1, 2, 3, 4, 5 are pushed onto the stack in that order. Then POP is executed three times. What value does the third POP get?

C: LIFO: the pop order is 5, 4, 3, 2, 1. The third POP gets 3; 2 and 1 remain on the stack. The stack returns values in reverse order; that is why, when restoring registers, the POP order must be the reverse of the PUSH order.

Classic Question · The Stack Concept

Question 4: Physical Address of the Stack

(a) In a .COM program SS = 0700h, SP = FFFEh. What is the physical address of the top of the stack? (b) With SS = 2000h, SP = 0100h, AX = 1234h, PUSH AX runs. What does SP become, and which bytes are written to which physical addresses?

Show answer

Physical address = SS × 10h + SP.

(a) 07000h + FFFEh = 16FFEh.

(b) SP = 0100h − 2 = 00FEh. 20000h + 00FEh = 200FEh: [200FEh] = 34h (low byte), [200FFh] = 12h (high byte). The 8086 stores words in little-endian order.

Multiple Choice · PUSH and POP

Question 5: A Valid Operand

Which one is a valid instruction on the 8086?

D: Segment registers can be pushed; PUSH DS + POP ES is one way to copy DS into ES. Only words are written to the stack, so PUSH AL is invalid. POP CS would throw execution to an arbitrary place while IP stays the same; it does not exist. PUSH with an immediate value arrived with the 80186 (even though emu8086 accepts it).

Output Question · PUSH and POP

Question 6: Saving a Value with PUSH

What are AX and SP after each instruction? (.COM program: SP starts at FFFEh.)

ORG 100h
MOV AX, 1234h
PUSH AX
MOV AX, 5678h
POP AX
RET
Show answer
InstructionAXSP[FFFCh]
MOV AX, 1234h1234hFFFEh—
PUSH AX1234hFFFCh1234h
MOV AX, 5678h5678hFFFCh1234h
POP AX1234hFFFEh1234h

PUSH writes a copy of AX; AX does not change. POP restores the 1234h on top of the stack; the value at FFFCh is not erased, it just ends up outside the stack. This is the pattern for temporarily using a register without destroying it.

Output Question · PUSH and POP

Question 7: Byte Order on the Stack

When the program ends, what are BX, CL, CH and DX? (In a .COM program DS = SS, so [BX] accesses the stack.)

ORG 100h
MOV AX, 0ABCDh
PUSH AX
MOV BX, SP
MOV CL, [BX]
MOV CH, [BX+1]
POP DX
RET
Show answer

BX = FFFCh (SP after the PUSH).

[FFFCh] = CDh → CL; [FFFDh] = ABh → CH. The word is stored little-endian: the low byte at the lower address.

DX = ABCDh: POP reads the two bytes back as a word. The stack window of emu8086 shows words; the memory window shows bytes.

On the 8086, SP cannot be a base register (MOV CL, [SP] is invalid); that is why SP was copied into BX first.

Multiple Choice · PUSH and POP

Question 8: The One That Changes Flags

Which of the following instructions changes the flags?

C: POPF loads the word on top of the stack into the flags register; any flag can change. PUSH, POP and PUSHF do not touch the flags. This is why the result of a CMP can be carried safely across a PUSH/POP; if the flags themselves must be saved, use PUSHF/POPF.

Output Question · PUSH and POP

Question 9: PUSHF and POPF

At the start IF = 1 and the other flags are 0. When RET is reached, what are AX, CF and ZF? What would BX and the flags be if POP BX were written instead of POPF?

ORG 100h
MOV AX, 0FFFFh
ADD AX, 1
PUSHF
ADD AX, 1
POPF
RET
Show answer
  • First ADD: AX = 0000h, CF = 1, ZF = 1 (PF = AF = 1). PUSHF puts the flags word (F257h) on the stack.
  • Second ADD: AX = 0001h, CF = 0, ZF = 0.
  • POPF: the flags come back → CF = 1, ZF = 1; AX stays 0001h, since POPF loads only the flags.

With POP BX: BX = F257h (the saved flags word), and the flags stay as after the second ADD: CF = 0, ZF = 0.

Output Question · Examples

Question 10: POP in the Same Order

After the code runs, what are AX, BX and CX? What does SP become?

ORG 100h
MOV AX, 2
MOV BX, 4
MOV CX, 8
PUSH AX
PUSH BX
PUSH CX
POP AX
POP BX
POP CX
RET
Show answer

AX = 0008h, BX = 0004h, CX = 0002h.

CX, pushed last, comes out first and goes to AX. PUSH and POP in the same order reverse the values; to preserve them, POP must be written in reverse order (POP CX, POP BX, POP AX). Three PUSHes, three POPs: SP is FFFEh again, the stack is balanced.

Multiple Choice · Examples

Question 11: Swapping with the Stack

Which sequence swaps the values of AX and BX using only PUSH and POP?

B: Since BX went in last, the first POP AX takes it, then POP BX takes the old value of AX. A and D are mirror order: the values stay where they were (the register-saving pattern). C pops each value right away, so nothing changes. The shortest way to swap is still XCHG AX, BX (1 byte).

Step-by-Step Trace · Examples

Question 12: Rotating Three Registers

Three registers are pushed and popped back in a different order. What are AX, BX and CX at the end? Is the stack balanced?

AX1111hBX2222hCX3333hSPFFFEhStackFFF8····FFFA····FFFC····FFFE0000h← SP

AX = 1111h, BX = 2222h, CX = 3333h. SP = FFFEh.

AX1111hBX2222hCX3333hSPFFFChStackFFF8····FFFA····FFFC1111h← SPFFFE0000h

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

AX1111hBX2222hCX3333hSPFFFAhStackFFF8····FFFA2222h← SPFFFC1111hFFFE0000h

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

AX1111hBX2222hCX3333hSPFFF8hStackFFF83333h← SPFFFA2222hFFFC1111hFFFE0000h

PUSH CX: SP = FFF8h. 3333h is on top.

AX3333hBX2222hCX3333hSPFFFAhStackFFF8····FFFA2222h← SPFFFC1111hFFFE0000h

POP AX: the 3333h on top goes to AX, SP = FFFAh.

AX3333hBX2222hCX2222hSPFFFChStackFFF8····FFFA····FFFC1111h← SPFFFE0000h

POP CX: the next value, 2222h, goes to CX.

AX3333hBX1111hCX2222hSPFFFEhStackFFF8····FFFA····FFFC····FFFE0000h← SP

POP BX: 1111h goes to BX, SP = FFFEh: three PUSHes, three POPs, the stack is balanced. Result: AX = 3333h, BX = 1111h, CX = 2222h; the values were shifted in a circle.

ORG 100h
MOV AX, 1111h
MOV BX, 2222h
MOV CX, 3333h
PUSH AX
PUSH BX
PUSH CX
POP AX
POP CX
POP BX
RET

Coding Question · Examples

Question 13: Reverse a String in Place

Write code that uses the stack to reverse the string msg DB 'ABC' in place in memory (CBA).

Show answer
LEA SI, msg
MOV CX, 3
put:
MOV AL, [SI]
PUSH AX
INC SI
LOOP put
LEA SI, msg
MOV CX, 3
get:
POP AX
MOV [SI], AL
INC SI
LOOP get

The first loop pushes the characters one by one (as words together with AH; a byte cannot be pushed). The second loop pops them in reverse order and writes them from the start: msg = 'CBA'. Three PUSHes, three POPs: the stack stays balanced.

Output Question · Stack Safety

Question 14: POP from an Empty Stack

A .COM program starts with SP = FFFEh and executes POP without having pushed anything. What are AX and SP after each instruction? Where does the final RET return to?

ORG 100h
MOV BX, 7
POP AX
PUSH BX
RET
Show answer
  • POP AX: the 0000h put there by DOS is read → AX = 0000h; SP = FFFEh + 2 = 0000h (wraps around in 16 bits).
  • PUSH BX: SP = 0000h − 2 = FFFEh; [FFFEh] = 0007h. The 0000h from DOS was overwritten.
  • RET: IP ← 0007h. Execution jumps into the middle of the PSP; the program does not terminate properly.

The 8086 does not check for underflow: there is no error, only corruption. A value that was not pushed must not be popped.

Multiple Choice · Stack Safety

Question 15: What Is SP?

With SP = FFFEh, the following run in order: PUSH AX, PUSH BX, PUSHF, POP CX, PUSH DS, POP DX, POP BX. What is SP afterward?

B: Four pushes (AX, BX, FLAGS, DS) decrease SP by 8, three pops increase it by 6: FFFEh − 8 + 6 = FFFCh. A copy of AX is still left on the stack; in this case the final RET would take that value as the return address. CX gets the flags word, DX gets DS; BX returns to its old value.

The End

Chapter 8.1: The Stack: Study Questions
Lecture slides: Chapter 8.1: The Stack · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.