0% found this document useful (0 votes)
88 views17 pages

COAL FALL 15 Lecture 11 and 12 Procedure Call

The document discusses procedures in computer architecture and the MIPS instruction set. Procedures break applications into modules and require passing arguments, allocating storage for local variables, and returning control. In MIPS, arguments are passed in registers $a0-$a3, return values in $v0-$v1, and the return address in $ra. The jal instruction jumps to a procedure and saves the return address, while jr $ra returns from the procedure. Procedures use a stack to temporarily store registers and return addresses. MIPS lacks push/pop, so the stack pointer $sp is manually adjusted to allocate and access stack frames.

Uploaded by

MuhammadHumza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views17 pages

COAL FALL 15 Lecture 11 and 12 Procedure Call

The document discusses procedures in computer architecture and the MIPS instruction set. Procedures break applications into modules and require passing arguments, allocating storage for local variables, and returning control. In MIPS, arguments are passed in registers $a0-$a3, return values in $v0-$v1, and the return address in $ra. The jal instruction jumps to a procedure and saves the return address, while jr $ra returns from the procedure. Procedures use a stack to temporarily store registers and return addresses. MIPS lacks push/pop, so the stack pointer $sp is manually adjusted to allocate and access stack frames.

Uploaded by

MuhammadHumza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Instruction Set Architecture

Procedures

12/10/15

Supporting Procedures

Procedures help decomposing applications into smaller


modules
Assembly language procedures require
Storage for local variable
Arguments must be passed in and return values passed out
Execution must continue after the call

Procedure Steps
1)
2)
3)
4)
5)
6)
2

Place parameters when procedure can access them


Transfer control to the procedure
Obtain storage needed for the procedure
Execute the desired task
Make results available to the calling program
Return control to the point of origin
12/10/15

Supporting Procedures: MIPS registers

Registers are the fastest place to hold data in a computer


MIPS registers to support procedure calling

$a0 - $a3 (Argument registers to pass parameters. At


the most four function arguments can be
passed)

$v0 - $v1 (Value registers to return values. At the most


two values can be returned)

$ra

(Return address register to return to the point


of origin)

12/10/15

Supporting Procedures: MIPS instructions

jal ProcedureAddress ( jump-and-link )


Jumps to an address and simultaneously saves the address
of the following instruction in register $ra
The link stored in register $ra is called the return address
The jal instruction saves PC+4 in register $ra to link to the
instruction that follows to set up the procedure return
jr $ra jumps to the address stored in the register $ra
The calling program, or caller, puts the parameter values in
$a0 - $a3, and uses jal X to jump to procedure X
(sometimes named the callee )
The callee then

performs the calculations

places the results in $v0 - $v1, and

returns control to the caller using jr $ra


12/10/15

Procedures and Stack

Stack is a LIFO data structure. A natural way of


temporarily store data for procedures as well as
call/return information
Stacks can grow up or down
Stack operations: push, pop
MIPS stack

The $sp register should always point to the top of


the stack
Stack grows from higher addresses to lower
addresses
MIPS does not provide push and pop instructions.
They both have to be implemented by the
programmer
12/10/15

Procedures and Stack: Push

To push data onto the stack

Make room for the data by moving $sp down.


Allocate storage.
Place data into the allocated storage.
Example: Push registers $s0, $t0 and $t1 onto the
stack.
MIPS Code:

addi
sw
sw
sw

$sp $sp -12


$s0, 8($sp)
$t0, 4($sp)
$t1, 0($sp)

12/10/15

Procedures and Stack: Pop

To pop data from the stack

Any stored data may be accessed by its position


relative to $sp
Example: Retrieve contents of registers $s0, $t0
and $t1 from stack
MIPS Code:

lw
lw
lw
addi

$s0, 8($sp)
$t0, 4($sp)
$t1, 0($sp)
$sp $sp +12

As you can see it is possible to access any data that


has been placed on the stack through the $sp
register.
12/10/15

Procedure
int leaf_example (int g, int h, int i, int j)
{
int f;
f = (g + h) (i + j);
return f;
}
f, g, h, i, j = $s0, $a0, $a1, $a2, $a3

12/10/15

Procedure
Leaf_example:
addi $sp, $sp, -12
sw $t1, 8($sp)
Adjusting Stack
sw $t0, 4($sp)
sw $s0, 0($sp)

f, g, h, i, j = $s0, $a0, $a1,


$a2, $a3

add $t0, $a0, $a1


g+h
add $t1, $a2, $a3
i+j
sub $s0, $t0, $t1
(g+h)-(i+j)
add $v0, $s0, $zero returns f
lw $s0, 0($sp)
lw $t0, 4($sp)
Adjusting Stack to Restore
lw $t1, 8($sp)
addi $sp, $sp, 12
jr $ra
9

12/10/15

Saving & Restoring $t Registers

Temporary registers $t0 - $t9

Temporary contents
Not saved
Not restored

sw and lw instructions not required

10

sw $t1, 8($sp) & sw $t0, 4($sp)


lw $t1, 8($sp) & lw $t0, 4($sp)

12/10/15

Example
int main()
{
x=addthem(a,b);
}
int addthem(int a, int b)
{
return a+b;
}
#assume value a is already in $t0, b in $t1
11

12/10/15

Example
.text
main: #assume value a is already in $t0, b in $t1
add $a0,$0,$t0 # it's the same function as move the value
add $a1,$0,$t1
jal addthem # call procedure
add $t3,$0,$v0 # move the return value from $v0 to where
#we want
syscall
addthem:
addi $sp,$sp,-4 # Moving Stack pointer
sw $t0, 0($sp) # Store previous value
add $t0,$a0,$a1 # Procedure Body
add $v0,$0,$t0 # Result
lw $t0, 0($sp) # Load previous value
addi $sp,$sp,4 # Moving Stack pointer
jr $ra # return (Copy $ra to PC)
12

12/10/15

Nested & Recursive Procedures

Procedure A calls Procedure B (nested)


Procedure A calls Procedure A (recursive)

Passing arguments through identical registers


Using same registers
Over writing return address

Push on to stack

13

Argument, temporary, save registers


Return address

12/10/15

Recursive Procedure

int fact (int n)


{
if (n < 1) return (1);
else return (n * fact(n-1));
}
$a0 = n

14

12/10/15

Recursive Procedure
fact:
sub $sp, $sp, 8
sw $ra, 4($sp)
sw $a0, 0($sp)
slt $t0, $a0, 1
beq $t0, $zero, L1
add $v0, $zero, 1
add $sp, $sp, 8
jr $ra

L1:

sub $a0, $a0, 1


jal fact
lw $a0, 0($sp)
lw $ra, 4($sp)
add $sp, $sp, 8
mult $v0, $a0, $v0
jr $ra

15

02/03/05

Addressing Modes

Register addressing (R-Type)


Operands must be in registers
Example: add $rd, $rs, $rt
Base addressing (I-Type)
Operand determined as sum of base register plus displacement.
Example: lw $rd, displacement($base_register)
Immediate addressing (I-Type)
Operand is literal value within instruction.
Example: addi $a0, $zero, 1
PC-relative addressing (I-Type)
Address determined by adding displacement to program counter
Pseudo addressing (J-Type)
Address determined by concatenating upper bits of PC with bits
of address. The jal instruction uses J-Type format( Procedure
code is commonly located further from current instruction.
Branches typically implement if-else or loop structure).
16

12/10/15

Addressing Modes: Summary


1. Immediate addressing
op

rs

rt

Immediate

2. Register addressing
op

rs

rt

rd

...

funct

Registers
Register

3. Base addressing
op

rs

rt

Memory

Address

Register

Byte

Halfword

Word

4. PC-relative addressing
op

rs

rt

Memory

Address

PC

Word

5. Pseudodirect addressing
op

Address

PC

17

Memory

Word

12/10/15

You might also like