Professional Documents
Culture Documents
Review
Instruction
Meaning
add $s1,$s2,$s3
sub $s1,$s2,$s3
addi $s1,$s2,4
ori $s1,$s2,4
$s1 = $s2 + 4
$s2 = $s2 | 4
lw $s1,100($s2)
sw $s1,100($s2)
$s1 = Memory[$s2+100]
Memory[$s2+100] = $s1
Pseudo-instructions
r The MIPS assembler supports several pseudo-
instructions
m Programmers
is translated into
add $7, $18, $0
r
Other Examples:
m
r Usage
m Switch/Case statements
m Returning from procedures/functions
}
Assume
1.
f, g, h, i, j are in registers $16 to $21, register $10
contains the constant 4
2.
In memory, the code for case 0 is at address L0, the code
for case 1 is at address L1, and the code for case 2 is at
address L2
3.
An array JumpTable has already been created with the
first word (at address JumpTable) containing L0, the
second word (address JumpTable + 4) containing L1, and
the third word (JumpTable + 8) containing L2
5
L0:
L1:
L2:
Exit:
mult
lw
jr
add
j
add
j
sub
# $9 = k*4
m jal
Passing arguments/parameters
m
Returning results
m
Example
procA:
procB:
Problems:
m
Solution:
m
B:
C:
jal B
addi
sw
jal
lw
addi
jr
jr
$sp, $sp,-4
$ra, 0($sp)
C
$ra, 0($sp)
$sp, $sp,4
$ra
$ra
10
1. After A calls B
Bs return
address
Memory
$ra
Top of stack
Bottom of stack
$sp
11
$ra
Memory
New Top of stack
Bottom of stack
$sp
12
3. After B calls C
Cs return
address
Bs return
address
Memory
$ra
Top of stack
Bottom of stack
$sp
13
Memory
$ra
New Top of stack
Bottom of stack
$sp
14
$sp
$sp
Contents of register $t1
Contents of register $t0
$sp
Lowaddress
a.
b.
c.
15
$fp
$sp
$sp
$fp
Saved argument
registers (if any)
Saved return address
Saved saved
registers (if any)
Low address
a.
b.
c.
16
Stack
grows
Local variables
$sp
Stack
Old $ra
Old $fp
main
Old $ra
Old $fp
Old $a0
fact (10)
Old $ra
Old $fp
Old $a0
fact (9)
Old $ra
Old $fp
Old $a0
fact (8)
Old $ra
Old $fp
Old $a0
fact (7)
Stack grows
18
$sp
7ff f ffff
$gp
1000 8000
hex
m
m
Text (code)
Data
Static data
Dynamic data
Stack
Stack
Dynamic dat a
hex
1000 0000
hex
0040 0000
he x
Static data
Text
pc
Reserved
0
19
1.
2.
3.
20
10
Caller:
1.
2.
3.
1.
2.
3.
1.
2.
3.
4.
11
Assignment 2
Assignment 2: matrix multiplication
m Multiply
m Three
procedures
Main()
Matrix_multiply(r1,c1,M1,r2,c2,M2,Mr)
Inner_product(row,num_columns,M,column,num_rows,N)
m Need
m Matrices
Matrix Multiplication
Inner Product
n
Cij
ik
Bkj
k =1
24
12