CS 136 Lab 2 MIPS ISA SPIM
Feb 05, 2016
CS 136 Lab 2
MIPS ISA
SPIM
Prob 2.30sll $a2, $a2, 2 sll $a3, $a3, 2add $v0, $zero, $zeroadd $t0, $zero, $zero
outer: add $t4, $a0, $t0lw $t4, 0($t4)add $t1, $zero, $zero
inner: add $t3, $a1, $t1lw $t3, 0($t3)bne $t3, $t4, skip addi $v0, $v0, 1
skip: addi $t1, $t1, 4bne $t1, $a3, inneraddi $t0, $t0, 4bne $t0, $a2, outer
Prob 2.31
• add, addi, sll => 1 cycle
• lw, bne => 2 cycles
• For 2GHz CPU, how much time for 1 cycle.
Prob 2.33
• x[4] = x[5] + a;
• Base addr for X = 6,400,000ten
• a is stored in $t3
• MIPS instruction (s)?
Prob 2.36
• for (i=0; i<=100; i=i+1) {a[i] = b[i] + c;}
• How many memory data references will be made during execution?– load, store
• How many instructions are executed during the running of this code?
Prob 2.39
• Suppose
• lb $s0, 100($zero) #byte@100= 0x0F?
• lb $s1, 200($zero) #byte@200= 0xFF
• What are the values of $s0 and $s1?
System Calls
Service System call code Arguments Result
print_int 1 $a0 = integer
print_float 2 $f12 = float
print_double 3 $f12 = double
print_string 4 $a0 = string
read_int 5 integer (in $v0)
read_float 6 float (in $f0)
read_double 7 double (in $f0)
read_string 8 $a0=buffer,$a1=len
sbrk 9 $a0 = amount address (in $v0)
exit 10
Exercise 1.text.globl main
main:li $v0, 4la $a0, str1syscall
li $v0, 5syscall
move $t0, $v0
#print out str2#input second integer#move the input to $t1#code start
#code end
move $t1, $v0
add $t2, $t0, $t1
#print out str3 and the sum#code start
#code end
.datastr1: .asciiz "input first i
nteger:"str2: .asciiz "input second
integer:"str3: .asciiz "the sum is:"
General Purpose Registers (32)
• Reference: A-24
• $at (1), $k0 (26), and $k1 (27): reserved for the assembler and operating system.
• $a0–$a3 (4–7) are used to pass the first four arguments to routines.
• $v0(2) and $v1(3) are used to return values from functions.– What else usage?
General Purpose Registers (32)
• Registers $t0–$t9 (8–15, 24, 25) are caller-saved registers that are used to hold temporary quantities that need not be preserved across calls.
• Registers $s0–$s7 (16–23) are callee-saved registers that hold long-lived values that should be preserved across calls.
• $gp (28), $sp (29), $fp (30), $ra (31)
• PCSPIM…
Source code format
.text
.globl mainmain:[#comment][label:] opcode [op1],[op2],[op3].data
str: .asciiz “string”integer: .word 0xaf,0xffff2,0x4233…
Reference: A-47
Load and Store
li $t0, 5 #load immediate
la $t0, str #load address
lw $t0, 8($sp) #load word
lb $t0, ($s1) #load byte
sb ‘a’, ($s0) #store byte
sw $a0, 100($sp) #store word
Prob 2.39 test code
.text
.globl mainmain:lb $s0, bb1lb $s1, bb2
.databb1: .byte 0x0Fbb2: .byte 0xFF
Arithmetic Instruction
add $t2, $t1, $t0 # $t2 = $t1 + $t0
addi $t1, $t0, 5 # $t1 = $t0 + 5
sub $t2, $t1, $t0 # $t2 = $t1 - $t0
mul $t2, $t1, $t0 # $t2 = $t1 * $t0
div $t2, $t1, $t0 # $t2 = $t1 / $t0
• If we multiply two 32 bit unsigned integers… what is the size in bits of the largest possible result?
#calculate W=A*X^2+B*X+C#asnser should be 180.text.globl main
main:#code start
#code end
.dataX: .word 7A: .word 3B: .word 4C: .word 5W: .word 0ans: .asciiz "answer = "
pseudo-instruction
• Instructions provided by an assembler but not implemented in hardware.
• li $t0, 0x40044005– lui $t0, 4004– ori $t0, $t0, 4005
• li $t0, 0x4005– ori $t0, $t0, 4005
• move $t1, $t2• mul $t2, $t2, $t3
– mult $t4, $t1– mflo $t4
• A-51
Branches
• j <addr>
• beq $t0, $t1, <addr>
• bne $t0, $t1, <addr>
• slt $t1, $t2, $t3– if (t2<t3) then t1=1, else t1=0
• slti $t1, $t2, 7– if (t2<7) then t1=1, else t1=0
pseudo-instruction
• beq $t1, small, L– li $at, small– beq $t1, $at, L
• beq $t2, big, L– li $at, big– beq $at, $t2, L
• blt $t0, $t1, L– slt $at, $t0, $t1– bne $at, $zero, L
• ble $t3, $t5, L– slt $at, $t5, $t3– beq $at, $zero, L
Branch Example.text.globl main
main:la $t2, strli $t1, 0
nextch:lb $t0, ($t2)beqz $t0, strendadd $t1, $t1, 1add $t2, 1j nextch
strend:move $a0, $t1li $v0, 1syscall
.datastr: .asciiz "hello world!"
Exercise.text.globl main
main:#swap all of ‘a’ from str to ‘A’#code begin
#code end
strend:la $a0, strli $v0, 4syscall
.datastr: .asciiz "aabbbababababbaaaabbaa"
void main(){
char str[] = "aabbbababababbaaaabbaa";
char* t2 = str;char t1 = 'A';char t0;
do{
t0 = *t2;if(t0 == 0)
break;if(t0 == 'a')
*t2 = t1;t2++;
}while(1);
printf("%s\n", str);}
Exercise.text.globl main
main:#load the address of string to $t2#load ‘A’ to $t1
nextch:#load byte from address ($t2) to $t0#branch to strend if $t0 = 0#branch to nota if $t0 != 'a'#store byte $t1 to address ($t2)
nota:increment $t2 by 1jump to nextch
strend:la $a0, strli $v0, 4syscall
.datastr:.asciiz "aabbbababababbaaaabbaa"
Booth’s AlgorithmCase # Bits
InstectedAction performed
0 0 0 0 >>2
1 0 0 1 +m’cand & >>2
2 0 1 0 +m’cand & >>2
3 0 1 1 +2*m’cand & >>2
4 1 0 0 -2*m’cand & >>2
5 1 0 1 -m’cand & >>2
6 1 1 0 -m’cand & >>2
7 1 1 1 >>2