Function Calls

Course: Computer Architectures
Type: Concept

ra

ra is the ABI name for:

x1

It is conventionally used to hold a return address.

jal

jal ra, function

does two things:

  1. saves the return address in ra
  2. jumps to function

call

call sub1

means “call the subroutine named sub1.”

sub1 is a label, not an instruction.

ret

ret

returns 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
    ret

Register roles

Useful ABI conventions:

ra      -> return address
sp      -> stack pointer
t0-t6   -> temporary values
s0-s11  -> saved values
a0-a7   -> arguments and return values