EE 361 Homework 3
Homework 3
Due Date: 9/20 (fri.)
Assignment:
Problem A (1 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-loop
Skip:
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 B. (1 pt.)
Consider the following C function:
increasing(int i, int j, int k) /* returns 1 if i < j < k */
{ if (i < j && j < k) return 1;
else return 0;
}
Write a MIPS assembly language implementation of this function assuming
- "increasing:" is the label to start the function. Thus, a call to
the function is done by the instruction "jal increasing".
- The values of i, j, and k are passed to the function in registers
$3, $4, and$5, respectively.
- The function returns its value through register $2.
- You may use register $1 to store temporary values (such as the
outcome of a "slt" instruction).
Problem C. (1 pt). Do this problem after we cover stack
frames . Consider the following C function "positive" and the call
"k = positive(n)" within "main"
main()
int k, n;
.
.
k = positive(n);
.
.
}
int positive(int m)
{ if (m < 0) return 0;
else return m;
}
The following is an assembly language implementation of "positive". Notice
that it gets the parameter m from the stack frame. It is assumed that
the frame pointer ($fp) is pointing to the old-$fp value in the stack. Below
the old-$fpvalue is the old-$ra value, and below that is the parameter m.
Thus the offset from $fp to the parameter is +8. Also notice
that the result of the function is returned in $2.
# Assembly language implementation of "positive"
positive:
# Notice that we use registers $t0 and $t1 for
# temporary storage. We do not have
to save
# their values before using them because they
are
# caller saved registers.
lw $t0,8($fp)
# Load $t0 = m.
slt $t1,$t0,$0
# $t1 = 1 if m < 0, and is 0 otherwise
bne $t1,$0,Return0
move $2,$t0
jr $ra
Return0:
move $2,$0
jr $ra
Implement the line "k = positive(n)" in assembly language. Suppose
variables n and k are in registers $t1 and $t2, respectively. Also, notice
that the parameter n is passed through the stack, and a stack frame is used.
Be sure to save the $fp and $ra values before the jal instruction.
Then restore all appropriate register values properly.
Textbook Problem 3.6. You don't need to use the simulator as suggested
in the problem (1 pt)
Textbook Problem 3.9. (1 pt).
Objective: Introduction to mcc compiler.
Preliminaries:
- Open netscape. If you're using UNIX then you can run netscape in
background by typing:
netscape &
- telnet into wiliki
Problem C (5 pts). The mcc compiler.
For this problem , you will be using the mcc compiler to compile some
simple C programs. There are five subproblems to work on.
First, create the following simple C program:
main()
{
int x1, x2;
for (x1 = 0; x1 < x2; x1++) x2 = 2*x2;
}
and name it "test.c". Now compile the program by typing
mcc test.c
This will create a new assembly language file "test.s", which the compiled
version of "test.c". Now examine "test.s" using an editor, e.g., "vi".
Notice it is a mix of
- labels, used in place of actual addresses, e.g., "$L2"
- 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.
- Subproblem C.1. Write down three examples of directives. (You
don't have to understand what they do.)
- Subproblem C.2. Write down three examples of machine instructions.
(Again you don't have to understand what they do.)
- Subproblem C.3. Write down three example of labels (used as memory
addresses).
- Subproblem C.4. How many machine instructions are there in this
assembly language program?
Now save this program by renaming it "testP.s" (P for plain) by typing
mv test.s testP.s
The mcc compiler has an "-O" option which stands for "Optimization."
The compiler will run slower but will reduce the size of the resulting assembly
language program. Compile test.c using this option by typing
mcc -O test.c
Again this will create another assembly language program "test.s".
- Subproblem C.5. How many machine instructions are there in this
assembly language program?