CO101 Principle of Computer Organization
Assignment 2
Due: October 19, 2016
Name: Student Number:
1. The following problems deal with translating from C to MIPS. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively.
f = -g – A[6];
1) For the C statements above, what is the corresponding MIPS assembly code?
lw $s0, 24($s6)
sub $s0, $zero, $s0
sub $s0, $s0, $s1
2) For the C statements above, how many different registers are needed to carry out the C statement?
4 registers.
2. The following problems deal with translating from MIPS to C. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively.
sll $s2, $s4, 4
add $s0, $s2, $s3
add $s0, $s0, $s1
1) For the MIPS assembly instructions above, what is the corresponding C statement?
f = 16*j + i + g
2) For the MIPS assembly instructions above, rewrite the assembly code to minimize the number if MIPS instructions (if possible) needed to carry out the same function.
no change
3) How many registers are needed to carry out the MIPS assembly as written above? If you could rewrite the code above, what is the minimal number of registers needed?
5
sll $s0, $s4, 4
add $s0, $s0, $s3
add $s0, $s0, $s1
4
3. Show the single MIPS instruction or minimal sequence of instructions for this C statement: b = 25 | a; Assume that a corresponds to register $t0 and b corresponds to register $t1.
ori $t1, $t0, 25
4. Convert the following MIPS instructions to the corresponding machine codes. Assume the instructions are stored in memory with the address listed left.
start: beq $s0, $a0, next #memory address [0x00000000]
sub $v0, $a0, $s0 #memory address [0x00000004]
next: addi $s0, $s0, 1 #memory address [0x00000008]
j start #memory address [0x0000000C]
5. (Optional) Write a procedure, find_k, in MIPS assembly language. The procedure should take a single argument that is a pointer to a null-terminated string in register $a0. The find_k procedure should locate the first ‘k’ character in the string and return its address in register $v0. If there is no ‘k’ in the string, then find_k should return a pointer to the null character at the end of the string. For example, if the argument to find_k points to the string “hijklmn”, then the return value will be a pointer to the fourth character of the string.