COMP1521 22T1 - Week 03 Tutorial Answers
COMP1521 22T1 - Week 03 Tutorial Answers
COMP1521 22T1 - Week 03 Tutorial Answers
Week 03
Tutorial
Answers
1. Often when writing large MIPS programs, you
will make accidental errors that cause your
program to misbehave.
Discuss what tools are available to help
debug broken MIPS code.
ANSWER:
Discussed in tutorial:
mipsy-interactive (1521 mipsy)
mipsy-web (https://cs1521.web.cse.unsw.edu.au/mipsy/)
Line-by-line debugging (stepping)
Breakpoints
Printf (syscall) debugging
2. If the data segment of
a particular MIPS program
starts at the address 0x10000020,
then what addresses are
the following
labels associated with,
and what value is stored in
each 4-byte memory cell?
.data
a: .word 42
b: .space 4
c: .asciiz "abcde"
.align 2
d: .byte 1, 2, 3, 4
e: .word 1, 2, 3, 4
f: .space 1
ANSWER:
Memory map showing labels,
associated addresses,
and contents of memory cells:
0x1001000C .word 3
0x10010010 .word 5
0x10010014 .word 7
b. lw $t0, bb
c. lb $t0, bb
d. lw $t0, aa+4
e. la $t1, cc
lw $t0, ($t1)
f. la $t1, cc
lw $t0, 8($t1)
g. li $t1, 8
lw $t0, cc($t1)
h. la $t1, cc
lw $t0, 2($t1)
ANSWER:
a. la $t0, aa
b. lw $t0, bb
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/03/answers 2/10
23/05/2022, 16:25 COMP1521 22T1 — Week 03 Tutorial Answers
lw $t0, ($t1)
lw $t0, 8($t1)
g. li $t1, 8
lw $t0, cc($t1)
lw $t0, 2($t1)
int main(void) {
int i;
i = 0;
scanf("%d", &numbers[i]);
i++;
ANSWER:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/03/answers 3/10
23/05/2022, 16:25 COMP1521 22T1 — Week 03 Tutorial Answers
# i in register $t0
main:
li $t0, 0 # i = 0
loop0:
syscall #
la $t2, numbers #
j loop0 # }
end0:
jr $ra # return
.data
numbers:
int main(void) {
int i;
i = 0;
printf("%d\n", numbers[i]);
i++;
ANSWER:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/03/answers 4/10
23/05/2022, 16:25 COMP1521 22T1 — Week 03 Tutorial Answers
# i in register $t0
main:
li $t0, 0 # i = 0
loop1:
la $t2, numbers #
syscall
li $v0, 11
syscall
j loop1 # }
end1:
jr $ra # return
.data
numbers:
int i;
i = 0;
if (numbers[i] < 0) {
numbers[i] += 42;
i++;
ANSWER:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/03/answers 5/10
23/05/2022, 16:25 COMP1521 22T1 — Week 03 Tutorial Answers
# i in register $t0
main:
li $t0, 1 # i = 1
loop2:
la $t2, numbers #
sw $t5, ($t3) #
# }
skip2:
j loop2 # }
end2:
jr $ra # return
.data
numbers:
int main(void) {
int i;
i = 0;
while (i < 5) {
int x = numbers[i];
numbers[i] = y;
numbers[9 - i] = x;
i++;
ANSWER:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/03/answers 6/10
23/05/2022, 16:25 COMP1521 22T1 — Week 03 Tutorial Answers
main:
li $t0, 0 # i = 0
loop2:
la $t2, numbers #
li $t5, 9 # $t5 = 9 - i
j loop2 # }
end2:
jr $ra # return
.data
numbers:
char *s = &string[0];
int length = 0;
string:
.asciiz "...."
Assume that
the variable s is
implemented as register $t0, and
variable length is
implemented as register $t1.
And, assume
that the character '\0'
can be represented by a value of zero.
ANSWER:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/03/answers 7/10
23/05/2022, 16:25 COMP1521 22T1 — Week 03 Tutorial Answers
.data
string:
.asciiz "...."
.text
li $t1, 0
while:
Revision questions
The following questions are primarily intended for revision, either this week or later in session.
Your tutor may still choose to cover some of these questions, time permitting.
1. Conceptually,
the MIPS pseudo-instruction to
load an address could be encoded as
something like the following:
ANSWER:
The la pseudo-instruction
is implemented as two real MIPS instructions:
lui $t1, BaseAddr # top 16 bits of address
scanf("%d", &y);
x = (y + 2000) * (y + 3000);
ANSWER:
.data
x: .space 8
y: .space 4
.text
li $v0, 5
syscall
sw $v0, y
lw $t0, y
lw $t1, y
mfhi $t0
mflo $t0
lw $t1, y
lw $t4, z
lw $t1, y
lw $t1, x
lw $t2, y
lw $t1, z
lw $v0, y # v0 = y
lw $t0, z # t0 = z
lw $t0, x # x
lw $t1, y # y
lw $t0, z # z
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/03/answers 10/10