# EE 361 Homework

Homework 2

Due Date: Sept 14 (wed.)

Assignment:

Problem A [2 pts] Convert each of the following lines of C language code to MIPS assembly language. Note that two of the six lines are done as examples, so you only need to do the other four (each counts half a point). The variables "k" and "j" are stored in registers \$3 and \$4, respectively. You may use register \$2 for temporary storage.

1. if (k == j) k = 0;

`        bne    \$3,\$4,Skip      #  if (k != j) goto Skip        add    \$3,\$0,\$0        #  k = 0;Skip:`

2. if (k != j) k = 0;

3. if (k > j) k = 0;

`        #  if (k <= j) goto Skip        slt   \$2,\$4,\$3      # if (k > j) then \$2 = 1                            # Otherwise, "k <= j" and \$2 = 0        beq   \$2,\$0,Skip    # if (k <= j) goto Skip        add   \$3,\$0,\$0Skip:`

4. if (k < j) k = 0;

5. if (k >= j) k = 0;

6. if (k <= j) k = 0;

Problem B [2 pt].  Consider the following C language for-loop:

`     for (i = 0; i < 100; i++) j = j + i;`
The following is a MIPS assembly language implementation of the for-loop assuming that \$3 and \$4 are used to store i and j, respectively.
`       add     \$3,\$0,\$0     # i = 0;Loop:  slti    \$1,\$3,100    # \$1 = 1 if i < 100       beq     \$1,\$0,Skip   # if \$1 == 0 (i.e., i >= 100) then skip for-loop       add     \$4,\$4,\$3     # j = j + i;       addi    \$3,\$3,1      # i++;       beq     \$0,\$0,Loop   # go back to beginning of for-loopSkip:`
Write the machine code for the MIPS assembly language instructions. You may use decimal numbers (positive and negative ones) to fill in the fields. Note that all instructions and their machine code versions can be found in the back cover of the textbook.

Problem C [2 pts]:  Implement the following C for loop in MIPS assembly language.  Assume that variables x1 and x2 are realized by, respectively, \$3 and \$4.  You may use \$1, \$2, \$5, and \$6 for temporary storage.  Be sure to put in comments to explain your code.
`x1 = 1;while (x1 < x2) {   x2 = x2 + x1;   x1 = x1 + 3;   }`

Problem D (3 pts total). The mcc compiler.

The MIPS processor has a compiler named "mcc".  Here's an assignment which uses the output from the compiler.  The following is a short C program called "test.c"

`main(){int x1, x2;for (x1 = 0; x1 < x2; x1++) x2 = 2*x2;}`

`To compile, the command would be mcc test.c`

This will create a new assembly language file "test.s", which is the compiled version of "test.c".   You can download the files, which are text files, here:  test.c and test.s

The assignment is to examine "test.s" using an editor (e.g., in windows you can use wordpad or notepad and in unix you can use pico or vi).

• labels, used in place of actual addresses, e.g., "\$L2".   Note that labels can be most any name except special names, e.g., instruction names such as "add", "lw", etc cannot be labels.  The mcc compiler generates its names automatically, so they have the form "\$Ln", where n is some number.
• machine instructions, e.g., "lw \$3,0(\$fp)"
• directives, which are instructions prefixed by periods ".". An example directive is ".text". Directives are not instructions to be executed by the CPU. They are instructions to be followed only by the assembler software.

• Question C.1 (1 pt). Write down three examples of directives. (You don't have to understand what they do.)
• Question C.2 (1 pt). Write down three examples of machine instructions. (Again you don't have to understand what they do.)
• Question C.3 (1 pt). Write down three example of labels (used as memory addresses).