Mips Tutorial
Mips Tutorial
Lec.03 - 288
Name
$t0-$t7
$s0-$s7
$t8-$t9
Number
8-15
16-23
24-25
Usage
Temporaries
Saved
more temporaries
Lec.03 - 289
Lec.03 - 290
Lec.03 - 291
Lec.03 - 292
Lec.03 - 293
Lec.03 - 294
Lec.03 - 295
Lec.03 - 296
Lec.03 - 297
Lec.03 - 298
$s0,0x8000000
$t0,10
$t0,0($s0)
$t0,4($s0)
Lec.03 - 299
Base Address + 4 i
Lec.03 - 300
Recap Load/Store
Lec.03 - 301
Lec.03 - 302
MIPS Tutorial
All this has a positive side, you can write programs that write
other programs (Can you think of any?).
Lec.03 - 303
Lec.03 - 304
MIPS Tutorial
.data
n1:
.word 10
.text
.globl main
main:
la $t0,n1
lw $s0,0($t0)
addi $s0,$s0,1
sw $s0,0($t0)
jr $31
main is the address of the first instruction. n1 is the address of
a piece of data.
Lec.03 - 305
Lec.03 - 306
Lec.03 - 307
skip:
bne $s1,$s2,skip
add $s3,$0,$0
Next Instruction
Lec.03 - 308
set_zero:
skip2:
beq $s1,$s2,set_zero
addi $s3,$s3,1
j skip2
add $s3,$0,$0
Next Instruction
Lec.03 - 309
MIPS Tutorial
There is often more than one way of writing the same piece of
code.
For example:
skip:
bne $s1,$s2,skip
add $s3,$0,$0
Next Instruction
settozero:
skip:
beq $s1,$s2,settozero
j skip
add $s3,$0,$0
Next Instruction
Lec.03 - 310
exit:
addi $t0,$0,0
addi $t1,$0,10
beq $t0,$t1,exit
add $t2,$t0,$t0
add $t2,$t2,$t2 # $t2 = $t0*4
add $t2,$s0,$t2 # $t2 = address of A[$t0]
sw $0,0($t2)
addi $t0,$t0,1
j loop
Pseudo C code
for($t0 = 0; $t0 != 10 ; $t0 = $t0 +1 ) {
A[$t0] = 0
}
18-347 Fall 2003
Lec.03 - 311
Lec.03 - 312
General format
slt $Rdest,$Rsrc1,$RSrc2
Lec.03 - 313
MIPS Tutorial
Pseudo C code:
if $s1 < $s2 then $s3 = 0
Assembly:
slt $t0,$s1,$s2
beq $t0,$zero,skip
add $s3,$zero,$zero
skip:
Lec.03 - 314
Lec.03 - 315
Lec.03 - 316
Lec.03 - 317
silly:
li $a0,10
li $a1,21
li $a3,31
jal silly
#result is is $v0.
li $v0,10
syscall
add $t0,$a0,$a1
sub $v0,$a3,$t0
jr $ra
Lec.03 - 318
Lec.03 - 319
jal silly
jal silly2
jr $ra
jr $ra
Lec.03 - 320
Lec.03 - 321
Lec.03 - 322
Everything you push onto the stack, you must pop from the stack.
Never touch anything on the stack that does not belong to you.
Lec.03 - 323
silly:
addi $sp,$sp,-4
sw $ra,0($sp)
jal silly2
lw $ra,0($sp)
add $sp,$sp,4
jr $ra
# PUSH
# ra
# POP
# ra
Lec.03 - 324
addi $sp,$sp,-4
sw $s0,0($sp)
addi $sp,$sp,-4
sw $ra,0($sp)
jal silly2
lw $ra,0($sp)
add $sp,$sp,4
lw $s0,0($sp)
add $sp,$sp,4
jr $ra
#
#
#
#
PUSH
s0
PUSH
ra
#
#
#
#
POP
ra
POP
s0
Lec.03 - 325
addi $sp,$sp,-8
sw $s0,4($sp)
sw $ra,0($sp)
jal silly2
lw $ra,0($sp)
lw $s0,4($sp)
addi $sp,$sp,8
jr $ra
Lec.03 - 326
Optimization:
Lec.03 - 327
Example
Factorial
N! = N*(N-1)*(N-2)* * 1
Lec.03 - 328
Example: factorial
result: .space 4 #the place for the result
.text
.globl main
main: addi $sp,$sp,-4 #save the return address.
sw $ra,0($sp)
lw $a0, 5
jal fact
la $t0,result
sw $v0, 0($t0)
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra
Lec.03 - 329
Example: factorial
fact:
addi $sp,$sp,-4
sw $ra,0($sp) #push $ra on the stack
#fact of 0 is 1
bne $a0,$zero,not_zero
#Set $v0 to be 1
addi $v0,$zero,1
#Restore $ra from the stack
lw $ra,0($sp) #Read $ra from the stack
addi $sp,$sp,4 #restore stack pointer.
jr $ra
Lec.03 - 330
Example: factorial
not_zero:
addi $sp,$sp,-4
sw $a0,0($sp) #push n on the stack ($a0=n)
#So call fact with our new parameter
addi $a0,$a0,-1
jal fact # $v0=fact(n-1)
lw $t0,0($sp) #restore n from the stack.
addi $sp,$sp,4
mul $v0,$v0,$t0 #$v0 = fact(n-1)($v0)*n($t0)
#Restore the stack for $ra
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra
Lec.03 - 331