cs401 - CHAPTER 5 by Mishi
cs401 - CHAPTER 5 by Mishi
1. CALL Instruction:
o The processor saves the address of the next instruction (after CALL) to the
stack.
2. RET Instruction:
o Used to return from a subroutine back to the instruction after the CALL.
o The processor retrieves the return address from the stack and continues
execution from there.
4. Independence:
1. Definition:
o They define what the subroutine should work on (e.g., array, size, type of
sorting).
o The CALL instruction does not affect general-purpose registers (except IP and
SP).
4. Example:
Q4: Does the CALL instruction affect the contents of general-purpose registers?
Ans: No, CALL only changes IP (Instruction Pointer) and SP (Stack Pointer), not general-
purpose registers.
Q5: Give an example of how you would pass sorting order to a subroutine.
Ans: Store 1 in AX for ascending and 0 in AX for descending, then CALL the subroutine.
Q2: Which register is NOT typically used for passing parameters in Assembly?
a) AX
b) BX
c) IP
d) CX
✅ Answer: c) IP
What is a Stack?
A stack is a data structure that works on Last In First Out principle. You can only add or
remove items from the top. Imagine a test tube filled with balls – you can only remove the
top ball first.
In 8088, the stack works in words (2 bytes) and is decrementing (top moves
downward in memory)
Instructions:
Used to clean up extra parameters passed via the stack by increasing SP after returning.
❓Conceptual Questions:
1. What is the size of data that can be pushed onto the 8088 stack?
✅ MCQs:
In subroutines, if we use registers like AX, CX, or SI, they might get overwritten. To avoid this,
we use:
This ensures that when the function returns, the original register values are not lost.
📞 CALL and RET
CALL: Saves the return address (IP or CS:IP) to the stack and jumps to the procedure.
RET: Takes the return address from the stack and goes back to where CALL was made.
We can pass parameters using the stack when registers are not enough. But we cannot
directly POP parameters because the return address is on top of the stack.
To fix this:
asm
CopyEdit
push bp
mov bp, sp
This method does not disturb the stack structure and is commonly used.
🧹 Stack Clearing
After the subroutine finishes, the parameters are no longer needed and must be cleared to
avoid stack overflow.
Two options:
Caller clears: Adds extra code after each CALL to remove parameters.
Callee clears: Uses RET n to remove the return address and n bytes of parameters.
This is the standard method in most languages.
❓MCQs