Open changhengliou opened 4 years ago
Arguments are stored by registers
$a0
-$a3
void takesArguments(int x, int y) {
x = x + y;
}
takesArguments(3, 4);
# above becomes below
li $a0 3
li $a1 4
jal takesArgument
takesArguments:
add $a0, $a0, $a1
jr $ra
Return value are stored by registers
$v0
and$v1
int addTwo(int x, int y) { return x + y; }
addTwo: add $v0, $a0, $a1 jr $ra
Temp value is used by registers
$t0
-$t7
int needsTemps(int x, int y) {
int add = x + y;
int sub = x - y;
int mult = add * sub;
return mult;
}
# above becomes below
needsTemps:
add $t0, $a0, $a1
sub $t1, $a0, $a1
mult $t0, $t1
mflo $t2
move $v0, $t2
jr $ra
push
$ra
addiu $sp, $sp, -4 sw $ra, 0($sp)
void fourth() {}
void third() {
fourth();
}
void second() {
third();
}
void first() {
second();
exit();
}
# above becomes below
fourth:
jr $ra
third:
addiu $sp, $sp, -4
sw $ra, 0($sp)
jal fourth
lw $ra, 0($sp)
addiu $sp, $sp, 4
jr $ra
second:
addiu $sp, $sp, -4
sw $ra, 0($sp)
jal third
lw $ra, 0($sp)
addiu $sp, $sp, 4
jr $ra
first:
jal second
# perform the exit syscall from SPIM
li $v0, 10
syscall
Program structure
Registers
$
$0
through$31
$t1
,$sp
Lo
andHi
used to store result of multiplication and division$at
$v0
-$v1
$a0
-$a3
$t0
-$t7
$s0
-$s7
$t8
-$t9
$k0
-$k1
$gp
$sp
$s8
/$fp
$ra
Load / Store Instructions
copy byte at source RAM location to low-order byte of destination register
lb register_destination, RAM_source
store word in source register into RAM destination
sw register_source, RAM_destination
store byte (low-order) in source register into RAM destination
sb register_source, RAM_destination
load immediate value into destination register
li register_destination, value
copy RAM address of var1 into register $t0
la $t0, var1
load word at RAM address contained in $t0 to $t2
lw $t2, ($t0)
store $t2 to RAM address contained in $t0
sw $t2, ($t0)
load word at RAM address contained in $t0 + 4 to $t2
lw $t2, 4($t0)
load word at RAM address contained in $t0 - 12 to $t2
sw $t2, -12($t0)
array1: .space 12 # declare 12 bytes of storage to hold array of 3 integers .text __start: la $t0, array1 # load base address of array into register $t0 li $t1, 5 # $t1 = 5 ("load immediate") sw $t1, ($t0) # first array element set to 5; indirect addressing li $t1, 13 # $t1 = 13 sw $t1, 4($t0) # second array element set to 13 li $t1, -7 # $t1 = -7 sw $t1, 8($t0) # third array element set to -7 done
add $t0,$t1,$t2 # $t0 = $t1 + $t2; add as signed (2's complement) integers sub $t2,$t3,$t4 # $t2 = $t3 - $t4 addi $t2,$t3, 5 # $t2 = $t3 + 5; add immediate addu $t1,$t6,$t7 # $t1 = $t6 + $t7; add as unsigned integers subu $t1,$t6,$t7 # $t1 = $t6 + $t7; subtract as unsigned integers
mult $t3,$t4 # multiply 32-bit quantities in $t3 and $t4, and store 64-bit
result in special registers Lo and Hi: (Hi,Lo) = $t3 * $t4
div $t5,$t6 # Lo = $t5 / $t6 (integer quotient)
Hi = $t5 mod $t6 (remainder)
mfhi $t0 # move quantity in special register Hi to $t0: $t0 = Hi mflo $t1 # move quantity in special register Lo to $t1: $t1 = Lo
used to get at result of product or quotient
move $t2,$t3 # $t2 = $t3
b target # unconditional branch to program label target beq $t0,$t1,target # branch to target if $t0 = $t1 blt $t0,$t1,target # branch to target if $t0 < $t1 ble $t0,$t1,target # branch to target if $t0 <= $t1 bgt $t0,$t1,target # branch to target if $t0 > $t1 bge $t0,$t1,target # branch to target if $t0 >= $t1 bne $t0,$t1,target # branch to target if $t0 <> $t1
j target # unconditional jump to program label target jr $t3 # jump to address contained in $t3 (jump register)
copy program counter (return address) to register $ra (return address register)
jump to program statement at sub_label
jal sub_label # jump and link
jr $ra # jump register, jump to return address $ra