EE 361 Homework 3

Due Date: 9/23/05 (fri)


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]);

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. For the function "minarray", the first parameter is an address that points to the beginning of array a[].  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. Read the instructions in Appendix A.9 (page A-38 to A-49) for SPIM.  Actually, the pages A-38 to A-43 are sufficient to get started.  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. 

For this problem, you will use a PC version of this simulator called PCSpim.  You can download a free copy to your own computer and find documentation about PCSpim by going to the following web site:  SPIM web site   The instructions in Appendix A.9 is for SPIM rather than PCSpim but they should be reasonably close to what you need to run PCSpim.  If you don't have your own computer, PCSpim is available on the EE PC Server on the PCs in Holmes 387.  Find a PC then go to "My Computer" -> "EE PC Server" -> Folder named "spim" -> "pcspim.exe".

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.
move $22,$0 # This initializes $22 to zero.
move $23,$20 # $23 is a temp. reg., used as a counter.
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
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

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

    .align    2
    .globl    main

    .loc    1 2
    .ent    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
    .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.