In: Computer Science
1) Provide the type and assembly language instruction for the following binary value: 0000 0010 0001 0000 1000 0000 0010 0000 (two)
2) Provide the type, assembly language instruction, and binary representation of instruction described by the following MIPS fields: op=0, rs=3, rt=2, rd=3, shamt=0, funct=34
3)For the following C statement, write a minimal sequence of MIPS assembly instructions that does the identical operation. Assume $t1 = A, $t2 = B, and $s1 is the base address of C. A = C[0] << 4;
4) Consider the following MIPS loop:
LOOP: slt $t2, $0, $t1
beq $t2, $0, DONE
subi $t1, $t1, 1
addi $s2, $s2, 2
j LOOP
DONE:
4a) Assume that the register $t1 is initialized to the value 10. What is the value in register $s2 assuming $s2 is initially zero?
4b) For each of the loops above, write the equivalent C code routine. Assume that the registers $s1, $s2, $t1, and $t2 are integers A, B, i, and temp, respectively.
4c) For the loops written in MIPS assembly above, assume that the register $t1 is initialized to the value N. How many MIPS instructions are executed?
1) Provide the type and assembly language instruction for the
following binary value:
0000 0010 0001 0000 1000 0000 0010 0000 (two)
Answer:----------
000000 | 10000 | 10000 | 10000 | 00000 | 100000 |
Op=0 | rs = 16 | rt = 16 | rd = 16 | shamt=0 | funct =0x20 |
(add) | ($s0) | ($s0) | ($s0) | (add) |
Thus this is a R-type instruction: add $s0,$s0,$s0
2) Provide the type, assembly language instruction, and binary
representation of instruction described by the following MIPS
fields: op=0, rs=3, rt=2, rd=3, shamt=0, funct=34
Answer:----
000000 | 00011 | 00010 | 00011 | 00000 | 100010 |
Op=0 | rs = 3 | rt = 2 | rd = 3 | shamt=0 | funct =34 |
($v1) | ($v0) | ($v1) | (sub) |
So,
Type of instruction:--------- R-type
instruction
Assembly language instruction :----------- sub
$v1, $v0 , $v1
3)For the following C statement, write a minimal sequence of MIPS
assembly instructions that does the identical operation. Assume $t1
= A, $t2 = B, and $s1 is the base address of C. A = C[0] <<
4;
Answer:----
Assume the base address of C is in $s1 and that A is in $s2. Use
the minimum number of registers. Do not destroy the
contents of $s1 or $s2.
A = C[0] <<
4;
lw $t1, 0($s1) ---------------------- # loads
C[0] into A
sll $t1 ,$t1, 4 --------------------------# shift left by 4
bits contents of $t1
sw $t1, 0($s2) ---------------------
# store C[0] << 4 into A
4) Consider the following MIPS loop:
LOOP: slt $t2, $0, $t1
beq $t2, $0, DONE
subi $t1, $t1, 1
addi $s2, $s2, 2
j LOOP
DONE:
4a) Assume that the register $t1 is initialized to the value 10.
What is the value in register $s2 assuming $s2 is initially
zero?
Answer:----
LOOP:
slt $t2, $0, $t1 ------------------------- # if $t1 > 0 then $t2
= 1 else $t2 = 0
beq $t2, $0, DONE ------------------- # if $t2 = 0 then go to
DONE
subi $t1, $t1, 1-------------------------- # $t1 = $t1 -1
addi $s2, $s2, 2 ------------------------- # $s2 = $s2 + 2
j LOOP --------------------------- # Go to LOOP
DONE:
Number of loop executions:
$t1 at top = 10;
$t1 at bottom = 9
...............…
t1 at top = 1; $t1 at bocom = 0 => 10
executions => s2 = 2x10 =
20
4b) For each of the loops above, write the equivalent C code
routine. Assume that the registers $s1, $s2, $t1, and $t2 are
integers A, B, i, and temp, respectively.
Answer:----
while(i > 0){
i= i // Decrement i by one during each iteration of the loop (sub
$t1, $t1, 1)
B = B + 2 // Increment B by 2 each time (addi $s2, $s2, 2)
} //exit loop
4c) For the loops written in MIPS assembly above, assume that
the register $t1 is initialized to the value N. How many MIPS
instructions are executed?
Answer:----
Base case: N= 0. If N=0, only 2 MIPS instructions are executed:
LOOP: slt $t2, $0, $t1 -------------------------- # 0 == 0;
here, $t2 = 0 [#1]
beq $t2, $0, DONE ------------------------------ # 0 == 0; go to
DONE [#2]
sub $t1, $t1, 1
addi $s2, $s2, 2
j LOOP
DONE:
For any value of N > 0, (N full loops + 2
lines) of MIPS instructions must be executed; each full loop
contains 5 MIPS instructions. Therefore, if the register $t1 is
initialized to the value N, a total of (5*N)+2 MIPS
instructions are executed.