← Back to problems
UNSW COMP1521 · hard

COMP1521 2020T3 Q7 — MIPS functions & recursion

COMP1521 2020T3 Q7 · 9 marks · MIPS Topic: MIPS functions & recursion · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

Summary: Implement reflect(): transpose a 2D array

You have been given 20t3final_q7.s , a MIPS assembler program which implements all but one function of this C program.

// COMP1521 20T3 final exam Q7 C reference

#include <stdio.h>

void read_array ( int rows , int cols , int a [ rows ][ cols ]); void reflect ( int rows , int cols , int a [ rows ][ cols ], int b [ cols ][ rows ]); void print_array ( int rows , int cols , int a [ rows ][ cols ]);

int main ( void ) { int rows ; int cols ; scanf ( "%d" , & rows ); scanf ( "%d" , & cols ); int array1 [ rows ][ cols ]; int array2 [ cols ][ rows ]; read_array ( rows , cols , array1 ); reflect ( rows , cols , array1 , array2 ); print_array ( rows , cols , array1 ); printf ( " \n " ); print_array ( cols , rows , array2 ); }

void read_array ( int rows , int cols , int a [ rows ][ cols ]) { for ( int r = 0 ; r < rows ; r ++ ) { for ( int c = 0 ; c < cols ; c ++ ) { scanf ( "%d" , & a [ r ][ c ]); } } }

void reflect ( int rows , int cols , int a [ rows ][ cols ], int b [ cols ][ rows ]) { for ( int r = 0 ; r < rows ; r ++ ) { for ( int c = 0 ; c < cols ; c ++ ) { b [ c ][ r ] = a [ r ][ c ]; } } }

void print_array ( int rows , int cols , int a [ rows ][ cols ]) { for ( int r = 0 ; r < rows ; r ++ ) { for ( int c = 0 ; c < cols ; c ++ ) { printf ( "%d " , a [ r ][ c ]); } printf ( " \n " ); } }

The function which has not been implemented is named reflect .

Add MIPS instructions for the function reflect to 20t3final_q7.s .

There are comments in 20t3final_q7.s which indicate where the MIPS instructions should be added.

20t3final_q7.s should then be equivalent to the above C program:

For example:

1521 mipsy 20t3final_q7.s 1 2 45 37 45 37

45 37 1521 mipsy 20t3final_q7.s 3 2 14 15 16 17 18 19 14 15 16 17 18 19

14 16 18 15 17 19 1521 mipsy 20t3final_q7.s < 20t3final_q7.input.txt 0 1 2 3 4 5 6 7 8 9

0 2 4 6 8 1 3 5 7 9

No error checking is required.

DO NOT CHANGE the supplied code for the functions: main , read_array , or print_array .

You only have to implement the function reflect .

You can assume the supplied code works.

Your solution must be in MIPS assembler only.


🤖 Write MIPS (mipsy). Read rows, cols, then rows·cols integers (row-major). Print the matrix (each row = integers separated by single spaces), then a blank line, then its transpose in the same format.

Verified on the sandbox AND cross-checked against an independent oracle.

MIPS (mipsy)