CS 241 â Week 2 Tutorial Solutions - Student.cs.uwaterloo.ca
CS 241 â Week 2 Tutorial Solutions - Student.cs.uwaterloo.ca
CS 241 â Week 2 Tutorial Solutions - Student.cs.uwaterloo.ca
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>CS</strong> <strong>241</strong> – <strong>Week</strong> 2 <strong>Tutorial</strong> <strong>Solutions</strong><br />
Assembly Language Programming<br />
Spring 2013<br />
1 Problem 0 - Convert MIPS to ML<br />
1. add $3, $3, $0<br />
The format for an add instruction is:<br />
0000 00ss ssst tttt dddd d000 0010 0000<br />
in this <strong>ca</strong>se, d = 3(00011), s = 3(00011), t = 0(00000). So the final 32-bit binary is:<br />
0000 00<br />
}<br />
00<br />
{{<br />
011<br />
}<br />
0<br />
}<br />
0000<br />
{{ }<br />
0001<br />
} {{<br />
1<br />
}<br />
000 0010 0000<br />
$s $t $d<br />
The Hexadecimal representation is: 0x00601820.<br />
2. slt $3, $20, $30<br />
The format for a slt instruction is:<br />
0000 00ss ssst tttt dddd d000 0010 1010<br />
in this <strong>ca</strong>se, d = 3(00011), s = 20(10100), t = 30(11110). So the final 32-bit binary is:<br />
0000 00<br />
}<br />
10<br />
{{<br />
100<br />
}<br />
1<br />
}<br />
1110<br />
{{ }<br />
0001<br />
} {{<br />
1<br />
}<br />
000 0010 1010<br />
$s $t $d<br />
The Hexadecimal representation is: 0x029E182A.<br />
3. beq $0, $0, -4<br />
The format for an beq instruction is:<br />
0001 00ss ssst tttt iiii iiii iiii iiii<br />
in this <strong>ca</strong>se, s = 0(00000), t = 0(00000), i = -4(1111 1111 1111 1100). So the final 32-bit binary is:<br />
0001 00<br />
}<br />
00<br />
{{<br />
000<br />
}<br />
0<br />
}<br />
0000<br />
{{ }<br />
1111<br />
}<br />
1111<br />
{{<br />
1111 1100<br />
}<br />
$s $t<br />
i<br />
The Hexadecimal representation is: 0x1000FFFC.<br />
2 Problem 1 - How to write a MIPS loop<br />
Sol(a) ;set up constants<br />
lis $5<br />
.word 1<br />
add $3, $0, $0<br />
slt $2, $0, $1<br />
beq $2, $0, 3<br />
;$2 = 0 => 0 !< n<br />
add $3, $1, $3<br />
;result += n<br />
sub $1, $1, $5 ;n -= 1<br />
1
eq $0, $0, -5<br />
jr $31<br />
Sol(b) ;$2 = i<br />
;$1 = n<br />
;$3 = result<br />
;$5 = 1<br />
;set up constants<br />
lis $5<br />
.word 1<br />
;set $3 to 0, $2 to 1<br />
add $3, $0, $0<br />
add $2, $5, $0<br />
slt $6, $1, $2 ;i n)<br />
beq $6, $5, 3 ;$6 = 1 => (n < i)<br />
add $3, $3, $2<br />
add $2, $2, $5<br />
beq $0, $0, -5<br />
;result += i<br />
;i++<br />
jr $31<br />
Sol(c) ;set up constants<br />
lis $5<br />
.word 1<br />
add $3, $0, $0<br />
add $3, $1, $3<br />
;result += n<br />
sub $1, $1, $5 ;n -= 1<br />
bne $1, $0, -3<br />
;$1 != 0, then loop<br />
jr $31<br />
3 Problem 2 - More looping<br />
We want to compute the nth fibonacci number. Assume the value of n is in $1 and that the nth fibonacci<br />
number fits in a 32-bit integer. Compute the nth fibonacci number and store it in $3, then return.<br />
;find the nth fib number<br />
;$1 = n<br />
;$2 = F_n-1<br />
;$3 = F_n<br />
;$5 = 1<br />
;high level inspiration<br />
;int fib(int n) {<br />
; int a = 1;<br />
2
; int b = 0;<br />
; while (n != 0) {<br />
; int temp = a + b;<br />
; a = b;<br />
; b = temp;<br />
; n = n -1<br />
; }<br />
; return b;<br />
;}<br />
lis $2<br />
.word 1 ; a = 1<br />
add $5, $2, $0 ; $5 = 1<br />
add $3, $0, $0 ; b = 0<br />
beq $1, $0, 5<br />
; n != 0, goes into loop<br />
add $4, $2, $3<br />
; temp = a + b<br />
add $2, $3, $0<br />
; a = b<br />
add $3, $4, $0<br />
; b = temp<br />
sub $1, $1, $5 ; n = n - 1<br />
beq $0, $0, -6<br />
jr $31<br />
; return to top of loop<br />
; return b<br />
3