COMP1521 22T1 - Week 02 Laboratory Sample Solutions
COMP1521 22T1 - Week 02 Laboratory Sample Solutions
Week 02
Laboratory
Sample Solutions
Objectives
learning to run MIPS programs with mipsy and mipsy-web
understanding MIPS I/O (syscalls)
understanding MIPS control instructions (branch)
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Getting Started
Set up for the lab by
creating a new directory called lab02 and changing to this directory.
$ mkdir lab02
$ cd lab02
There are some provided files for this lab which you can fetch with this command:
$ 1521 fetch lab02
exercise — individual:
MIPS Grading
In the files for this lab,
you have been given grade.s,
a MIPS assembler program
which reads a number
and always prints FL:
$ 1521 mipsy grade.s
FL
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 1/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
#include <stdio.h>
int main(void) {
int mark;
scanf("%d", &mark);
printf("FL\n");
printf("PS\n");
printf("CR\n");
printf("DN\n");
} else {
printf("HD\n");
return 0;
For example:
$ 1521 mipsy grade.s
Enter a mark: 42
FL
Enter a mark: 72
CR
Enter a mark: 89
HD
HINT:
Make sure you understand the odd_even.s lecture example.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 2/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
main:
li $v0, 4
syscall
syscall
la $a0, fl
la $a0, ps
la $a0, cr
la $a0, dn
la $a0, hd
print:
li $v0, 4
syscall
li $v0, 0
jr $ra # return 0
.data
exercise — individual:
MIPS Counting
In the files for this lab,
you have been given a MIPS assembler program count.s,
which reads a number and prints 42:
$ 1521 mipsy count.s
Enter a number: 13
42
#include <stdio.h>
int main(void) {
int number, i;
scanf("%d", &number);
i = 1;
printf("%d\n", i);
i = i + 1;
return 0;
For example:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 3/17
For example:
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
Enter a number: 4
Enter a number: 10
10
HINT:
Make sure you understand the print10.s lecture example.
Started by choosing which register you will use for each variable.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 4/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
# read a number n and print the integers 1..n one per line
li $v0, 4
syscall
syscall
li $t1, 1
loop: # loop:
li $v0, 1
syscall
li $v0, 11
syscall
end:
li $v0, 0
jr $ra # return 0
.data
exercise — individual:
MIPS 7-Eleven
In the files for this lab,
you have been given seven_eleven.s,
a MIPS assembler program
which reads a number and prints 42:
$ 1521 mipsy seven_eleven.s
Enter a number: 13
42
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 5/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
#include <stdio.h>
int main(void) {
int number, i;
scanf("%d", &number);
i = 1;
if (i % 7 == 0 || i % 11 == 0) {
printf("%d\n", i);
i = i + 1;
return 0;
For example:
$ 1521 mipsy seven_eleven.s
Enter a number: 15
11
14
Enter a number: 42
11
14
21
22
28
33
35
HINT:
Make sure you understand the odd_even.s
and print_10.s lecture examples.
Start by choosing which register you will use for each variable.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 6/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
li $v0, 4
syscall
syscall
li $t1, 1 # i = 1;
loop: # loop:
print:
li $v0, 1
syscall
li $v0, 11
syscall
next:
end:
li $v0, 0
jr $ra # return 0
.data
exercise — individual:
MIPS Sequence
In the files for this lab,
you have been given a MIPS assembler program sequence.s,
which reads a number and prints 42:
$ 1521 mipsy sequence.s
42
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 7/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
// Print the integers bwtween `start` and `stop` moving in increments of size `step`
#include <stdio.h>
int main(void) {
scanf("%d", &start);
scanf("%d", &stop);
scanf("%d", &step);
if (step < 0) {
printf("%d\n", i);
if (step > 0) {
printf("%d\n", i);
return 0;
For example:
$ 1521 mipsy sequence.s
-1
-3
-5
-7
-9
# Print the integers bwtween `start` and `stop` moving in increments of size `step`
# `start` in $t0
# `stop` in $t1
# `step` in $t2
# `i` in $t3
li $v0, 4
syscall
syscall
li $v0, 4
syscall
syscall
li $v0, 4
syscall
syscall
b main__end
main__negative:
main__negative__loop:
blt $t3, $t1, main__end
li $v0, 1
syscall
li $a0, '\n'
li $v0, 11
syscall
j main__negative__loop
main__positive:
main__positive__loop:
bgt $t3, $t1, main__end
li $v0, 1
syscall
li $a0, '\n'
li $v0, 11
syscall
j main__positive__loop
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 9/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
main__end:
li $v0, 0
jr $ra # return 0
.data
MIPS Tetrahedra
In the files for this lab,
you have been given tetrahedral.s,
a MIPS assembler program that
reads a number and prints 42:
$ 1521 mipsy tetrahedral.s
Enter a number: 42
42
// https://en.wikipedia.org/wiki/Tetrahedral_number
#include <stdio.h>
int main(void) {
scanf("%d", &how_many);
n = 1;
total = 0;
j = 1;
while (j <= n) {
i = 1;
while (i <= j) {
total = total + i;
i = i + 1;
j = j + 1;
printf("%d\n", total);
n = n + 1;
return 0;
For example:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 10/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
10
20
35
10
20
35
56
84
120
165
220
286
364
HINT:
Make sure you understand
the sum_100_squares.s lecture example.
Started by choosing which register you will use for each variable.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 11/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
# https://en.wikipedia.org/wiki/Tetrahedral_number
# i in register $t0
# j in register $t1
# n in register $t2
li $v0, 4
syscall
syscall
li $t2, 1 # n = 1;
loop0:
li $t3, 0 # total = 0
li $t1, 0 # j = 1
loop1:
li $t0, 1 # i = 1
loop2:
j loop2
next:
j loop1
print:
li $v0, 1
syscall
li $v0, 11
syscall
j loop0
end0:
li $v0, 0
jr $ra # return 0
.data
872677418
872546305
12
65011720
-1
872939537
873005081
19419168
663585
872546305
12
872677386
872546315
12
65011720
-1
42
10
338350
537133098
537001985
12
537133066
537001995
12
65011720
-1
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 13/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
main:
li $v0, 11
syscall
li $v0, 11
syscall
li $v0, 11
syscall
jr $ra
HINT:
This exercises involves
writing only a small amount of code,
but it is tricky to get right
and will involve some thought and
experimentation.
NOTE:
The input to dynamic_load.s
will be between 1 and 1024 signed decimal integers:
spim can't read hexadecimal.
You can assume the instructions don't use initialized data.
This mean printing strings in the normal way won't work.
You can assume the instructions read in execute a jr $ra instruction to terminate.
You can assume the instructions don't use jump instructions,
except for jr $ra.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 14/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
main:
li $v0, 4
syscall
loop:
syscall #
j loop
end:
li $v0, 4
syscall
jalr $t0
li $v0, 4
syscall
li $v0, 0
.data
SOLUTION:
Alternative solution for dynamic_load.s
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 15/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
.data
.text
.globl main
main:
sw $fp, -4($sp)
la $fp, -4($sp)
sw $ra, -4($fp) # $ra is the only register that we need after dynamic code
li $v0, 4 # puts()
la $a0, GREET
syscall
next_input:
li $v0, 5 # scanf("%d")
syscall
sw $v0, 0($sp) # save the dynamic instruction into memory at the location pointed to by $sp
la $sp, 4($sp) # $sp ins't in the stack, it's in the text, so grow up not down
j next_input
end_input:
li $v0, 4 # puts()
la $a0, START
syscall
jal unsafe # goto dynamic code, and allow dynamic code to return to here
li $v0, 4 # puts()
la $a0, STOP
syscall
lw $ra, -4($fp) # $fp is assumed to be in the same place after dynamic code as it was before
la $sp, 4($fp) # $sp is now return to pointing into the stack like nothing strage happend at
all
lw $fp, ($fp) # we do assume that the dynamic code either didn't touch the stack or
conformed to the ABI
li $v0, 0
jr $ra
unsafe:
Submission
When you are finished each exercises make sure you submit your work by running give.
You can run give multiple times.
Only your last submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient
to upload your work via
give's web interface.
Remember you have until
Week 3 Wednesday 21:00:00 to submit your work.
You cannot obtain marks by e-mailing your code to tutors or lecturers.
You check the files you have submitted here.
Automarking will be run by the lecturer several days after the submission deadline,
using test cases different to those autotest runs
for you.
(Hint: do your own testing as well as running autotest.)
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 16/17
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Laboratory Sample Solutions
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/02/answers 17/17