0% found this document useful (0 votes)
92 views19 pages

Procedure Calls

This document discusses procedure calls in MIPS assembly language. It defines key terms like caller and callee. It explains how procedures pass parameters and return values. It discusses the stack and how procedures use it to preserve registers. It also covers nested and recursive procedure calls, and PC-relative addressing used in conditional branches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views19 pages

Procedure Calls

This document discusses procedure calls in MIPS assembly language. It defines key terms like caller and callee. It explains how procedures pass parameters and return values. It discusses the stack and how procedures use it to preserve registers. It also covers nested and recursive procedure calls, and PC-relative addressing used in conditional branches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Facultad de Ingeniería

Departamento de Electrónica y Ciencias de la Computación

Carrera de Ingeniería Electrónica


Arquitectura de Computadores

Procedure Calls
Agosto 2013

Tek
Procedure Calls

Definitions

• Caller: calling procedure (in this case, main)


• Callee: called procedure (in this case, sum)

void main ( void ) int sum ( int a, int b )


{ {
int y; return ( a + b );
}
y = sum ( 42, 7 );
...
}
Procedure Calls

Caller/Callee Duties

• Caller: • Callee:
– passes arguments to – performs the
callee. procedure
– jumps to the callee – returns the result to
caller
– returns to the point
of call
– must not overwrite
registers or memory
needed by the caller

void main ( void ) int sum ( int a, int b )


{ {
int y; return ( a + b );
}
y = sum ( 3, 6 );
...
}
Procedure Calls

Procedure Call and Return

• Calling a procedure requires transferring execution to a different part of


the code… in other words, a branch or jump operation:

jal   <address>  # $ra = PC + 4
                 # PC = <address>

• MIPS reserves register $31, a.k.a. $ra, to store the return address.

• The called procedure must place the return value (if any) somewhere
from which the caller can retrieve it. The convention is that registers
$v0 and $v1 can be used to hold the return value. We will discuss what
to do if the return value exceeds 8 bytes later…

• Returning from the procedure requires transferring execution to the


return address the jal instruction placed in $ra:

jr   $ra         # PC = $ra
Procedure Calls
Passing Parameters

• In most cases, passing parameters is straightforward, following the MIPS


convention:

$a0     # 1st parameter
$a1     # 2nd parameter
$a2     # 3rd parameter
$a3     # 4th parameter

• The called procedure can then access the parameters by following the same
convention.

• What if a parameter needs to be passed by reference? Simply place the


address of the relevant data object in the appropriate register, and design the
called procedure to treat that register value accordingly.

• What if a parameter is smaller than a word? Clever register manipulation in


the callee.

• What if there are more than four parameters? We'll discuss that later…
Procedure Calls

Input Arguments and Return Values


# $s0 = y
int main ( void ) main:
{
int y;
: :
y = sum ( 3, 6 ); addi $a0, $0, 3 # argument 0 = 2
: addi $a1, $0, 6 # argument 1 = 3
jal sum # call procedure
} add $s0, $v0, $0 # y = returned value
:
# $s0 = result
int sum ( int a, int b ) sum:
{
int result;
result = f + g; add $s0, $a0, $a1 # $s0 = a + b
add $v0, $s0, $0 # return value in $v0
return ( result ); jr $ra # return to caller
}

• sum overwrote 1 register: $s0


• sum can use the stack to temporarily store registers
Procedure Calls
The Stack

• Grows down (from higher to lower memory addresses)


• Stack pointer: $sp, points to Top Of the Stack (TOS)

Address Data Address Data

7FFFFFFC 12345678 $sp 7FFFFFFC 12345678


7FFFFFF8 7FFFFFF8 AABBCCDD
7FFFFFF4 7FFFFFF4 11223344 $sp
7FFFFFF0 7FFFFFF0
Procedure Calls

How Procedures use the Stack


# $s0 = result # $s0 = result
sum: sum:

addi $sp, $sp, -4 # make space on stack


# to store 1 registers
sw $s0, 0($sp) # save $s0 on stack
add $s0, $a0, $a1 add $s0, $a0, $a1 # $s0 = a + b
add $v0, $s0, $0 add $v0, $s0, $0 # return value in $v0
lw $s0, 0($sp) # restore $s0 from stack
addi $sp, $sp, 4 # deallocate stack space
jr $ra jr $ra # return to caller

• Called procedures must have no other unintended side effects:


– sum preserves register $s0 on stack before using it and restores its
previous value before returning
Procedure Calls

Functions with up to four arguments


int diffofsums ( int a, int b, int c, int d )
{
int result;
result = (a + b) - (c + d);
return ( result );
}

# $s0 = result
diffofsums:
addi $sp, $sp, -12 # make space on stack to store 3 registers
sw $s0, 8($sp) # save $s0 on stack
sw $t0, 4($sp) # save $t0 on stack
sw $t1, 0($sp) # save $t1 on stack
add $t0, $a0, $a1 # $t0 = a + b
add $t1, $a2, $a3 # $t1 = c + d
sub $s0, $t0, $t1 # result = (a + b) - (c + d)
add $v0, $s0, $0 # put return value in $v0
lw $t1, 0($sp) # restore $t1 from stack
lw $t0, 4($sp) # restore $t0 from stack
lw $s0, 8($sp) # restore $s0 from stack
addi $sp, $sp, 12 # deallocate stack space
jr $ra # return to caller
Procedure Calls
Registers Usage Convention

Preserved Nonpreserved
Callee-Saved Caller-Saved
$s0 - $s7 $t0 - $t9

$ra $a0 - $a3

$sp $v0 - $v1

stack above $sp stack below $sp


Procedure Calls

Functions with up to four arguments (revisited)


int diffofsums ( int a, int b, int c, int d )
{
int result;
result = (a + b) - (c + d);
return ( result );
}

# $s0 = result
diffofsums:
addi $sp, $sp, -4 # make space on stack to store 1 register
sw $s0, 0($sp) # save $s0 on stack
# # no need to save $t0 or $t1...
add $t0, $a0, $a1 # $t0 = a + b
add $t1, $a2, $a3 # $t1 = c + d
sub $s0, $t0, $t1 # result = (a + b) - (c + d)
add $v0, $s0, $0 # put return value in $v0
lw $s0, 0($sp) # restore $s0 from stack
addi $sp, $sp, 4 # deallocate stack space
jr $ra # return to caller
Procedure Calls

Nested Procedure Calls


void proc1 ( void )
{
:
proc2 ();
:
return ( result );
}

proc1:
:
addi $sp, $sp, -4 # make space on stack
sw $ra, 0($sp) # save $ra on stack
jal proc2
:
lw $ra, 0($sp) # restore $s0 from stack
addi $sp, $sp, 4 # deallocate stack space
jr $ra # return to caller
Procedure Calls

Recursive Procedure Call


int factorial ( int n )
{
if ( n == 1 )
return ( 1 );
else
return ( n * factorial (n-1) );
}

factorial:
addi $sp, $sp, -8 # make room
sw $a0, 4($sp) # store $a0
sw $ra, 0($sp) # store $ra
addi $t0, $0, 2
slt $t0, $a0, $t0 # n == 1 ?
beq $t0, $0, else # no: go to else
addi $v0, $0, 1 # yes: return 1
addi $sp, $sp, 8 # restore $sp
jr $ra # return
else:
addi $a0, $a0, -1 # n = n - 1
jal factorial # recursive call
lw $ra, 0($sp) # restore $ra
lw $a0, 4($sp) # restore $a0
addi $sp, $sp, 8 # restore $sp
mul $v0, $a0, $v0 # n * factorial(n-1)
jr $ra # return
Procedure Calls
PC-Relative Addressing

• Conditional branch instructions use PC-relative addressing to specify


the new value of the PC if the branch is taken.
– The signed offset in the immediate field is added to the PC to obtain the new
PC; hence, the branch destination address is said to be relative to the current
PC:
• imm16 = ( BTA – (PC + 4) ) >> 2

:
0x00400024 beq $t0, $0, else
0x00400028 addi $v0, $0, 1
0x0040002C addi $sp, $sp, i
0x00400030 jr $ra
0x00400034 else: addi $a0, $a0, -1 # BTA (Branch Target Address)
0x00400038 jal factorial
:

Assembly Code Field Values


op rs rt imm

beq $t0, $0, else 4 8 0 3


(beq $t0, $0, 3) 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
Procedure Calls
Pseudo-direct Addressing

• In direct addressing, an address is specified in the instruction. The jump


instructions, j and jal, ideally would use direct addressing to specify a 32-bit
target address to indicate the instruction address to execute next.
:
0x0040005C jal sum
:
0x004000A0 sum: add $v0, $a0, $a1 # JTA (Jump Target Address)
:

• 26 bits are used to encode the target address and the two least significant bits,
should always be 0, because instructions are word aligned:

JTA 0000 0000 0100 0000 0000 0000 1010 0000 (0x004000A0)
26-bit addr 0000 0000 0100 0000 0000 0000 1010 0000 (0x0100028)
0 1 0 0 0 2 8

Field Values Machine Code


op imm op addr

3 0x0100028 000011 00 0001 0000 0000 0000 0010 1000 (0x0C100028)


6 bits 26 bits 6 bits 26 bits
Procedure Calls
Memory Addressing
Procedure Calls
Additional Arguments and Local Variables
• Procedures may have more than four input arguments and local variables.
– the first four arguments are passed in the $a0­$a3
– aditional arguments are passed on the stack, just above $sp.
• The caller must expand its stack to make room for the additional arguments.

• A procedure can also declare local variables or arrays.


– local variables are stored in $s0–$s7
– if there are too many local variables, they can also be stored in the
procedure’s stack frame.
• The frame holds the procedure’s own arguments (if it calls other procedures), the
return address, and any of the saved registers that the procedure will modify. It
also holds local arrays and any excess local variables.
Procedure Calls

The Frame Pointer


• The frame pointer register, $fp, is intended as a "bookmark" to keep
track of where the stack pointer was when a procedure was entered:

proc:
add $fp, $sp, $0 # $fp points to original top of stack

sub $sp, $sp, $a0 # need a local array of size $a0


add $t0, $sp, $0 # $t0 points to the array on the stack

## do stuff with the array; mess with $a0 all you like

add $sp, $fp, $0 # restore stack pointer to original value


jr $ra # right before you return
Further Reading

• PATTERSON, David A.; HENNESSY, John L. Computer


Organization and Design: The Hardware/Software Interface.
Fourth Edition, Revised Printing. Morgan Kaufmann, 2011.
Chapter 2.

Some of this material has been adapted from slides prepared by


Jason Bakos, University of South Carolina; William D McQuain,
Virginia Tech

You might also like