Chapter 8.2: Procedures
Multiple Choice · Definition
Question 1: Directive or Instruction?
Which of the following is not an assembler directive but an instruction the processor executes?
C: CALL (E8 for a near call) and RET (C3) are instructions that become machine code. PROC/ENDP tell the assembler where a procedure starts and ends and what type (NEAR/FAR) it is; END says the source is finished. Directives take no space in memory.
Output Question · Definition
Question 2: Falling into a Procedure
The programmer forgot to write RET at the end of the main program. What is AX when the program ends? How many times does the procedure twice run?
ORG 100h MOV AX, 2 CALL twice twice PROC ADD AX, AX RET twice ENDP
Show answer
AX = 0008h; the procedure runs twice.
- CALL twice: the return address 0106h is pushed; AX = 2 + 2 = 4; RET returns to 0106h.
- 0106h is the procedure's own first instruction: the processor does not "see" ENDP, so execution falls into the procedure. AX = 8.
- This RET takes the 0000h that DOS put on the stack, and the program ends.
Fix: RET after the CALL; procedures are written after the main program's RET.
Multiple Choice · CALL and RET
Question 3: What Does RET Do?
What does the RET instruction at the end of a near procedure do?
A: RET takes the word on top of the stack as the return address into IP and increases SP by 2 (like POP IP). B describes the first half of CALL. The far return, which takes CS and IP together, is RETF (D). The return address is kept on the stack, not in a register (C).
Output Question · CALL and RET
Question 4: CALL, RET and the Return Address
When CALL SQUARE runs, what is written to the stack, and what happen to SP and IP? When the program ends, what are AX and DX? (The comments are instruction addresses.)
ORG 100h MOV AX, 3 ; 0100h CALL SQUARE ; 0103h ADD AX, 1 ; 0106h RET ; 0109h SQUARE PROC MUL AX ; 010Ah RET ; 010Ch SQUARE ENDP
Show answer
CALL pushes the address of the next instruction: SP = FFFCh, [FFFCh] = 0106h; IP ← 010Ah (SQUARE).
MUL AX: DX:AX ← 3 × 3 = 9 → AX = 0009h, DX = 0000h (the 16-bit MUL writes DX too).
The RET in SQUARE puts 0106h into IP, SP = FFFEh. ADD AX, 1 → AX = 000Ah, DX = 0000h; the final RET ends the program.
Classic Question · CALL and RET
Question 5: Machine Code of CALL
A near CALL is 3 bytes: E8 + a 16-bit displacement. (a) CALL m1 at 0100h, with m1 at 0107h. (b) CALL p at 0120h, with p at 0100h. What are the machine codes, and which address is pushed?
Show answer
Displacement = target − (CALL address + 3).
(a) 0107h − 0103h = 0004h → E8 04 00; 0103h is pushed.
(b) 0100h − 0123h = −23h = FFDDh → E8 DD FF; 0123h is pushed.
The 16-bit value is stored little-endian: low byte first. Because the target is stored as a displacement, not an absolute address, the code still works if it is moved in memory.
Multiple Choice · CALL and RET
Question 6: Three CALLs, No RET
With SP = FFFEh, three near CALLs are executed back to back (nested calls, no RET yet). What is SP?
D: Each near CALL pushes a 2-byte return address: FFFEh − 3 × 2 = FFF8h. The stack grows downward, SP decreases (C is wrong). FFF2h would correspond to 4-byte far calls (FFFEh − 3 × 4), FFFBh to a 1-byte push; the 8086 always writes words to the stack.
Step-by-Step Trace · Example Programs
Question 7: Nested Calls
main calls A, and A calls B. How many return addresses pile up on the stack at most? In what order do the RETs return? What is AX at the end?
AX = 1. SP = FFFEh; the stack holds the 0000h from DOS.
CALL A: the return address 0106h is pushed (SP = FFFCh), IP = 0107h.
Inside A, AX = 3.
CALL B from inside A: 010Dh is pushed, SP = FFFAh. There are two return addresses on the stack.
B: AX = AX + AX = 6.
B's RET takes the top value 010Dh: it returns to A (LIFO). SP = FFFCh.
A continues: AX = 10 = 000Ah.
A's RET takes 0106h: it returns to main, SP = FFFEh. The last one called returned first. main's RET ends the program: AX = 000Ah.
ORG 100h MOV AX, 1 CALL A RET A PROC ADD AX, 2 CALL B ADD AX, 4 RET A ENDP B PROC ADD AX, AX RET B ENDP
Output Question · Parameters
Question 8: Parameters in Registers
Procedure m2 receives its parameters in AL and BL and returns the result in AX. What is AX when the program ends? Why does the second call give a different result?
ORG 100h MOV AL, 5 MOV BL, 3 CALL m2 CALL m2 RET m2 PROC ; In: AL, BL MUL BL ; Out: AX = AL * BL RET m2 ENDP
Show answer
1st call: AX = 5 × 3 = 15 = 000Fh. AL is now 0Fh.
2nd call: AX = 0Fh × 3 = 45 = 002Dh.
MUL BL writes the result to AX and changes AL; the first parameter of the second call becomes the result of the first. A procedure's inputs, outputs and the registers it destroys (here AX, CF, OF) should be written as a comment at its head.
Multiple Choice · Parameters
Question 9: Ways to Pass Parameters
Which statement about ways of passing parameters is correct?
B: When passing by address (pointer), only the start address of the array is passed; the data is not copied (SI in the print_me example). Registers are the fastest way (A). Memory variables are permanent (C). On the 8086 SP cannot be a base register; parameters on the stack are read with BP ([BP+4]) (D).
Spot the Bug · Parameters
Question 10: Why Does the Loop Never End?
The program was written to print "**" 3 times, but it never ends. Why? Suggest two fixes.
ORG 100h MOV CX, 3 again: CALL stars LOOP again RET stars PROC MOV CX, 2 inner: MOV AL, '*' MOV AH, 0Eh INT 10h LOOP inner RET stars ENDP
Show answer
stars uses CX, the main loop's counter, for its own loop. When the procedure returns, CX = 0. The LOOP in the main loop makes CX FFFFh and jumps back; since every call sets CX to 0 again, this goes on forever: stars are printed on the screen without end.
- Protect it in the procedure:
PUSH CXon entry,POP CXbefore RET. Then the output is 6 stars. - Or the caller protects it:
PUSH CX/CALL stars/POP CX.
Multiple Choice · Parameters
Question 11: RET 4
A procedure called with a near CALL after pushing two parameters returns with RET 4. By how much in total does SP increase when RET 4 runs?
A: RET first takes the return address (SP + 2), then adds 4 more to SP for the parameters: 6 in total. This way the caller does not have to write ADD SP, 4; SP returns to its value before the two PUSHes and the CALL (FFFEh).
Step-by-Step Trace · Parameters
Question 12: Parameters on the Stack and BP
DIFF reads the two parameters on the stack with BP and subtracts the second from the first. What do [BP+4] and [BP+6] refer to? What is SP on return?
1st parameter (10 = 000Ah) on the stack: [FFFCh].
2nd parameter (4) on the stack: [FFFAh], SP = FFFAh.
CALL: the return address 010Bh → [FFF8h], IP = 010Ch.
PUSH BP and MOV BP, SP: BP = FFF6h is a fixed frame point. [BP] = old BP, [BP+2] = return address, [BP+4] = the last pushed parameter.
MOV AX, [BP+6]: [FFFCh] = 000Ah → AX = 10.
SUB AX, [BP+4]: 10 − 4 = 6.
POP BP; RET 4: IP = 010Bh, SP = FFF8h + 2 + 4 = FFFEh. The parameters are discarded, the stack is balanced.
ORG 100h MOV AX, 10 PUSH AX MOV AX, 4 PUSH AX CALL DIFF RET DIFF PROC PUSH BP MOV BP, SP MOV AX, [BP+6] SUB AX, [BP+4] POP BP RET 4 DIFF ENDP
Multiple Choice · Parameters
Question 13: Location of the First Parameter
The caller pushes three parameters and makes a near CALL; the procedure starts with PUSH BP and MOV BP, SP. Where is the first pushed parameter?
B: [BP] is the old BP, [BP+2] the return address, [BP+4] the last pushed (third) parameter, [BP+6] the second, [BP+8] the first pushed parameter. Parameters are at positive, local variables (the space opened with SUB SP, n) at negative BP offsets.
Step-by-Step Trace · Example Programs
Question 14: A Procedure That Converts Letters
Procedure to_upper converts the lowercase letter in AL to uppercase and leaves other characters unchanged. When the loop ends, what is msg?
SI = address of msg (011Ch), CX = 3 characters.
Round 1: AL = 'a' (61h). 'a' ≤ AL ≤ 'z' → SUB AL, 20h: 'A' (41h). After RET it is written to memory.
Round 2: AL = '1' (31h) < 'a' → CF = 1, JB done: returns unchanged, '1' stays in memory. JB is an unsigned comparison.
Round 3: 'b' → 'B'.
LOOP: CX = 0, the loop ends. msg = 'A1B'. Since to_upper does not touch CX, the LOOP counter is safe; input and output are in AL.
ORG 100h LEA SI, msg MOV CX, 3 next_char: MOV AL, [SI] CALL to_upper MOV [SI], AL INC SI LOOP next_char RET to_upper PROC CMP AL, 'a' JB done CMP AL, 'z' JA done SUB AL, 20h done: RET to_upper ENDP msg DB 'a1b'
Multiple Choice · Common Mistakes
Question 15: JMP Instead of CALL
The programmer accidentally wrote JMP add5 instead of CALL add5. In a .COM program, what does the RET at the end of add5 do?
ORG 100h MOV AX, 1 JMP add5 MOV BX, 9 ; does it run? RET add5 PROC ADD AX, 5 RET add5 ENDP
A: JMP does not push a return address. RET takes whatever is on top of the stack into IP; if the main program pushed nothing, this is the 0000h of DOS: the INT 20h in the PSP quietly ends the program and the lines after the JMP never run. If there were another value on the stack, it would return to an arbitrary address.
Multiple Choice · Common Mistakes
Question 16: Preserving the Result
A procedure returns its result in AX; on entry it does PUSH AX and before RET it does POP AX. What happens?
C: POP AX restores the value from entry and so erases the computed result. The stack is balanced because the PUSH and POP counts are equal, and RET returns correctly (A). The rule: a procedure preserves the registers it uses, but not the register that returns the result.
Coding Question · Example Programs
Question 17: Return the Larger One
Write a procedure bigger that returns the larger of the unsigned numbers in AX and BX in AX. The procedure must not destroy any other register. Also give a calling example.
Show answer
; In: AX, BX (unsigned)
; Out: AX = the larger one
bigger PROC
CMP AX, BX
JAE done
MOV AX, BX
done:
RET
bigger ENDPMOV AX, 5 MOV BX, 9 CALL bigger ; AX = 9
JAE (CF = 0) is used for unsigned comparison. Only AX changes; since no other register is used, no PUSH/POP is needed. Tested: (5, 9) → 9, (9, 5) → 9, (7, 7) → 7, (FFFFh, 1) → FFFFh; CX and BX are preserved. For signed numbers JGE would be used.
Step-by-Step Trace · Common Mistakes
Question 18: A Procedure That Calls Itself
fact computes AX = CX! by calling itself (CX = 3). How deep does the stack get at most? What would happen without the stopping condition?
CALL fact: 0109h pushed, SP = FFFCh. CX = 3.
CX = 3 > 1: AX = 1 × 3 = 3, CX = 2; it calls itself: 0115h pushed, SP = FFFAh.
AX = 3 × 2 = 6, CX = 1; CALL again: the third return address, SP = FFF8h.
CMP CX, 1 → CX ≤ 1, JBE done: the stopping condition. The first RET returns to 0115h (SP = FFFAh).
The second RET also returns to 0115h (SP = FFFCh): each RET consumes one return address, and the RET at 0115h runs repeatedly.
The third RET returned to 0109h, SP = FFFEh. Main program: AX = 6. Without the stopping condition (CMP/JBE), each call would take 2 bytes and the stack would grow until it overwrote the code (stack overflow).
ORG 100h MOV CX, 3 MOV AX, 1 CALL fact RET fact PROC CMP CX, 1 JBE done MUL CX DEC CX CALL fact done: RET fact ENDP
Spot the Bug · Common Mistakes
Question 19: Unbalanced Stack
Where does the RET in procedure SUBR return to? Why does the program crash? How is it fixed?
ORG 100h MOV AX, 1234h ; 0100h CALL SUBR ; 0103h MOV BX, 1 ; 0106h RET SUBR PROC PUSH AX ; save AX ; ... work ... RET SUBR ENDP
Show answer
CALL pushes the return address 0106h; PUSH AX puts 1234h on top of it.
RET takes the word on top of the stack as the return address: IP ← 1234h. The processor executes random bytes at CS:1234h as instructions; the program crashes or behaves unpredictably. MOV BX, 1 never runs.
Fix: POP AX before RET. Rule: when leaving a procedure SP must equal its value on entry; every PUSH must have a POP in reverse order (on every branch).
Multiple Choice · Common Mistakes
Question 20: Name Mismatch
Which one is a procedure error that the assembler can catch at assembly time?
D: The names at PROC and ENDP must be the same; a mismatch is a syntax error. The others are valid code and show up only at run time: without RET execution falls into the code after the procedure, with unbalanced PUSH/POP the RET returns to the wrong address, a destroyed CX confuses the loop.