Function Calls
Course: Computer Architectures
Type: Concept
ra
ra is the ABI name for:
x1It is conventionally used to hold a return address.
jal
jal ra, functiondoes two things:
- saves the return address in
ra - jumps to
function
call
call sub1means “call the subroutine named sub1.”
sub1 is a label, not an instruction.
ret
retreturns using the address stored in ra.
Conceptually:
jalr x0, 0(ra)Nested calls
If function A calls function B, B’s call can overwrite ra.
So A saves its own return address first:
funcA:
addi sp, sp, -4
sw ra, 0(sp)
jal ra, funcB
lw ra, 0(sp)
addi sp, sp, 4
retRegister roles
Useful ABI conventions:
ra -> return address
sp -> stack pointer
t0-t6 -> temporary values
s0-s11 -> saved values
a0-a7 -> arguments and return values