A portion of code within larger program, typically called:
procedures or subroutines in imperative languages like C
methods in OO languages like Java
and functions in functional languages such as Haskell
Functions usually return a value; procedures don’t
Procedures can call other procedures; even themselves
What happens when we call a procedure?
Control hands over to the callee; the caller is suspended
Callee performs requested task
Callee returns control to the caller
Calling Procedures in MIPS Assembly
jal label – jump and link
$ra := PC + 4; PC := [label]-----??????????
Calls procedure at address label
jr src – jump register
PC := src
Issuing j $ra is the assembly equivalent of return
Register $ra contains the return address of the caller
Arguments are passed in registers $a0 to $a3
Results are left in registers $v0 and $v1
The Stack
The stack provides last-in, first out (LIFO) storage
Register $sp points to the topmost word on the stack
By convention, the stack grows downwards
Placing words onto the stack is termed pushing
Taking words off the stack is called popping
Calling Convention
Caller
Push any of $a0-3, $v0-1 and $t0-9 needed later
Place arguments in $a0 to $a3, and stack if necessary
Make the call using jal callee; result in $v0 and $v1
Pop saved registers and/or extra arguments off stack
Callee
Push any of $ra, $s0-$s1 that may be overwritten
Perform desired task; place result in $v0 and $v1
Pop above registers off the stack
Return to caller with jr $ra
The main label is just another procedure
Local Variables
Solution: save initial value of $sp in $fp //---------?????????
Hence local variables always have the same $fp-offset
Note callee must preserve previous value of $fp!
Monday, 14 May 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment