Assignment 8


This home work is based on all the material covered in the class, and Chapter 2 of new edition of Computer Organization and Design Book.

 

P1. (10 points) Give the machine language format for the following instructions classes: ld/st, arithmetic/logic with register as operands holders, arithmetic/logic with immediate data, branch class, jump. Explain each field clearly.

 

P2. (30 points) Consider the following code used to implement the instruction

sllv $s0, $s1, $s2

which uses the least significant 5 bits of the value in register $s2 to specify the amount register $s1 should be shifted left:

 

.data

mask:   .word 0xfffff83f

.text

start:     lw $t0, mask

lw $s0, shifter

and $s0,$s0,$t0

andi $s2,$s2,0x1f

sll $s2,$s2,6

or $s0,$s0,$s2

sw $s0, shifter

shifter: sll $s0,$s1,0

 

(a) Add comments to the code and write a paragraph describing how it works. Note that the two lw instructions are pseudo instructions that use a label to specify a memory address that contains the word of data to be loaded. Why do you suppose that writing “self-modifying code” such as this is a bad idea (and oftentimes not actually allowed)?

 

(b) Determine the instruction format for each instruction and the decimal values of each instruction field.

 

 

P3. (10 points) The following MIPS instruction sequence could be used to implement a new instruction that has two register operands. Give the instruction a name and describe what it does. Note that register $t0 is being used as a temporary.

 

srl $s1, $s1, 1 #

sll $t0, $s0, 31 # These 4 instructions accomplish

srl $s0, $s0, 1 # “new $s0 $s1”

or $s1, $s1, $t0 #

 

P4. (30 points) Write a MIPS assembly code for the following program segment. Do not use pseudo instructions.

 

sum = 0; /*sum can be assigned to register r4*/

for (i=0; i<n; i++) /* i can be assigned to register r5, n (=10) can be put is r6 */ sum=sum+array[i]; /* array address is 0x04000000 can be stored in register r7 */

 

P5. (20 points) Some C programmers do not understand the distinction between x = y and *x = *y. Assume x is associated with register $s0, y with $s1. Here are six MIPS instructions, labeled L1 to L6:

 

L1: add $s0, $s1, zero

 

L2: add $s1, $s0, zero

 

L3: lw $s0, 0($s1)

 

L4: lw $s1, 0($s1)

 

L5: sw $s1, 0($s0)

 

L6: lw s1, 0($s0)

 

Which is true? (“L4; L5” means L4 then L5)

 

A: L1 is x = y,            L6 is *x = *y?

 

B: L2 is x = y,             L3 is *x = *y?

 

C: L4; L5 is x = y,       L3 is *x = *y?

 

D: L2 is x = y,            L4 is *x = *y?

 

E: L2 is x = y,             L4; L5 is *x = *y?

 

F: L1 is x = y,             L4; L5 is *x = *y?