call and ret in Assembly
call pushes the return address; ret pops and jumps back.
; call pushes the return address; ret pops and jumps back.
global _start
section .text
double: ; rdi -> rax = rdi * 2
lea rax, [rdi * 2]
ret
_start:
mov rdi, 21
call double ; rax = 42
mov rdi, rax
call double ; rax = 84
mov rdi, rax
mov rax, 60 ; exit code 84
syscall
How it works
double:is a label like any other — call makes it a function.lea rax, [rdi * 2]computes without touching flags.- Two calls chain: 21 to 42 to 84.
Keywords and builtins used here
callgloballeamovraxrdiretsectionsyscall
The run, in numbers
- Lines
- 17
- Characters to type
- 330
- Tokens
- 41
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 41 seconds.
Step 2 of 3 in The stack, step 20 of 31 in Language basics.