Detailed Explanation of call and ret Instructions in Assembly Language

ret and retf Instructions

<span>call</span> and <span>ret</span> are transfer instructions that control the program flow by modifying the IP (Instruction Pointer) or simultaneously modifying both the CS (Code Segment Register) and IP. These instructions are typically used together to implement the design of subroutines (functions).

ret Instruction

<span>ret</span> (return) instruction pops data from the stack to modify the content of IP, achieving a near transfer (intra-segment transfer).

The operations performed by the CPU when executing the <span>ret</span> instruction are:

  1. <span>(IP) = ((ss)*16 + (sp))</span> – Pops the word data from the top of the stack into IP
  2. <span>(sp) = (sp) + 2</span> – Increments the stack pointer by 2

In assembly pseudocode, this is equivalent to:

pop IP

retf Instruction

<span>retf</span> (return far) instruction pops data from the stack to modify both CS and IP, achieving a far transfer (inter-segment transfer).

The operations performed by the CPU when executing the <span>retf</span> instruction are:

  1. <span>(IP) = ((ss)*16 + (sp))</span> – Pops the word data from the top of the stack into IP
  2. <span>(sp) = (sp) + 2</span> – Increments the stack pointer by 2
  3. <span>(CS) = ((ss)*16 + (sp))</span> – Pops the new top word data into CS
  4. <span>(sp) = (sp) + 2</span> – Increments the stack pointer again by 2

In assembly pseudocode, this is equivalent to:

pop IP
pop CS

Example Program Analysis

assume cs:code
stack segment
    db 16 dup(0)  ; Define 16 bytes of stack space, initialized to 0
stack ends

code segment
start:
    mov ax, stack
    mov ss, ax    ; Set stack segment
    mov sp, 16    ; Set stack pointer

    mov ax, 0     ; Push 0 onto the stack
    push ax

    ret           ; Execute ret instruction

    mov ax, 4c00h
    int 21h
code ends
end start

In this example:

  1. The program first sets the stack segment (SS) and stack pointer (SP)
  2. Pushed 0 onto the stack
  3. When executing the <span>ret</span> instruction:
  • Pops 0 from the top of the stack into the IP register
  • SP increments by 2
  • Result: (IP)=0, CS:IP points to the first instruction of the code segment (i.e., <span>mov ax, stack</span>)
  • The program will restart execution
  • Key Points Summary

    1. <span>ret</span> is used for near returns, modifying only IP; <span>retf</span> is used for far returns, modifying both CS and IP
    2. Both achieve the setting of return addresses through stack operations
    3. <span>ret</span> is equivalent to <span>pop IP</span>, while <span>retf</span> is equivalent to <span>pop IP</span> followed by <span>pop CS</span>
    4. Subroutines are typically called using <span>call</span>, and returned using <span>ret</span> or <span>retf</span>, both must be used in matching pairs

    Understanding the coordination of <span>call</span> and <span>ret</span>/<span>retf</span> is fundamental to mastering subroutine calls in assembly language.

    Leave a Comment