CS 241 â Week 4 Tutorial - Student.cs.uwaterloo.ca
CS 241 â Week 4 Tutorial - Student.cs.uwaterloo.ca
CS 241 â Week 4 Tutorial - 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> 4 <strong>Tutorial</strong><br />
Assemblers<br />
Spring 2013<br />
<strong>Tutorial</strong> 3 Topi<strong>cs</strong><br />
• Creating Symbol Tables<br />
• Assembling<br />
• Error Checking<br />
1 Problem 1a<br />
Given the following MIPS assembly code, create a symbol table containing all of the labels used.<br />
; Assume program is to be run with mips.twoints<br />
; $1 - first integer<br />
; $2 - second integer<br />
lis $3<br />
.word 100<br />
bound: lis $4<br />
.word -100<br />
lis $1<br />
.word bound<br />
slt $5, $1, $3<br />
slt $6, $2, $4<br />
beq $5, $0, move<br />
bne $6, $0, jump<br />
bne $5, $0, leap<br />
leap: beq $6, $0, bound<br />
shuffle: ; store ’leap’s lo<strong>ca</strong>tion in $7<br />
lis $7<br />
.word leap<br />
jump:<br />
move: jr $31<br />
1
2 Problem 1b<br />
Given the following MIPS assembly code, create a symbol table containing all of the labels used.<br />
; Assume program is to be run with mips.twoints<br />
; $1 - first integer<br />
; $2 - second integer<br />
lis $3<br />
.word 0xFF123<br />
start:<br />
next:<br />
add $1, $1, $3<br />
sub $3, $3, $1<br />
sw $3, -4($30)<br />
lis $4<br />
.word 4<br />
sub $30, $30, $4<br />
lis $5<br />
.word -1<br />
beq $0, $0, skip<br />
handleNeg: ; handle negativity<br />
mult $3, $5<br />
mflo $3<br />
skip:<br />
; Check if $3 is negative<br />
slt $6, $3, $0<br />
bne $6, $0, handleNeg<br />
loop:<br />
add $3, $3, $5<br />
bne $3, $0, loop<br />
end: endplus1: jr $31<br />
2
3 Problem 2<br />
Assemble the following (short) MIPS assembly code into binary, taking into account what values should be<br />
substituted in place of the labels.<br />
; Problem 2: Assemble this (short!) MIPS program into binary format.<br />
L1: lis $28<br />
.word L2<br />
bne $0, $0, L3<br />
L2:<br />
beq $0, $0, L1<br />
L3: ; End program<br />
jr $31<br />
4 Error Checking<br />
All of the following programs are invalid and contain multiple errors. Identify the errors in each of them.<br />
1. label: .word label<br />
label:<br />
.word label label2:<br />
.word abcd<br />
2. .word $16<br />
.word 1 2 3<br />
ten:<br />
.word (10)<br />
add $1, $2<br />
add $1 $2 $3<br />
lw $5, ten($30)<br />
3. lis $10<br />
.word 5000000000<br />
lis $11<br />
.word 0xBEEFBEEFBEEF<br />
sw $10, 0xffff000c($0)<br />
beq $10, $11, -65432<br />
sub $32, $33, $34<br />
3