Homework 4
Due Date: 9/27/02 (friday)
Assignment:
Problem 1. (2 pts) Consider the following C functions:
/* Returns the minimum value of array a[], which has length "length" */
minarry(int a[], int length)
{ int i, k;
k = a[0];
for (i=0; i<length; i++) k = min(k,a[i]);
return(k);
}
min(int i, int j)
{ if (i < j) return(i);
else return(j);
}
Write a MIPS assembly language implementation of the functions. You may assume that parameters are passed to the functions through registers $a0 and $a1, where $a0 is the first parameter and $a1 is the second. You may use $t0-$t9 for temporary storage, as well as the stack. Also assume that all return parameters are passed through $2.
Problem 2. (1 pt) Consider the following machine code of a program that starts at memory location 16. Disassemble the code giving an assembly language equivalent, complete with labels (you may use whatever names you want for your labels, e.g., "Skip:", "Loop:"). Note that the machine code is in "decimal representation", which shows the decimal values of each field.
+-----+-----+-----+-----+-----+-----+
| 8 | 0 | 3 | 4 |
+-----+-----+-----+-----+-----+-----+
| 5 | 0 | 3 | 3 |
+-----+-----+-----+-----+-----+-----+
| 0 | 2 | 3 | 1 | 0 | 34 |
+-----+-----+-----+-----+-----+-----+
| 8 | 3 | 3 | -1 |
+-----+-----+-----+-----+-----+-----+
| 2 | 5 |
+-----+-----+-----+-----+-----+-----+
Problem 3. (2 pts) For this part, you will be introduced to the MIPS simulator SPIM. The simulator will allow you to run MIPS assembly language programs. For additional reading, there is Appendix A.9 (page A-38 to A-49) which you can read if the following assignment is confusing. You may also go over Appendix A.10 which describes the MIPS assembly language. This goes into detail about different instructions, pseudo instructions, and assembler directives. Note that there are three questions to answer at the end.
The SPIM simulator is available on spectra and wiliki. There is also a free PC version that you can download I believe the PC version is called PCSpim.. The PC version is available on the EE PC Server on the PCs in Holmes 486. It will be in the folder labeled "spim".
The following instructions are for SPIM on spectra and wiliki, but the operation of SPIM on PCs should be similar. (However, format may not be the same. For example, instead of buttons, there may be pull-down menu options. Also instead of "loading" a assembly language file, you "open" it.)
Before running spin on wiliki (or spectra), make sure you have the correct path to xspim, which is in the ~ee361/bin. Your .cshrc file should have the following line somewhere:
set path=($path ~ee361/bin)
If you just entered it into your .cshrc file then you must restart by
logging out and relogging in.
Now we can start. On spectra and wiliki, you can invoke SPIM by typing:
xspim
A window will pop up, as shown below. Now if the you are running the simulator remotely then you'll have to direct the output of SPIM to your terminal. For example, suppose you're in the College of Engineering computer room, and you're at workstation "wailana". Then you can open a window, and telnet to wiliki. You should let wiliki know that you want the output of xspim to go to wailana. So you type at wiliki
setenv DISPLAY wailana:0.0
This will direct the output of wiliki to wailana. Next you have to prepare "wailana" to accept the output from wiliki (and to subsequently display it). So at "wailana", you type
xhost +
This tells wailana that anything that comes from the network should be displayed on the screen. To be more discriminating, you can type instead
xhost +wiliki
Now you're all set, and the xspim window (that's running on wiliki) will be displayed on "wailana".
Now notice that the window has five panels.
Text Segment This shows the instructions of your program. An example line in this panel is
[0x00400000] 0x8fa40000 lw $4,0($29) ; 89: lw $a0,0($sp)
Control Buttons
Let's run an assembly language program.
Loading an example program. First, create the following program using some editor such as vi (use another window), and call this program test2.s. (Alternatively, you can download the program from the web, by clicking here and saving the file as test2.s.)
In your program, you don't have to include the comments:
#
# This program multiplies $20 and $21, and puts the product in $22.
#
main:
move $22,$0 # This initializes $22 to zero.
move $23,$20 # $23 is a temp. reg., used as a counter.
loop:
beq $0,$23,quit # if the counter is zero, then quit
add $22,$22,$21 # $22 = $22 + $21
addi $23,$23,-1 # $23 = $23 - 1 (update counter)
# Note: "addi" is a new instruction
j loop
quit:
jr $31
First Run -- No Frills.
lw $4, 0($29)
addiu $5, $29, 4
addiu $6, $5, 4
sll $2, $4, 2
addu $6, $6, $2
jal 0x00400020 [main]
ori $2, $0, 10
Second Run -- Single Stepping. This time we will use the single step feature.
lw $4, 0($29)
addiu $5, $29, 4
addiu $6, $5, 4
sll $2, $4, 2
addu $6, $6, $2
jal 0x00400020 [main]
Before the program is executed. Note that the loop of the program is executed three times. Notice that the register values will change as the program is executed.
The program is completed after the jr $31 instruction is executed, and we jump back to ori $2, $0, 10, which is one of the "housekeeping instructions".
Notice that we have the option of stepping through multiple instructions. Try this exercise again, but using different step sizes to see what happens.
Third Run -- Breakpoints. This time we will use breakpoints.