You are here: Home / Past Courses / Fall 2012 - ECPE 170 / Labs / Lab 8: MIPS Assembly Programming (Basic)

Lab 8: 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.

 

Pre-Lab

There is no pre-lab activity for this lab.

 

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.

 

Programming Requirements (for all exercises):
(1) File names: Choose names for your assembly source code 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 lab08 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=10;
int B=15;
int C=5;
int D=2;
int E=7;
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 in memory when your program finishes executing.

Lab Report:
(1) Take two screenshots of the MIPS register panel: before your program runs, and 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): before your program runs, and after your program finishes.   Put the memory panel is Decimal mode (right-click), so it is easy to see memory values.  In the after-execution capture, circle the memory element 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=5;
int Z=0;

if(A > B && C >= 5)
Z = 1;
else if( A < B || ((C+1) == 7))
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.

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: before your program runs, and 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): before your program runs, and after your program finishes.   Put the memory panel is Decimal mode (right-click), so it is easy to see memory values.  In the after-execution capture, circle the memory element 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=5;

for(int i=0; i<10; i++) {
Z++;
}

while(Z > 0) {
Z--;
}

do {
Z++;
} while (Z<10);
}

 

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,3,4,5};
int C=12;
int i;

for(i=0; i<5; i++)
{
A[i] = B[i] + C;
}

i--;
while(i >= 0)
{
A[i]=A[i]*2;
i--;
}
}

A-C 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 is 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 t.
// Result is pointer to first t (if it exists)
// or NULL pointer if it does not exist
while(string[i] != '\0')
{
if(string[i] == 't')
{
result = &string[i];
break; // exit from while loop early
}
i++;
}

if(result != NULL)
printf("Matching character is %c\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.

Lab Report:
(7) Take a screenshot of the MIPS memory panel (data tab) after your program finishes.   Put the memory panel is 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 't'? (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?