Assembly Language - Procedures (Subroutines)
A procedure (Procedure) is the mechanism for reusing functions/subroutines in assembly; it makes code structured and reusable. Understanding the relationship between procedures and the stack is an important milestone in assembly programming.
What is a procedure
A procedure is a block of code that can be called multiple times, similar to a function (Function) or subroutine (Subroutine) in high-level languages.
In assembly, procedures are invoked via CALL instructions and return via RET instructions.CALL pushes the return address onto the stack,RET pops the return address from the stack and jumps back.
Example
; File path: simple_proc.asm
; Simplest example of a procedure call
section
.data
msg
db
'Hello, runoob!'
,
0xA
len
equ
$
msg
section
.text
global
_start
_start
:
call
print_message
; Call the procedure (push the next instruction address onto the stack)
call
print_message
; Call it again
mov
eax
,
1
mov
ebx
,
0
int
0x80
; Procedure: print a message
print_message
:
push
eax
; Save the registers to be modified
push
ebx
push
ecx
push
edx
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
msg
mov
edx
,
len
int
0x80
pop
edx
; Restore the registers (in reverse order)
pop
ecx
pop
ebx
pop
eax
ret
; Return to the caller (pop the return address from the stack)
Inside a procedure, all modified registers should be saved and restored (except EAX when used as the return value); this is basic assembly etiquette. Failing to do so will cause the caller's register values to be modified unexpectedly.
Principles of CALL and RET
CALL and RET work as a pair, relying on the stack to manage return addresses:
| Instruction | Operation actually performed |
|---|---|
CALL label |
push eip(save the next instruction address) + jmp label |
RET |
pop eip(pop the address from the stack and jump) |
Passing parameters via registers
Place parameters into agreed-upon registers before calling the procedure:
Example
; File path: proc_params_reg.asm
; Pass parameters via registers
section
.data
newline
db
0xA
section
.text
global
_start
_start
:
; Call the add_two procedure: compute 10 + 20
mov
eax
,
10
; First parameter in eax
mov
ebx
,
20
; Second parameter in ebx
call
add_two
; Call the procedure
; Return value in eax = 30
mov
ebx
,
eax
; Exit code = 30
mov
eax
,
1
int
0x80
; Procedure: compute eax + ebx, return the result in eax
add_two
:
add
eax
,
ebx
; eax = eax + ebx
ret
Passing parameters via the stack
Passing parameters via the stack is more flexible and is the standard approach in high-level languages such as C:
Example
; File path: proc_params_stack.asm
; Pass parameters via the stack (cdecl style)
section
.data
msg
db
'Sum is: '
msg_len
equ
$
msg
newline
db
0xA
section
.bss
result_buf
resb
4
section
.text
global
_start
_start
:
; Call the sum procedure: compute 100 + 200
push
dword
200
; Push the 2nd parameter
push
dword
100
; Push the 1st parameter
call
sum
; Call the procedure
add
esp
,
8
; * Caller cleans up the stack (cdecl convention)
; Return value in eax = 300
mov
ebx
,
eax
mov
eax
,
1
int
0x80
; Procedure: sum(a, b) = a + b
sum
:
push
ebp
; * Save the old base pointer
mov
ebp
,
esp
; * Set up the new stack frame base
; Stack layout (from high to low addresses):
; [ebp+12] = parameter b (200)
; [ebp+8] = parameter a (100)
; [ebp+4] = return address
; [ebp] = old ebp
; [ebp-4] = local variable space
mov
eax
,
[
ebp
8
]
; Get the 1st parameter (a)
add
eax
,
[
ebp
12
]
; Add the 2nd parameter (b)
pop
ebp
; * Restore the old base pointer
ret
; Return
Stack frame structure diagram:
Below is the corresponding textual illustration:
高地址
+------------------+
| 参数2 (200) | <-- [ebp + 12]
+------------------+
| 参数1 (100) | <-- [ebp + 8]
+------------------+
| 返回地址 | <-- [ebp + 4]
+------------------+
| 旧的 EBP | <-- [ebp] (当前 EBP 指向这里)
+------------------+
| 局部变量区 | <-- [ebp - 4], [ebp - 8], ...
+------------------+ <-- ESP 指向这里
低地址
Function prologue (Prologue)
push ebp; mov ebp, espand epilogue (Epilogue)pop ebp; ret(orleave; ret) are the standard stack frame management template; almost all assembly functions begin and end with this structure.
Return values of procedures
Return values are usually placed in the EAX register (32-bit value) or EDX:EAX (64-bit value):
Example
; Return value example
; Return int
call
get_answer
; eax = 42
; Return a 64-bit value (e.g. long long)
call
get_big_value
; edx:eax = 64-bit result
get_answer
:
mov
eax
,
42
; Return value placed in eax
ret
get_big_value
:
mov
eax
,
0x00000001
; Low 32 bits
mov
edx
,
0x00000000
; High 32 bits
ret
local variables
Local variables use stack space, allocated in the procedure prologue by decreasing ESP:
Example
; File path: local_vars.asm
; Use local variables in a procedure
section
.text
global
_start
_start
:
push
dword
10
push
dword
20
call
multiply_add
add
esp
,
8
; eax = 10 * 20 + 10 + 20 = 230
mov
ebx
,
eax
mov
eax
,
1
int
0x80
; Procedure: multiply_add(x, y) = x*y + x + y
multiply_add
:
push
ebp
mov
ebp
,
esp
sub
esp
,
8
; * Allocate 8 bytes of local variable space on the stack
; Now [ebp-4] and [ebp-8] are usable local variables
mov
eax
,
[
ebp
8
]
; x
mov
ebx
,
[
ebp
12
]
; y
; [ebp-4] = x * y
imul
eax
,
ebx
mov
[
ebp
4
]
,
eax
; Local variable 1 = x * y
; [ebp-8] = x + y
mov
eax
,
[
ebp
8
]
add
eax
,
[
ebp
12
]
mov
[
ebp
8
]
,
eax
; Local variable 2 = x + y
; Return value = [ebp-4] + [ebp-8]
mov
eax
,
[
ebp
4
]
add
eax
,
[
ebp
8
]
; * Clean up local variable space and restore
mov
esp
,
ebp
; Equivalent to add esp, 8
pop
ebp
ret
Local variables are allocated in the stack frame and freed automatically after the procedure returns. Using
leaveinstruction can replacemov esp, ebp; pop ebp, with equivalent effect.
Comparison of procedure calling conventions
| Convention | to the parameter | Stack cleaner | Register saving | Return Value |
|---|---|---|---|---|
| cdecl | Stack (pushed right to left) | Caller | EAX,ECX,EDX saved by the caller | EAX |
| stdcall | Stack (pushed right to left) | Callee | EAX,ECX,EDX saved by the caller | EAX |
| fastcall | ECX, EDX + stack | Callee | EAX,ECX,EDX saved by the caller | EAX |