0% found this document useful (0 votes)
4 views9 pages

cs401 - CHAPTER 5 by Mishi

Chapter 5 covers the CALL and RET instructions in assembly language, explaining their roles in subroutine control flow. CALL temporarily transfers control to a subroutine and saves the return address on the stack, while RET retrieves this address to resume execution. The chapter also discusses passing parameters, stack operations, and the importance of clearing the stack after subroutine execution.

Uploaded by

sheezailyas89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

cs401 - CHAPTER 5 by Mishi

Chapter 5 covers the CALL and RET instructions in assembly language, explaining their roles in subroutine control flow. CALL temporarily transfers control to a subroutine and saves the return address on the stack, while RET retrieves this address to resume execution. The chapter also discusses passing parameters, stack operations, and the importance of clearing the stack after subroutine execution.

Uploaded by

sheezailyas89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CHAPTER 5

📘 CALL and RET Instructions – Summary

1. CALL Instruction:

o Used for temporary control transfer to a subroutine.

o Syntax: CALL label

o The processor saves the address of the next instruction (after CALL) to the
stack.

o Execution jumps to the code at the label.

2. RET Instruction:

o Used to return from a subroutine back to the instruction after the CALL.

o No arguments are required.

o The processor retrieves the return address from the stack and continues
execution from there.

3. Permanent vs Temporary Diversion:

o JMP: Permanently changes control flow, never comes back.

o CALL: Temporarily changes control flow and returns with RET.

4. Independence:

o Technically, CALL and RET can be used separately.

o However, logically they are used together in proper code.

o Using them alone is only for advanced tricks.

✅ Questions and Answers

Q1: What is the purpose of the CALL instruction?


Ans: CALL is used to temporarily transfer control to a subroutine, and execution resumes
after the subroutine finishes.

Q2: What does the RET instruction do?


Ans: RET returns control from the subroutine back to the instruction following the CALL.

Q3: What is the main difference between JMP and CALL?


Ans: JMP permanently transfers control, while CALL temporarily transfers control and allows
returning back using RET.
Q4: Does RET instruction take any argument?
Ans: No, RET does not take any argument.

Q5: Can CALL and RET be used independently?


Ans: Yes, technically they can be used independently, but usually they are used together as a
logical pair.

✅ MCQs (Multiple Choice Questions)

Q1: Which instruction is used for temporary diversion of control?


a) JMP
b) MOV
c) CALL
d) RET
✅ Answer: c) CALL

Q2: Which instruction is used to return from a subroutine?


a) JMP
b) CALL
c) RET
d) PUSH
✅ Answer: c) RET

Q3: What is stored before executing a CALL instruction?


a) Data
b) Return address
c) Label
d) Instruction
✅ Answer: b) Return address

Q4: What is the main difference between CALL and JMP?


a) CALL is faster
b) JMP is temporary
c) CALL allows return
d) JMP allows return
✅ Answer: c) CALL allows return
Q5: RET instruction gets the return address from:
a) Code segment
b) Data segment
c) Stack
d) Register
✅ Answer: c) Stack

📘 Parameters in Assembly Language

1. Definition:

o Parameters are values passed from the calling code to a subroutine.

o They define what the subroutine should work on (e.g., array, size, type of
sorting).

2. Why Parameters Are Needed:

o Same subroutine may be used to sort different arrays.

o Sometimes sort order may be ascending or descending.

o Sometimes data may be signed or unsigned.

o These differences are handled using parameters.

3. Method of Passing Parameters:

o The most straightforward method in Assembly is to use registers.

o The CALL instruction does not affect general-purpose registers (except IP and
SP).

o So, AX, BX, CX, DX etc., can be used to pass information.

4. Example:

o CX can hold the size of the array.

o AX can be used to define sorting order (e.g., 1 for ascending, 0 for


descending).

o Subroutine reads from these registers and processes accordingly.

✅ Questions and Answers


Q1: What is a parameter in the context of subroutines?
Ans: A parameter is a value passed to a subroutine to control its operation, such as array size
or sort order.

Q2: Why do we need parameters in subroutines?


Ans: Parameters allow a subroutine to work with different inputs like various arrays, sizes,
and sorting orders, making it reusable.

Q3: What is the most straightforward way to pass parameters in Assembly?


Ans: By using general-purpose registers like AX, BX, CX, etc.

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.

✅ MCQs (Multiple Choice Questions)

Q1: What is the purpose of passing parameters to a subroutine?


a) To reduce memory usage
b) To make subroutine reusable for different data
c) To change the instruction pointer
d) To avoid using registers
✅ Answer: b) To make subroutine reusable for different data

Q2: Which register is NOT typically used for passing parameters in Assembly?
a) AX
b) BX
c) IP
d) CX
✅ Answer: c) IP

Q3: What does the CALL instruction do to the general-purpose registers?


a) Clears them
b) Fills them with zeros
c) Does not change them
d) Multiplies them by 2
✅ Answer: c) Does not change them
Q4: In Assembly, which of the following is the simplest method to pass parameters to
subroutines?
a) Memory-mapped I/O
b) Stack
c) Using registers
d) Hardcoding in the code
✅ Answer: c) Using registers

Q5: What is a good way to pass array size to a subroutine in Assembly?


a) Using the instruction pointer
b) Using AX register
c) Using CX register
d) Using memory segment
✅ Answer: c) Using CX register

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.

 Push = Add value on top of the stack

 Pop = Remove value from the top

How Stack Works in Assembly?

 SP (Stack Pointer): Keeps track of the top of the stack

 SS (Stack Segment): Tells where the stack is stored in memory

 In 8088, the stack works in words (2 bytes) and is decrementing (top moves
downward in memory)

Instructions:

 PUSH ax: SP -= 2; value of AX is stored at [SP]

 POP ax: AX = [SP]; SP += 2

Use with CALL and RET:

 CALL: Saves current IP on the stack, then jumps to function

 RET: Gets IP back from the stack to resume where it left


RET n:

Used to clean up extra parameters passed via the stack by increasing SP after returning.

💡 Questions for Practice:

❓Conceptual Questions:

1. What is the size of data that can be pushed onto the 8088 stack?

2. What does the RET instruction do?

3. Why is the order of PUSH and POP important?

✅ MCQs:

1. What does the PUSH instruction do in 8088?


a) Increments SP by 2
b) Decrements SP by 1
✅ c) Decrements SP by 2 and stores data
d) Does nothing

2. What does RET instruction do?


a) Jumps to a fixed address
✅ b) Pops the return address from stack into IP
c) Pushes data onto stack
d) Halts the CPU

3. What is the minimum data size for PUSH/POP in 8088?


a) 1 byte
✅ b) 2 bytes
c) 4 bytes
d) Any size

🔁 Saving and Restoring Registers

In subroutines, if we use registers like AX, CX, or SI, they might get overwritten. To avoid this,
we use:

 PUSH reg: Saves the value of the register to the stack.

 POP reg: Restores the value from the stack.

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.

Near call = within same segment.


Far call = different segment.

📦 Passing Parameters via Stack

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 lets us access:

 [bp+2] = return address

 [bp+4] = second parameter

 [bp+6] = first parameter

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.

📝 Important Questions (Theory)


1. What is the purpose of using PUSH and POP in subroutines?

2. Explain the difference between CALL and RET instructions.

3. Why is BP used to access parameters from the stack instead of SP?

4. What is the difference between caller-cleared and callee-cleared stacks?

5. Why is RET n preferred over popping each argument manually?

❓MCQs

1. What does the PUSH instruction do?


a) Increments SP by 2
b) Decrements SP by 2 and stores data at new SP
c) Pushes data from stack to register
d) Removes data from the stack
✅ Answer: b

2. Which register is used to access parameters in the stack?


a) SP
b) AX
c) BP
d) CX
✅ Answer: c

3. What does RET instruction do?


a) Saves return address on stack
b) Jumps to another subroutine
c) Loads return address from stack and returns control
d) Clears registers
✅ Answer: c

4. What happens if stack is not cleared after function call?


a) Return address is lost
b) Stack overflows eventually
c) Registers are reset
d) Memory is erased
✅ Answer: b

5. RET n instruction is used for:


a) Returning control without stack changes
b) Clearing stack manually
c) Returning and discarding parameters from the stack
d) Pushing data to stack
✅ Answer: c

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy