0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the bottom.
- Array-sum with n = 5: total cycles with MOV 1, LOAD 3, ADD 1, SUB 1, BNZ 2, STORE 3, HALT 1? from Lesson 1.3.1
−(R)changes R when? from Lesson 1.2.2- 16-bit instructions, 4-bit fields, 15 + 15 + 15 classes, how many zero-address? from Lesson 1.1.4
1 · The Hook
🎣
A function call has to remember where to come back to, and calls nest: main calls f, f calls g, g returns to f, f returns to main. The last call made is the first to return, which is exactly a stack. This lesson puts the stack in memory and builds CALL and RET on it.
2 · Predict First
🤔
SP = 1000, the stack grows toward lower addresses, and each push stores 4 bytes. After PUSH R1, PUSH R2, POP R3, what is SP, and what is in R3?
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
💡
Predict First — the answer
Push: 996, push: 992, pop: 996. SP = 996; R3 holds R2's value (last in, first out).
The stack in memory
A region of memory used last-in, first-out, with a stack pointer (SP) register. Common convention (used here): the stack grows downward (toward lower addresses) and SP points to the top item.
- PUSH X: SP ← SP − w; M[SP] ← X (w = word size). Same as
MOV −(SP), X(auto-decrement). - POP X: X ← M[SP]; SP ← SP + w. Same as
MOV X, (SP)+(auto-increment).
Subroutine call and return
- CALL target: push the return address (the address of the instruction after the CALL, which the PC already holds), then PC ← target.
- RET: pop the top of the stack into the PC.
Nested calls stack up return addresses; each RET takes the most recent one. Because the return address is on a stack, a subroutine can call itself (recursion).
Stack frames
Each active call gets a stack frame (activation record): return address, saved registers, parameters and local variables. A frame pointer (FP) marks a fixed point in the frame, so locals are reached as FP − d (base + displacement mode, Lesson 1.2.2). Stack depth for recursion = number of active calls × frame size.
Registers a subroutine overwrites must be saved and restored (pushed and popped), either by the caller or by the subroutine (callee).
RISC vs CISC
| RISC (reduced instruction set) | CISC (complex instruction set) | |
|---|---|---|
| Instructions | few, simple | many, some very complex |
| Length | fixed (often 32 bits) | variable |
| Addressing modes | few | many |
| Memory operands | only LOAD/STORE (load/store architecture) | most instructions may use memory |
| Registers | many general-purpose | fewer |
| CPI | about 1 per instruction (pipelined) | several cycles per instruction |
| Control unit (Phase 3) | usually hardwired | often microprogrammed |
| Examples | ARM, RISC-V, MIPS | x86, VAX |
RISC programs have more instructions (higher IC) but lower CPI and shorter cycle times; the performance equation (Lesson 0.1.2) decides who wins.
4 · Visual
main at 100: CALL A (4 bytes) A at 300: CALL B (4 bytes) SP starts at 0x8000
after CALL A after CALL B after RET (in B)
0x7FFC | 104 | <- SP 0x7FFC | 104 | 0x7FFC | 104 | <- SP
0x7FF8 | 304 | <- SP PC = 304 (back in A)
5 · Worked Examples
Example 1 Easy
A 4-byte CALL instruction is at address 2000. What return address is pushed?
Solution
2004, the address of the next instruction.
Example 2 Medium
SP = 0x8000; main's CALL A is at 100, A's CALL B is at 300 (both 4 bytes). While B runs, what is SP and what is on top of the stack?
Solution
Two pushes: SP = 0x8000 − 8 = 0x7FF8; top = 304 (A's return point).
Example 3 GATE
f(n) calls f(n − 1) until n = 0. main calls f(10). Each frame is 16 bytes. What is the maximum stack space used by f's frames?
Solution
Active calls at the deepest point: f(10) … f(0) = 11 frames → 11 × 16 = 176 bytes.
6 · GATE Traps
⚠️
- The pushed return address is the next instruction's address, not the CALL's.
- With a downward-growing stack, PUSH decreases SP.
- f(10) down to f(0) is 11 calls, not 10.
- RISC means fewer cycles per instruction, but usually more instructions.
7 · Memory Hook
🧠
"CALL pushes where to come back; RET pops it into the PC." Last call in, first return out.
8 · Connect the Dots
Phase 1 is complete: instruction formats and encodings, addressing modes, tracing and counting, stack and subroutines. Phase 2 opens the CPU: the ALU that computes and the datapath that moves values between registers.
9 · Self-Test
GATE format. Try every question before opening the vault.
Q1NAT1 markStack pointer
SP = 1000; stack grows downward; 4-byte words. After PUSH R1, PUSH R2, POP R3, PUSH R4, what is SP?
Numerical answer: ________
Q2NAT1 markReturn address
A 6-byte CALL instruction starts at address 5000. What return address is pushed?
Numerical answer: ________
Q3MCQ1 markRISC
Which is a typical RISC property?
- (A) Only LOAD and STORE access memory
- (B) Many complex addressing modes
- (C) Variable-length instructions
- (D) Arithmetic instructions with memory operands
Q4NAT2 marksRecursion depth
g(n) calls g(n − 2) while n > 0. main calls g(15). Each frame is 24 bytes. Maximum bytes of stack used by g's frames?
Numerical answer: ________
Q5MSQ2 marksCALL and RET
Which statements are true?
- (A) Nested calls return in the same order as they were made.
- (B) A stack frame can hold local variables of the call.
- (C) RET loads the PC from the top of the stack.
- (D) CALL saves the return address before changing the PC.
10 · Rule Card
- Downward stack, SP at top: PUSH = SP −= w, M[SP] ← X; POP = X ← M[SP], SP += w.
- CALL: push return address (next instruction), PC ← target. RET: PC ← pop.
- Recursion depth = active calls × frame size; count calls carefully (f(10)…f(0) = 11).
- RISC: fixed length, load/store, few modes, many registers, CPI ≈ 1. CISC: the opposite.
11 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
stack pointer (SP)downward-growing stackPUSH/POP via auto-decrement/incrementCALL and RETreturn addressnested callsrecursionstack frame (activation record)frame pointer (FP)caller-saved vs callee-savedRISCCISC
12 · Answer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Recall Warm-up
-
- Before the access.
-
Self-Test
Q1 Answer: 9921 mark
1000 − 4 − 4 + 4 − 4 = 992.
Re-read: Lesson 1.3.2 — Stack, subroutine call and return; RISC vs CISC
Q2 Answer: 50061 mark
The next instruction starts at 5006.
Re-read: Lesson 1.3.2 — Stack, subroutine call and return; RISC vs CISC
Q3 Answer: (A)1 mark
RISC is a load/store architecture.
- (B) is wrong: RISC keeps few modes.
- (C) is wrong: RISC uses fixed-length instructions.
- (D) is wrong: That is CISC.
Re-read: Lesson 1.3.2 — Stack, subroutine call and return; RISC vs CISC
Q4 Answer: 2162 marks
Calls: 15, 13, 11, 9, 7, 5, 3, 1, −1 → 9 active frames × 24 = 216.
Re-read: Lesson 1.3.2 — Stack, subroutine call and return; RISC vs CISC
Q5 Answer: (B) (C) (D)2 marks
Nested calls return in reverse order (last in, first out).
- (A) is wrong: The most recent call returns first.
Re-read: Lesson 1.3.2 — Stack, subroutine call and return; RISC vs CISC