Procedures: call & ret
A procedure (function, subroutine) is a piece of code you can run from many places. In assembly it is simply a label followed by instructions ending in ret. Two instructions make it work:
| Instruction | Effect |
|---|---|
call label |
push the address of the next instruction (the return address), then jump to label |
ret |
pop the return address from the stack and jump there |
That's all: no declarations, no parameter lists. Where arguments go and which registers survive is a convention — the topic of the next lesson. In this lesson we use its basic rules: the first argument goes in rdi, the second in rsi, and the result comes back in rax.
A first function
section .text
global _start
_start:
mov edi, 12
call square ; rax = 12 * 12
mov rdi, rax
call print_uint
mov edi, 25
call square
mov rdi, rax
call print_uint
mov eax, 60
xor edi, edi
syscall
; square: return rdi * rdi in rax
square:
mov rax, rdi
imul rax, rdi
ret
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret144
625
Step by step, the first call square:
- pushes the address of the following
mov rdi, raxonto the stack, - jumps to
square, which computes the result inrax, retpops that address intorip, and execution continues right after the call.
A print_str helper
Functions remove repetition. Instead of four instructions for every write, put them in a helper that takes the address in rsi and the length in rdx:
section .data
hello: db "Hello, "
hello_len equ $ - hello
name1: db "Ada", 10
name1_len equ $ - name1
name2: db "Linus", 10
name2_len equ $ - name2
section .text
global _start
_start:
mov rsi, hello
mov edx, hello_len
call print_str
mov rsi, name1
mov edx, name1_len
call print_str
mov rsi, hello
mov edx, hello_len
call print_str
mov rsi, name2
mov edx, name2_len
call print_str
mov eax, 60
xor edi, edi
syscall
; print_str: write rdx bytes starting at rsi to stdout
print_str:
mov eax, 1
mov edi, 1
syscall
retHello, Ada
Hello, Linus
Functions calling functions
Each call pushes another return address, so calls can nest as deeply as the stack allows. The catch is registers: the caller and the callee share the same sixteen. If sum_of_squares keeps its running total in a register that square overwrites, the total is lost. The solution is to save registers you need on the stack, or to use registers the convention promises are preserved (rbx, rbp, r12–r15) — saving and restoring them yourself if you change them:
section .text
global _start
_start:
mov edi, 3
mov esi, 4
call sum_of_squares ; 3*3 + 4*4
mov rdi, rax
call print_uint
mov eax, 60
xor edi, edi
syscall
; sum_of_squares: return rdi*rdi + rsi*rsi
sum_of_squares:
push rbx ; we will use rbx and r12: save the caller's values
push r12
mov r12, rsi ; keep the second argument safe
call square ; rax = rdi * rdi
mov rbx, rax ; keep the first square safe
mov rdi, r12
call square ; rax = rsi * rsi
add rax, rbx
pop r12 ; restore in reverse order
pop rbx
ret
; square: return rdi * rdi in rax
square:
mov rax, rdi
imul rax, rdi
ret
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret25
Multiple return values
There is no rule against returning more than one value in registers. The convention uses rax for the main result and rdx for a second one (as in div). A divmod function returns the quotient in rax and the remainder in rdx:
section .text
global _start
_start:
mov edi, 100
mov esi, 7
call divmod
mov rbx, rdx ; save the remainder
mov rdi, rax
call print_uint ; 100 / 7
mov rdi, rbx
call print_uint ; 100 % 7
mov eax, 60
xor edi, edi
syscall
; divmod: rax = rdi / rsi, rdx = rdi % rsi (unsigned)
divmod:
mov rax, rdi
xor edx, edx
div rsi
ret
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret14
2
When the stack is unbalanced
ret blindly pops whatever is on top of the stack and jumps there. If a function pushes something and forgets to pop it, ret "returns" to an address that is really data:
section .text
global _start
_start:
call broken
mov eax, 60
xor edi, edi
syscall
broken:
push 12345 ; pushed but never popped...
ret ; ...so this jumps to address 12345Program terminated with signal SIGSEGV (11)
The rule of thumb: every function leaves rsp exactly as it found it, and restores saved registers in the reverse order of saving.
Test yourself
What does call square push onto the stack?
- The address of square
- The address of the instruction after the call
- The value of rdi
- Nothing
A function does push rbx and push r12 at the start. In what order must it restore them?
- pop rbx, pop r12
- pop r12, pop rbx
- Any order
- They don't need restoring
Where does a function return its main result by convention?
- rdi
- rbx
- rax
- On the stack