Week 03 Tutorial Questions
Objectives
- to introduce MIPS registers as locations to store data
- to introduce some common MIPS instructions
- to introduce MIPS syscalls as ways to interact with the program
- introducing MIPS branch instructions as a way to control program flow
-
What is
mipsy
(andmipsy_web
), and what do we use them for in DPST1092? -
Often when writing large MIPS programs, you will make accidental errors that cause your program to misbehave.
Discuss what tools are available to help debug broken MIPS code.
-
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
-
-
Translate the following C program into
MIPS assembler and run it with
1092 mipsy
.Store variable x in register $t0 and store variable y in register $t1.// Prints the square of a number #include <stdio.h> int main(void) { int x, y; int y = 1; 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.
// Squares a number, unless its square is too big for a 32-bit integer. // If it is too big, prints an error message instead. #include <stdio.h> #define SQUARE_MAX 46340 int main(void) { int x, y; printf("Enter a number: "); scanf("%d", &x); if (x > SQUARE_MAX) { 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"); } }
// A simple program demonstrating how to represent a implementing an && in an // if-statement in MIPS. // This version: C, but a slightly less nice approach. #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.
// Print every third number from 24 to 42. #include <stdio.h> int main(void) { // This 'for' loop is effectively equivalent to a while loop. // i.e. it is a while loop with a counter built in. 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.
// Prints a right - angled triangle of asterisks, 10 rows high. #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.
-
Write a MIPS program that is equivalent to the following c program
int main(void) { int a; printf("Enter a: "); scanf("%d", &a); printf("%d is a ", a); if (a < 0) { if (a < -100) { printf("big"); } else { printf("small"); } printf(" negative"); } else { if (a < 100) { printf("big"); } else { printf("small"); } printf(" positive"); } printf(" number.\n"); return 0; }
-
Write a MIPS program that is equivalent to the following c program
int main(void){ int i, n; scanf("%d",&n); i = 0; while ( i < n ){ printf("%d\n",i); i++; } return 0; }
-
Write a MIPS program that is equivalent to the following c program
int main(void){ int start, end; scanf("%d",&start); scanf("%d",&end); while ( start <= end ){ printf("%d\n",start); start++; } return 0; }
-
Write a MIPS program that is equivalent to the following c program
int main(void) { int sum, n; sum = 0; scanf("%d", &n); while (n >= 0) { sum += n; scanf("%d",&n); } printf("Sum of the numbers is %d\n", sum); return 0; }
-
Write MIPS assembler to evaluate the following C expression,
leaving the result in register
$v0
.((x*x + y*y) - x*y) * z
Then try to write one version that minimises the number of instructions and another version that minimises the number of registers used (without using temporary memory locations).
Assume that: all variables are in labelled locations in the
.data
segment; the labels are the same as the C variable names; all results fit in a 32-bit register (i.e. no need to explicitly useHi
andLo
).