Lab 10: MIPS Assembly Programming (Basic)
Overview
In this lab, you will gain experience with the SPIM simulator and MIPS assembly programming by writing a sequence of programs with increasing complexity.
Lab - Getting Started
We will be using SPIM, a MIPS simulator, in order to learn assembly programming. Before lab, you should complete the QtSPIM tutorial, including installing the simulator and running your first program.
Familiarize yourself with the various MIPS reference materials on the Resources page, particularly the MIPS Instruction Set overview.
Review the MIPS Example Programs to see basic arithmetic, looping, I/O, and function calls, plus good assembly programming style.
If you are using the GEdit text editor, don't forget to install the syntax highlighting extension, which makes your code easier to read.
Programming Requirements (for all exercises):
(1) File names: Names your assembly source code something which makes it obvious what the file contains, like: part1.asm
(2) Commenting: **Each line** of assembly code must be documented with a comment!
This comment can be terse. For example, "i++" is a perfectly reasonable comment for the line addi $t1,$t1,1 where register $t1 holds the loop counter i.
(3) Commenting: Each region of assembly code must clearly have a **comment block** documenting the overall purpose of that region, along with what values each register holds. You can use your own judgement for a reasonable region size.
Lab Submission:
(1) All source code and lab report PDF must be submitted via Mercurial. Place the source files inside the lab10 folder that was previously created.
Lab Part 1 - Arithmetic
Write a complete MIPS program that calculates the equation shown below (in C):
int main()
{
int A=15;
int B=10;
int C=5;
int D=2;
int E=18;
int F=-3;
int Z=0;
Z = (A-B) * (C+D) + (E-F) - (A/C);
}
A-F can be stored in temporary registers. However, the final result Z must be an integer word stored in memory when your program finishes executing. It is also a good idea to write this C/C++ program and run it to be sure your assembly code uses the same operator precedence rules.
Lab Report:
(1) Take two screenshots of the MIPS register panel: one before your program runs, and one after your program finishes. Put the register panel in Decimal mode (right-click) so it is easy to see register values.
(2) Take two screenshots of the MIPS memory panel (data tab): one before your program runs, and one after your program finishes. Put the memory panel in Decimal mode (right-click), so it is easy to see memory values. In the after-execution capture, circle the memory location (not register) that contains the final calculated value of Z.
Lab Part 2 - Branches
Write a complete MIPS program that implements the same algorithm shown below (in C):
int main()
{
// Note: I should be able to change
// the values of A, B, and C when testing
// your code, and get correct output each time!
// (i.e. don't just hardwire your output)
int A=10;
int B=15;
int C=6;
int Z=0;
if(A > B || ((C+1) == 7))
Z = 1;
else if(A < B && C > 5)
Z = 2;
else
Z = 3;
switch(Z) {
case 1:
Z = -1;
break;
case 2:
Z = -2;
break;
case 3:
Z = -3;
break;
default:
Z = 0;
break;
}
}
A-C and Z must be integer words in memory, both when the program begins and when the program ends. In between, they can be stored in registers.
Tip: Consult the Resources page for extra MIPS instructions (or pseduo-instructions) to make the task of comparing values and branching easier.
Lab Report:
(3) Take two screenshots of the MIPS register panel: one before your program runs, and one after your program finishes. Put the register panel in Decimal mode (right-click) so it is easy to see register values.
(4) Take two screenshots of the MIPS memory panel (data tab): one before your program runs, and one after your program finishes. Put the memory panel in Decimal mode (right-click), so it is easy to see memory values. In the after-execution capture, circle the memory location (not register) that contains the final calculated value of Z.
Lab Part 3 - Loops
Write a complete MIPS program that implements the same algorithm shown below (in C):
int main()
{
int Z=4;
int i;
for(i=0; i<=21; i=i+3) {
Z++;
}
do {
Z++;
} while (Z<100);
while(i > 0) {
Z--;
i--;
}
}
I and Z must be integer words in memory.
Lab Report:
(5) Take a screenshot of the MIPS register panel after your program finishes. Put the register panel in Decimal mode (right-click) so it is easy to see register values.
(6) Take a screenshot of the MIPS memory panel (data tab) after your program finishes. Put the memory panel in Decimal mode (right-click), so it is easy to see memory values. Circle the memory location (not register) that contains the final calculated value of Z.
Lab Part 4 - Arrays
Write a complete MIPS program that implements the same algorithm shown below (in C):
int main() {
int A[5]; // Empty memory region for 5 elements
int B[5] = {1,2,4,8,16};
int i;
for(i=0; i<5; i++) {
A[i] = B[i] - 1;
}
i--;
while(i >= 0) {
A[i]=(A[i]+B[i]) * 2;
i--;
}
}
A and B must be arrays of integer words in memory, but i can be a register.
Lab Report:
(7) Take a screenshot of the MIPS register panel after your program finishes. Put the register panel in Decimal mode (right-click) so it is easy to see register values.
(8) Take a screenshot of the MIPS memory panel (data tab) after your program finishes. Put the memory panel in Decimal mode (right-click), so it is easy to see memory values. Circle the final values of array A.
Lab Part 5 - I/O, Loops, and Arrays
Write a complete MIPS program that implements the same algorithm shown below (in C):
int main()
{
char string[256];
int i=0;
char *result = NULL; // NULL pointer is binary zero
// Obtain string from user, e.g. "Constantinople"
scanf("%255s", string);
// Search string for letter 'm'.
// Result is pointer to first m (if it exists)
// or NULL pointer if it does not exist
while(string[i] != '\0') {
if(string[i] == 'm') {
result = &string[i];
break; // exit from while loop early
}
i++;
}
if(result != NULL)
printf("First match at address %d\n", result);
else
printf("No match found\n");
}
The array of characters is an array of bytes, not words! The result pointer must be stored in memory when the program finishes.
10 points extra credit: write code to convert the value of 'result' into a hexadecimal string and print it out.
Lab Report:
(9) Take a screenshot of the MIPS memory panel (data tab) after your program finishes. Put the memory panel in Hex mode (right-click), since Decimal mode will not allow us to distinguish between bytes. Circle two things: the final value of the pointer 'result' in memory, and the corresponding location that result points to. Does that location in memory contain the ASCII code for the character 'm'? (If not, you had better check your work!)
Lab Report - Wrapup:
(1) What was the best aspect of this lab?
(2) What was the worst aspect of this lab?
(3) How would you suggest improving this lab in future semesters?