Week 04 Tutorial Questions
Objectives
- introducing MIPS data directives as a way to define and store globally-accessible data
- introducing MIPS instructions that use and modify memory addresses
- introducing the arithmetic needed to manipulate MIPS 1D arrays
-
Translate the following C program into
MIPS assembler and run it with
1092 mipsy.Do the answer slowly and step by step. Have a plan before you start the question. 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; printf("Enter a number: "); scanf("%d", &x); y = x * x; printf("%d\n", y); return 0; }
-
Write a MIPS assembler program that reads an integer and prints whether the number is odd or even.
You should use a branch instruction to select the appropriate message.
#include <stdio.h> int main(void) { int number; printf("Enter a number: "); scanf("%d", &number); if ((number & 1) == 0) { printf("Even\n"); } else { printf("Odd\n"); } 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.
#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"); } }
-
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; }
-
Write a MIPS assembler program that reads a non-negative integer and counts how many digits it contains.
Your program should print the number of digits in the input number.
You may assume that the input number is non-negative.
Some examples are shown below:
Enter a number: 7 1
Enter a number: 42 2
Enter a number: 58392 5
Enter a number: 100000 6
The number0should be counted as having one digit:Enter a number: 0 1
#include <stdio.h> int main(void) { int number; int digit_count = 0; printf("Enter a number: "); scanf("%d", &number); if (number == 0) { digit_count = 1; } else { while (number > 0) { number = number / 10; digit_count++; } } printf("%d\n", digit_count); return 0; }
-
Write a MIPS assembler program that reads a non-negative integer and prints the integer with its digits reversed.
You may assume that the input number is non-negative.
Some examples are shown below:
Enter a number: 7 7
Enter a number: 42 24
Enter a number: 58392 29385
Enter a number: 1000 1
Leading zeroes in the reversed number do not need to be printed. For example:Enter a number: 1200 21
#include <stdio.h> int main(void) { int number; int reversed = 0; printf("Enter a number: "); scanf("%d", &number); while (number > 0) { int digit = number % 10; reversed = reversed * 10 + digit; number = number / 10; } printf("%d\n", reversed); return 0; }
-
Write a MIPS assembler program that implements a simple calculator.
The program should read an integer, an operation, and another integer. The operation will be one of:
- `+` for addition
- `-` for subtraction
- `*` for multiplication
- `/` for integer division
The program should calculate and print the result.
You may assume that the input contains a valid operation and that the second number is not zero.
#include <stdio.h> int main(void) { int number1; int number2; char operation; printf("Enter an expression: "); scanf("%d %c %d", &number1, &operation, &number2); if (operation == '+') { printf("%d\n", number1 + number2); } else if (operation == '-') { printf("%d\n", number1 - number2); } else if (operation == '*') { printf("%d\n", number1 * number2); } else if (operation == '/') { printf("%d\n", number1 / number2); } return 0; }