Week 04 Tutorial Answers
-
What are
mipsy
,spim
,qtspim
&xspim
.All four are MIPS emulators which simulate the execution of a MIPS CPU and let you run MIPS assembler on any computer.
mipsy
has been developed at CSE by Zac Kologlu - and it designed to give students learning MIPS assembler extra help.mipsy
is the best emulator to use to develop and debug your assembler programs.mipsy
is very new so if you find bugs - please report in the course forum.spim
is a similar (much older) program. You probably need to use spimfor
this weeks second challenge exercise becausemipsy
might not (yet) work for the unusual things you need to do in it.qtspim
&xspim
are GUI-based versions of spim. They show you the value in all registers and the state of memory which is helpful when debugging. Students usually preferqtspim
We hope to deploy a web-based version of mipsy in time for you to use this term. It should be better then
qtspim
&xspim
-
The MIPS processor has 32 general purpose 32-bit registers, referenced as
$0
..$31
. Some of these registers are intended to be used in particular ways by programmers and by the system. For each of the registers below, give their symbolic name and describe their intended use:-
$0
-
$1
-
$2
-
$4
-
$8
-
$16
-
$26
-
$29
-
$31
-
-
Translate this C program to MIPS assembler
Store variable x in register $t0 and store variable y in register $t1.
// print the square of a number #include <stdio.h> int main(void) { int x, y; printf("Enter a number: "); scanf("%d", &x); y = x * x; printf("%d\n", y); return 0; }
-
Translate this C program so it uses goto rather than if/else.
Then translate it to MIPS assembler.
#include <stdio.h> int main(void) { int x, y; printf("Enter a number: "); scanf("%d", &x); if (x > 46340) { printf("square too big for 32 bits\n"); } else { y = x * x; printf("%d\n", y); } return 0; }
-
Translate this C program so it uses goto rather than if/else.
Then translate it to MIPS assembler.
Consider this alternate version of the above program, use its approach to produce simpler MIPS assembler.#include <stdio.h> int main(void) { int x; printf("Enter a number: "); scanf("%d", &x); if (x > 100 && x < 1000) { printf("medium\n"); } else { printf("small/big\n"); } }
#include <stdio.h> int main(void) { int x; printf("Enter a number: "); scanf("%d", &x); char *message = "small/big\n"; if (x > 100 && x < 1000) { message = "medium"; } printf("%s", message); }
-
Translate this C program so it uses goto rather than if/else.
Then translate it to MIPS assembler.
#include <stdio.h> int main(void) { for (int x = 24; x < 42; x += 3) { printf("%d\n",x); } }
-
Translate this C program so it uses goto rather than if/else.
Then translate it to MIPS assembler.
// print a triangle #include <stdio.h> int main (void) { for (int i = 1; i <= 10; i++) { for (int j = 0; j < i; j++) { printf("*"); } printf("\n"); }; return 0; }
-
Translate this C program so it uses goto rather than if/else.
Then translate it to MIPS assembler.
// Simple factorial calculator - without error checking #include <stdio.h> int main (void) { int n; printf("n = "); scanf("%d", &n); int fac = 1; for (int i = 1; i <= n; i++) { fac *= i; } printf ("n! = %d\n", fac); return 0; }
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.