OlveraeUNSW
Back to problems
COMP1521

COMP1521 2020T2 Q4 — MIPS control flow

COMP1521 2020T2 Q4 · 10 marks · MIPS
Topic: MIPS control flow · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

Summary: Read numbers until the sum reaches 42, then print the sum


You have been given practice_q4.s ,
a MIPS assembler program that reads 1 number and then prints it.

Add code to practice_q4.s to make it equivalent to this C program:

// read numbers until their sum is >= 42, print their sum

#include <stdio.h>

int main ( void ) {
int sum = 0 ;
while ( sum < 42 ) {
int x ;
scanf ( "%d" , & x );
sum = sum + x ;
}
printf ( "%d \n " , sum );
return 0 ;
}

In other words, it should read numbers until their sum is ≥ 42 and then print their sum.

For example:

1521 mipsy practice_q4.s
10
20
25
55
1521 mipsy practice_q4.s
20
22
42
1521 mipsy practice_q4.s
100
100
1521 mipsy practice_q4.s
10
10
10
10
10
50

No error checking is required.

Your program can assume its input contains only integers.

Your program can assume these integers sum to at least 42.

You can assume the value of the expression can be represented as a signed 32 bit value.
In other words, you can assume overflow/underflow does not occur.

Your solution must be in MIPS assembler only.

Samples

stdin
10
20
25
expected
55
stdin
20
22
expected
42
stdin
100
expected
100
stdin
10
10
10
10
10
expected
50
mipsy
14