Title: Procedures and Addressing Modes
1- Procedures and Addressing Modes
2Procedures
- Place parameters in a place where the called
procedure can access them. - Transfer control to the procedure (keep track of
where to continue when it returns). - Acquire the storage resources needed by the
procedure. - Perform the desired task.
- Place the result value in a place where the
calling program can access it. - Return control to point of origin.
3 A Leaf Procedure
Remember that MIPS conventions mandate that the
callee preserve s0-s7.
leaf addi sp,sp,-12 sw t1,8(sp) sw
t0,4(sp) sw s0,0(sp) add t0,a0,a1 add
t1,a2,a3 sub s0,t0,t1 add v0,s0,zero lw
s0,0(sp) lw t0,4(sp) lw t1,8(sp) addi
sp,sp,12 jr ra
caller
int x leaf(1,2,3,4)
callee
- int leaf(int g,int h,
- int i, int j)
- int f
- f (gh)-(ij)
- return f
4The Stack in a Leaf Procedure
5Nested Procedures
caller
int x p1(1,2,3,4)
callee caller
int p1(int a, int b, int c, int d)
int r p2(a,b,c,d)
callee
- int p2(int g,int h,
- int i, int j)
- int f
- f (gh)-(ij)
- return f
6Nested Procedures
- Question What are the added complications in
generating assembly code when we deal with
procedures that call other procedures?
7Nested Procedures
- int fact(int n)
- if (n lt 1)
- return n
- else
- return (n fact(n-1))
fact addi sp,sp,-8 sw ra,4(sp) sw a0,0(sp
) slti t0,a0,1 beq t0,zero,L1 addi v0,zero,1
addi sp,sp,8 L1 addi a0,0(sp) jal fact lw
a0,0(sp) lw ra,4(sp) addi sp,sp,8 mul v0,
a0,v0 jr rah
Question How can a procedure access global
variables?
8Allocation on the Stack
procedure frame or activation record
9Allocation on the Heap
gp Pointer to static variables. fp Pointer to
the first word of the frame of a procedure. It is
used as the base for access to saved registers
and local variables. It remains fixed throughout
the procedure. sp Pointer to the top of the
stack.
Questions What is a memory leak? Does it happen
in Java? What is garbage collection?
10MIPS Addressing 32-bit immediates
- In immediate addressing, the operand is a
constant within the instruction itself. - I-format instructions allocate space for 16-bit
immediates.
Question How can MIPS programs work with
immediates that are longer than 16 bits, say,
when they are 32-bits long?
11MIPS Register Addressing
- In register addressing, the operand is a
register. - R-format instructions use register addressing.
12MIPS Base Addressing
- In base or displacement addressing, the operand
is the memory location whose address is the sum
of a register and a constant in the instruction.
13MIPS Addressing in Jumps(Pseudodirect addressing)
J-format
6 bits
26 bits
The address in these instructions is a long
address. The program could be jumping to any
location in memory. Remember, however, that the
pc is 32-bits long.
Question Where do the remaining 4 bits come from?
14MIPS Addressing in Branches(PC-relative
addressing)
I-format
op
rs
rt
address
6 bits
5 bits
5 bits
16 bits
The address in these instructions is a short
address. The program can branch to a location in
memory somewhat near that of the current pc.
15MIPS Addressing