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? -
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:
-
Write a MIPS program that:
1. Loads the number
5into register$t0. 2. Loads the number7into register$t1. 3. Adds the two numbers and stores the result in$t2. 4. Prints the result with a new line character '\n'. The program should print:12
Run your program with1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question. -
Write a MIPS program that reads an integer from the user and then prints the integer followed by a new line character.
The C equivalent is:
Your MIPS program should behave in the same way. For example:
#include<stdio.h> int main(void) { int x; scanf("%d", &x); printf("%d\n", x); return 0; }
42 42
Run your program with1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question. -
Write a MIPS program that reads an integer from the user, doubles the integer, and then prints the result followed by a new line character.
The C equivalent is:
For example, if the user enters:
int main(void) { int x; scanf("%d", &x); x = x * 2; printf("%d\n", x); return 0; }
6
The program should print:12
Run your program with1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question. -
Write a MIPS program that defines an integer in the
.datasection, loads the integer from memory usinglw, and then prints the integer followed by a new line character. The C equivalent is:Your MIPS program should store the integer in the#include<stdio.h> int x = 42; int main(void) { printf("%d\n", x); return 0; }
.datasection and uselwto load its value into a register before printing it. Run your program with1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question. -
Write a MIPS program that defines an integer in the
.datasection. Load the integer from memory, add10to it, and then store the new value back into memory usingsw. Finally, load the value from memory again and print it followed by a new line character. The C equivalent is:Your MIPS program must use bothint x = 10; int main(void) { x = x + 10; printf("%d\n", x); return 0; }
lwandsw. Run your program with1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question. -
Write a MIPS program that swaps the values of two integers and then prints the two values, each followed by a new line character.
The C equivalent is:
Your MIPS program should use the
int x = 10; int y = 20; int main(void) { int temp; temp = x; x = y; y = temp; printf("%d\n", x); printf("%d\n", y); return 0; }
.datasection to store the two integers and a temporary variable. Run your program with1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question. -
Write a MIPS program that defines two integers in the
.datasection, loads both integers usinglw, adds them together, and prints the result followed by a new line character. The C equivalent is:Your MIPS program must useint x = 10; int y = 20; int main(void) { printf("%d\n", x + y); return 0; }
lwto load both integers from memory. Run your program with1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question. -
Write a MIPS program that prints
Hello, world!and run it with1092 mipsy.Do the answer slowly and step by step. Have a plan before you start the question.#include <stdio.h> int main(void) { printf("Hello, world!\n");// this is going to be 2 syscalls }
-
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; }
-
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.#include<stdio.h> int main(){ int x, y, z; printf("Enter the first number: "); scanf("%d", &x); printf("Enter the second number: "); scanf("%d", &y); printf("Enter the third number: "); scanf("%d", &z); int result = ((((x<<2) + y)>>1)+z); printf("%d\n", result); return 0; }
-
Write MIPS code to take an 8-bit value (given by the user using a syscall). Then, set the middle 4 bits to 1 (bits 2 to 5, counting from the least significant bit as bit 0).
Leave the other bits unchanged, and print the result to standard output. In order to do this we need to create the following mask
0b00111100We learned two ways to create this mask. Use the following C code as a guide on how to create a mask.#include<stdio.h> int main(){ int mask = 0x3c; //OR the following method int mask = 1; //0000 0001 mask = mask << 4; //0001 0000 mask = mask - 1; // 0000 1111 mask = mask << 2; // 0011 1100 }
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
.datasegment; the labels are the same as the C variable names; all results fit in a 32-bit register (i.e. no need to explicitly useHiandLo).