← Back to problems
UNSW COMP1521 · medium

COMP1521 2020T2 Q7 — MIPS arrays

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

Summary: Read into an array until the sum reaches 42, print it reversed

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

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

// Read numbers into an array until their sum is >= 42 // then print the numbers in reverse order

#include <stdio.h>

int numbers [ 1000 ];

int main ( void ) { int i = 0 ; int sum = 0 ; while ( sum < 42 ) { int x ; scanf ( "%d" , & x ); numbers [ i ] = x ; i ++ ; sum += x ; }

while ( i > 0 ) { i -- ; printf ( "%d \n " , numbers [ i ]); } }

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

For example:

1521 mipsy practice_q7.s 11 17 3 19 19 3 17 11 1521 mipsy practice_q7.s 10 20 30 30 20 10 1521 mipsy practice_q7.s 42 42

No error checking is required.

Your program can assume its input contains only integers.

Your program can assume these integers sum to ≥ 42.

Your program can assume that it will have to read no more than 1000 integers before their sum is ≥ 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.


🤖 Write MIPS (mipsy). Read integers until their running sum ≥ 42, then print the numbers read in reverse order, one per line.

Verified on the sandbox AND cross-checked against an independent oracle (official examples included).

MIPS (mipsy)