Title: CSC3420 Tutorial 3
1CSC3420Tutorial 3
- Assembly Language Procedure Call
Ricky Wong 3 / 5 Feb 2009
2Contents
- Introduction
- Version 1 Using registers for passing arguments
and storing returned values - Version 2 Using stack for passing arguments and
storing returned values
3MIPS Assembly Procedure Call
- Why do we need procedure call?
- More structural
- Easier to understand
- Reuse of code
- Recursive structure
4Basic example
s1 4 a1 (for integer array address)
- swap the 1st element and 2nd element of the
array - .data
- array .word 5, 4, 3, 2, 1
- .text
- .globl main
- main
- la a0, array
- addi a1, 0, 1
- jal swap
- exit
- swap add s1, a1, a1
- add s1, s1, s1
- add s1, a0, s1
- lw s0, 0(s1)
- lw s2, 4(s1)
- sw s2, 0(s1)
- sw s0, 4(s1)
- jr ra
s1 the address of 1st element of the array
5MIPS Assembly Procedure Call
caller
callee
jal loc
ra
loc
jr ra
What if the callee calls another procedure?
6MIPS Assembly Procedure Call
caller
callee caller
another callee
7MIPS Assembly Procedure Call
- In MIPS a temporary variable is implemented by
use of a register - this is the only resource to play with!
- But a register used in this procedure might
also be used by that procedure - A register used in this procedure might have been
used in the main program or calling procedure. - Housekeeping is required.
8Stacking of Subroutine Calls Returns and
Environments
A
A CALL B CALL C
C RET
RET
B
A
B
A
B
C
A
B
A
Each procedure invocation makes use of the stack,
and during its lifetime owns its part of the
stack (stack frame).
9Stack
- Last-In-First-Out (LIFO) data structure
- push puts an item into the stack
- pop gets the item at the top of the stack
- MIPS stack
- push
- addi sp, sp, -4
- sw t0, 0(sp)
- pop
- lw t0, 0(sp)
- add sp, sp, 4
High Memory Address
8(sp)
4(sp)
0(sp)
Low Memory Address
10Assembly Procedure Call
- Six steps in procedure call
- Place the parameter in a place where the
procedure can access it - Transfer the control to the procedure
- Acquire the storage resources needed for the
procedure - Perform the procedure task
- Place the result value in a place where the
calling program can access it - Return control to the point of origin
11MIPS Assembly Procedure Call
- Six steps in procedure call
- Pass parameters to callee
- Store arguments into a0 -- a3 or stack
- Transfer control to the procedure
- jal (ProcName)
- Allocate storage resources
- Local variables and saved registers, stack
- Perform the desired task
- Return value
- Store the return values into v0, v1 or stack
- Transfer control to the caller
- jr ra
12Version 1
- Version 1 Using registers for passing arguments
and storing returned values. (Uses stack for
storing others) - Fewer memory access
- Limited number of arguments (a0-a3)
13Version 1
- In assembly view, the six steps become
- Caller
- Store the arguments into a0 - a3
- Execute a jal instruction
- Callee
- Establish the stack pointer
- Allocate memory for the frame
- Save registers
14Version 1
- Callee (on return)
- Place the return values in v0-v1
- Restore registers
- Subtract the stack frame size
- Jump to the address in ra
- Caller (after return)
- Get the return values from v0-v1
15Version 1 example
void main() int x 10 x x
leaf(4,3,2,1) x x1
- int leaf(int g, int h, int i, int j)
- int f
- f (g h) (i j)
- return f
-
- g, h, i, j a0, a1, a2, a3 f v0
- compiles to
16Version 1 example
- main addi t0, 0, 10 t0 is x, initialize x
10 -
- addi a0, 0, 4 1st argument 4
- addi a1, 0, 3 2nd argument 3
- addi a2, 0, 2 3rd argument 2
- addi a3, 0, 1 4th argument 1
- jal leaf
- add t0, t0, v0 x x leaf(4,3,2,1)
- addi t0, t0, 1 x x 1
-
-
void main() int x 10 x x
leaf(4,3,2,1) x x1 x t0
17Version 1 example
- leaf addi sp, sp, -12 adjust stack for 3
items - sw t1, 8(sp) push t1 on stack
- sw t0, 4(sp) push t0 on stack
- sw s0, 0(sp) push s0 on stack
-
- add t0, a0, a1 g h
- add t1, a2, a3 i j
- sub s0, t0, t1 f (g h) (i j)
- add v0, s0, zero copy f to return register
- lw s0, 0(sp) pop s0 from stack
- lw t0, 4(sp) pop t0 from stack
- lw t1, 8(sp) pop t1 from stack
- add sp, sp, 12 adjust stack for 3 items
- jr ra return to caller
int leaf(int g, int h, int i, int j) int
f f (g h) (i j) return
f g, h, i, j a0, a1, a2, a3 f s0
18Version 2
- Version 2 Using stack for passing arguments and
storing returned values (Uses stack for storing
others) - More memory access
- Unlimited number of arguments
19Version 2
- In assembly view, the six steps become
- Caller
- Store the arguments into stack
- Execute a jal instruction
- Callee
- Establish the stack pointer
- Allocate memory for the frame
- Save registers
- Establish the frame pointer
20Version 2
- Callee (on return)
- Place the return values in stack
- Restore registers
- Subtract the stack frame size
- Jump to the address in ra
- Caller (after return)
- Get the return values from stack
21Version 2
int funct(int a, int b) int x, y, z
return x
void main() N funct(P, Q)
22Version 2
fp
sp
Main N funct(P, Q)
23Version 2
fp
Main N funct(P, Q)
sp
24Version 2
- Before executing procedure body
int funct(int a, int b) int x, y, z
return x
fp
sp
25Version 2
- Before returning to caller
fp
int funct(int a, int b) int x, y, z
return x
sp
26Version 2
fp
sp
Main N funct(P, Q)
27Version 2 example(same as version 1 example)
void main() int x 10 x x
leaf(4,3,2,1) x x1
- int leaf(int g, int h, int i, int j)
- int f
- f (g h) (i j)
- return f
-
- g, h, i, j a0, a1, a2, a3 f v0
- compiles to
28Version 2 example
void main() int x 10 x x
leaf(4,3,2,1) x x1 x t0
- main addi t0, 0, 10 t0 is x, initialize x
10 -
- addi sp, sp, -20
- addi t1, 0, 4 1st argument 4
- sw t1, 16(sp) store 1st argument into
stack - addi t1, 0, 3 2nd argument 3
- sw t1, 12(sp) store 2nd argument into
stack - addi t1, 0, 2 3rd argument 2
- sw t1, 8(sp) store 3rd argument into
stack - addi t1, 0, 1 4th argument 1
- sw t1, 4(sp) store 4th argument into
stack -
- jal leaf
- lw t1, 0(sp) t1 leaf(4,3,2,1)
- add t0, t0, t1 x x t1
- addi t0, t0, 1 x x 1
-
-
29Version 2 example
int leaf(int g, int h, int i, int j) int
f f (g h) (i j) return
f g, h, i, j a0, a1, a2, a3 f s0
- leaf addi sp, sp, -16 adjust stack for 4
items - sw t1, 8(sp) push t1 on stack
- sw t0, 4(sp) push t0 on stack
- sw s0, 0(sp) push s0 on stack
- sw fp, 12(sp) store frame pointer
- addi fp, sp, 12 update frame pointer to
current frame -
- lw a0, 20(fp) load 1st argument from stack
- lw a1, 16(fp) load 2nd argument from stack
- lw a2, 12(fp) load 3rd argument from stack
- lw a3, 8(fp) load 4th argument from stack
- add t0, a0, a1 g h
- add t1, a2, a3 i j
- sub s0, t0, t1 f (g h) (i j)
- sw s0, 4(fp) copy f to the stack for
return - lw fp, 12(sp) store frame pointer
30Summery
Callers responsibility Place arguments where
procedure can access them (a0..a3, or
stack) Transfer control Callees
responsibility Do the work, using the arguments
in a0..a3 or stack Put return values in where
caller can access it (v0, v1 or stack) Return
control to the calling procedure
31Rule of thumb
- After finishing the procedure call, the stack
should be restored as before calling the
procedure.
32Book example
- int fact (int n)
- If (nlt1) return (1)
- Else return (nfact(n-1))
-
- Answer section 2.7 of your book
33References
- Section 2.7 of your book
- Csci 136 Computer Architecture
- http//www.seas.gwu.edu/cheng/136/LecNotes/Proced
ureCall.ppt
34End
35MIPS register convention
- a0 - a3 (arguments registers)
- v0 - v1 (return value registers)
- t0 - t9 (caller saved registers)
- s0 - s7 (callee saved registers)
- ra (return address register)
36Example (last year)
int sum(int n, int a) int s, i s
0 for (i0 iltn i) s
ai return s
int s int a10 int n10 Main s
sum(n, a)
37main subiu sp, sp, 8 sw ra,
0(sp) sw fp, 4(sp) addiu fp, sp,
4 la s0, n lw s0, 0(s0) la s1,
number la s2, s subiu sp, sp, 12 sw s0,
4(sp) sw s1, 8(sp) jal sum lw s3,
0(sp) sw s3, 0(s2) addiu sp, sp,
12 lw fp, 4(sp) lw ra, 0(sp) addiu sp,
sp, 8 jr ra n .word 10 number .word 34 35
24356 123 s .word 0
High Memory Address
fp
sp
Low Memory Address
38main subiu sp, sp, 8 sw ra,
0(sp) sw fp, 4(sp) addiu fp, sp,
4 la s0, n lw s0, 0(s0) la s1,
number la s2, s subiu sp, sp, 12 sw s0,
4(sp) sw s1, 8(sp) jal sum lw s3,
0(sp) sw s3, 0(s2) addiu sp, sp,
12 lw fp, 4(sp) lw ra, 0(sp) addiu sp,
sp, 8 jr ra n .word 10 number .word 34 35
24356 123 s .word 0
High Memory Address
fp
sp
Low Memory Address
39sum subiu sp, sp, 16 sw ra,
8(sp) sw fp, 12(sp) addiu fp, sp,
12 lw a0, 8(fp) lw a1, 12(fp) add t0,
zero, zero add t1, zero, zero sw t0,
4(sp) sw t1, 0(sp) for bge t0, a0,
endfor lw t2, 0(a1) add t1, t1,
t2 sw t1, 0(sp) addi a1, a1,
4 addi t0, t0, 1 sw t0, 4(sp) j for sw
t1, 4(fp) lw fp, 12(sp) lw ra,
8(sp) addiu sp, sp, 16 jr ra
High Memory Address
fp
sp
Low Memory Address
40sum subiu sp, sp, 16 sw ra,
8(sp) sw fp, 12(sp) addiu fp, sp,
12 lw a0, 8(fp) lw a1, 12(fp) add t0,
zero, zero add t1, zero, zero sw t0,
4(sp) sw t1, 0(sp) for bge t0, a0,
endfor lw t2, 0(a1) add t1, t1,
t2 sw t1, 0(sp) addi a1, a1,
4 addi t0, t0, 1 sw t0, 4(sp) j for end
for sw t1, 4(fp) lw fp, 12(sp) lw ra,
8(sp) addiu sp, sp, 16 jr ra
High Memory Address
fp
sp
Low Memory Address
41main subiu sp, sp, 8 sw ra,
0(sp) sw fp, 4(sp) addiu fp, sp,
4 la s0, n lw s0, 0(s0) la s1,
number la s2, s subiu sp, sp, 12 sw s0,
4(sp) sw s1, 8(sp) jal sum lw s3,
0(sp) sw s3, 0(s2) addiu sp, sp,
12 lw fp, 4(sp) lw ra, 0(sp) addiu sp,
sp, 8 jr ra n .word 10 number .word 34 35
24356 123 s .word 0
High Memory Address
fp
sp
Low Memory Address
42main subiu sp, sp, 8 sw ra,
0(sp) sw fp, 4(sp) addiu fp, sp,
4 la s0, n lw s0, 0(s0) la s1,
number la s2, s subiu sp, sp, 12 sw s0,
4(sp) sw s1, 8(sp) jal sum lw s3,
0(sp) sw s3, 0(s2) addiu sp, sp,
12 lw fp, 4(sp) lw ra, 0(sp) addiu sp,
sp, 8 jr ra n .word 10 number .word 34 35
24356 123 s .word 0
High Memory Address
fp
sp
Low Memory Address