EE 361 Homework 4

Due Date: 9/19/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.. If you don't have your own computer, 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 (it's calle xspim).  The operation of PCSpim should be similar but the 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.

The following are preliminaries before running spim on wiliki or spectra (which you can skip if you are using PCSpim)

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

Once you have xspim (or PCSpim) going, you should have the following display.  (In the case of the xspim, it will be displayed on "wailana".)

Now notice that the window has five panels.

  1. Register Display: This displays all the registers in the MIPs microprocessor. Note that the general registers $0-$31 are shown as R0-R31. The registers are updated whenever your program stops running.
  2. Control Buttons: These buttons are used to control the simulator. These buttons will be discussed in greater detail later.
  3. Text Segments: This displays instructions of your program. This will also be discussed in more detail below.
  4. Data Segments: This shows the data stored in memory. For this homework, this panel is not important. However, it is important for most programs.
  5. SPIM messages: This displays all error messages for SPIM. If your program has syntax errors, it will show up here after your program has been loaded.

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, download the following program from the web, by clicking here and saving the file as test2.s.

#
# 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.

Second Run -- Single Stepping. This time we will use the single step feature.

Third Run -- Breakpoints. This time we will use breakpoints.

Problem 4 (1 pt)

Consider the following C program

main()
{
int addall(int n0, int n1, int n2, int n3, int n4, int n5);
int i;

i = addall(10,11,12,13,14,15);
}

addall(int x0, int x1, int x2, int x3, int x4, int x5)
{
return x0+x1+x2+x3+x4+x5;
}

After compiling the program using mcc, we get the following assembly language program  (Note that "li" is a psuedo instruction and can be implemented by an "addi" instruction.  For example, "li    $2,10" is to load register $2 with the constant 10.  It can be implemented using "addi $2,$0,10".  You can find the description of "li" in Appendix A along with all the other assembly language instructions.  Also note that "addu" and "subu" is just like "add" and "sub" so for this exercise you can treat them as the same.)

    .file    1 "test4.c"

 
# GNU C 2.5.7 [AL 1.1, MM 40] BSD Mips compiled by CC

 # Cc1 defaults:

 # Cc1 arguments (-G value = 8, Cpu = default, ISA = 1):
 # -quiet -dumpbase -O -fno-delayed-branch

gcc2_compiled.:
__gnu_compiled_c:
    .text
    .align    2
    .globl    main

    .loc    1 2
    .ent    main
main:
    .frame    $sp,32,$31        # vars= 0, regs= 1/0, args= 24, extra= 0
    .mask    0x80000000,-8
    .fmask    0x00000000,0
    subu    $sp,$sp,32
    sw    $31,24($sp)
    li    $2,0x0000000e        # 14
    sw    $2,16($sp)
    li    $2,0x0000000f        # 15
    sw    $2,20($sp)
    li    $4,0x0000000a        # 10
    li    $5,0x0000000b        # 11
    li    $6,0x0000000c        # 12
    li    $7,0x0000000d        # 13
    jal    addall
    lw    $31,24($sp)
    addu    $sp,$sp,32
    j    $31
    .end    main
    .align    2
    .globl    addall

    .loc    1 10
    .ent    addall
addall:
    .frame    $sp,0,$31        # vars= 0, regs= 0/0, args= 0, extra= 0
    .mask    0x00000000,0
    .fmask    0x00000000,0
    addu    $2,$4,$5
    addu    $2,$2,$6
    lw    $3,16($sp)
    lw    $8,20($sp)
    addu    $2,$2,$7
    addu    $2,$2,$3
    addu    $2,$2,$8
    j    $31
    .end    addall

Function addall has six input parameters x0, x1, ..., x5, which are passed via registers and the stack.  List the registers that are used to pass parameters, and denote exactly which parameters they pass, e.g., parameter xk is passed through register $R.  For the other parameters, indicate where they are in the stack (at the time addall  is being executed) by drawing a picture.