KNE345 DESIGN OF MICROPROCESSORS AND MICROCONTROLLERS
LAB 1 – MIPS COMPILING AND SIMULATION
1. Overview
In this lab you will use MARS to simulate some MIPS assembly programmes that you have written by manually compiling some C code.
The objectives of this assignment are:
1. Gain an understanding of the MIPS Instruction set Architecture (ISA),
2. Familiarise you with a simulator (MARS),
3. Give you an opportunity to practice assembly programming,
4. Teach you standard assembly programming and compiling convention.
2. Pre-Lab
Before the lab you will need to install MARS on your computer. You can find MARS here: https://courses.missouristate.edu/KenVollmar/MARS/download.htm.
3. Part 1
1.1 Compiling from C to MIPS
Compile the following C code segments into MIPS assembly (save each in a separate file).
1. Write a simple program to include the MIPS code corresponding to the following C segment:
g = h + A[i]
The base of array A is held in register $S3, g, h and i are in registers $s1, $s2 and $s4.
2. Write a simple program to include the MIPS code corresponding to the following C segment:
Draw a flowchart for the MIPS code. (Hint use bne). Use $s0, $s1, $s2 $s3 and $s4 for f, g, h, i and j.
3. Write a simple program to include the MIPS code corresponding to the following C segment:
Array A is an array of 100 elements. g, h, i and j are associated with $s1 to $s4 respectively. The base of array A is held in $s5.
4. Write a simple program to include the MIPS code corresponding to the following C segment:
COLLEGE OF SCIENCES AND ENGINEERING
if(i==j)
f = g + h;
else
f = g – h;
loop:
g = g +A[i];
i = i + j;
if (i != h) goto loop;
5.
i, j and k correspond to registers $s3, $s4 and $s5. The base address of array save is held in $s6.
Write a simple program to include the MIPS code corresponding to the following C segment:
f thru k corresponds to $s0 to $s5 and $t2 may contain 4
Run your code
KNE345 DESIGN OF MICROPROCESSORS AND MICROCONTROLLERS
while(save[i] == k)
i = i + j;
LAB 1 – MIPS COMPILING AND SIMULATION
COLLEGE OF SCIENCES AND ENGINEERING
switch (k) {
case 0: f = i + j; break
case 1: f = g + h; break
case 2: f = g + h; break
case 3: f = i – j; break
}
1.2
Open MARS and choose two of your compiled programmes from above.
Click on the assemble button in the toolbar to assemble your programme. The execute tab should appear with your code in it ready to run.
The Basic column of your loaded program may not look the same as what you wrote since some of the instructions you use may be macros or psudoinstructions and MARS translates them to bare machine instructions. The original code is displayed in the Source column.
What is the starting address of your main routine?
Single-step the code from start until it reaches the first instruction of your main routine.
Set a breakpoint in your program by selecting the checkbox beside the row of your instruction.
Continue tracing through the program execution by using single stepping. Pay attention to the registersintheright-handpane,andnoticewhatishappeningtothePC(programcounter). Turn in your program code as well as a screenshot or similar verification showing your program works.
4. Part 2
Following is an example of a MIPS procedure call. Information that may be lost is stored on the stack. Note the convention of who (the caller or the callee) saves the information.
.text
.globl main
main:
subu$sp,$sp,32 # Example of code required by calling protocol sw$ra,20($sp) # for saving registers (uses stack). sw$fp,16($sp)
KNE345 DESIGN OF MICROPROCESSORS AND MICROCONTROLLERS
LAB 1 – MIPS COMPILING AND SIMULATION
addu $fp,$sp,28
la$t0, yoursubroutinecode jalr $t0
lw$ra,20($sp) lw$fp,16($sp) addiu $sp,$sp,32 j $ra
yoursubroutinecode: ***
*************
Compile the following C code into MIPS assembly language using the conventions of the above MIPS code, and make sure x is stored in memory such that is must be loaded by each function that uses it.
void Fred()
{ x=x+1;
Bill(); }
void Bill()
{ x=x+2;
Charlie(); }
void Charlie()
{ x=x+3;
}
int x=0;
main()
{ Fred();
}
Address the following in your report.
1. What is the starting address of your main routine? Single-step the code from start until it reaches the first instruction of your main routine. Note that R31 has been modified.
2. Continue tracing through the program execution by using single stepping and breakpoints. What are the registers SP and FP used for? Note the values on the stack when the first instruction of the procedures is about to be execute.
3. Disable the pseudo-instructions by turning off the “Permit extended (pseudo) instructions and formats” option in the Settings menu and try to assemble your program. MARS will let you know if there is a problem. Pseudo-instructions, will need to be converted. Rewrite your code without Pseudo-instructions and verify by assembling and running it in MARS.
COLLEGE OF SCIENCES AND ENGINEERING
KNE345 DESIGN OF MICROPROCESSORS AND MICROCONTROLLERS
LAB 1 – MIPS COMPILING AND SIMULATION
4. Turn on “Delayed branching” in the settings menu. This may impose delayed jumps and branches if a branch instruction’s condition is met. Run your code and take notice of the difference.
5. Part 3
Consider the following code; how can you modify it to make use of a delayed branch slot?
Answer:
Use your expertise with MARS to verify the two code examples are the same using the delayed branch options.
6. Submission
Submission is via the MyLO assignment “Lab 1 – MIPS Compiling and Simulation”. You must submit a report in PDF format including the following:
1. All compiled programme code from Part 1.
2. Compiled programme code from Part 2.
3. Discussion and answers relating to the four points from Part 2.
4. An explanation of how delayed branches work with reference to Part 3.
Please keep answers brief and to the point – any report content not directly related to the above 4 points will be ignored.
Loop: lw $2, 100($3)
addi $3, $3, 4
beq $3, $4, Loop
COLLEGE OF SCIENCES AND ENGINEERING
Loop: addi $3, $3, 4
beq $3, $4, Loop
lw $2, 96($3) # branch delay slot