Upload
muhammadhumza
View
216
Download
1
Embed Size (px)
DESCRIPTION
MIPS function believe me its awesome and free
Citation preview
04/22/20231
Instruction Set Architecture
Procedures
2 04/22/2023
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 Steps1) Place parameters when procedure can access them2) Transfer control to the procedure3) Obtain storage needed for the procedure4) Execute the desired task5) Make results available to the calling program6) Return control to the point of origin
3 04/22/2023
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)
4 04/22/2023
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
5 04/22/2023
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
6 04/22/2023
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 $sp $sp -12 sw $s0, 8($sp) sw $t0, 4($sp) sw $t1, 0($sp)
7 04/22/2023
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 $s0, 8($sp) lw $t0, 4($sp) lw $t1, 0($sp) addi $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.
8 04/22/2023
Procedureint 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
04/22/20239
ProcedureLeaf_example:
addi $sp, $sp, -12
sw $t1, 8($sp)
sw $t0, 4($sp)
sw $s0, 0($sp)
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)
lw $t1, 8($sp)
addi $sp, $sp, 12
jr $ra
Adjusting Stack
Adjusting Stack to Restore
f, g, h, i, j = $s0, $a0, $a1, $a2, $a3
10 04/22/2023
Saving & Restoring $t Registers Temporary registers $t0 - $t9
Temporary contents Not saved Not restored
sw and lw instructions not required sw $t1, 8($sp) & sw $t0, 4($sp) lw $t1, 8($sp) & lw $t0, 4($sp)
04/22/2023
Example
11
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
04/22/2023
Example
12
.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)
13 04/22/2023
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 Argument, temporary, save registers Return address
14 04/22/2023
Recursive Procedure int fact (int n)
{if (n < 1) return (1);
else return (n * fact(n-1));}
$a0 = n
15 02/03/05
Recursive Procedurefact: 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
16 04/22/2023
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).
04/22/202317
Byte Halfword Word
Registers
Memory
Memory
Word
Memory
Word
Register
Register
1. Immediate addressing
2. Register addressing
3. Base addressing
4. PC-relative addressing
5. Pseudodirect addressing
op rs rt
op rs rt
op rs rt
op
op
rs rt
Address
Address
Address
rd . . . funct
Immediate
PC
PC
+
+
Addressing Modes: Summary