43.9k views
4 votes
2-If we assume we place the following MIPS code starting at location 4000 in memory, what is the MIPS machine code for this code? (Please show binary and decimal value for each field) Assume that i, j and k correspond to registers $s3, $s4 and $s5 and the base of the array save is in $s6. Please also explain each instruction and specify its type (R format, I format, or J format). Finally convert the following code to C.

addi $s5,$zero,0
Loop: sll $t1,$s3,2
add
$t1,$t1,$s6
Iw $to,0($t1)
bne $to, $s5, Exit
sll $s4,$s3, 1
sub $s4,$s4,$10
j Loop
Exit

User Adam Ralph
by
8.5k points

1 Answer

2 votes

Final answer:

The MIPS machine code for the given code is provided, along with an explanation of each instruction and its format. The code is then converted to C.

Step-by-step explanation:

The MIPS machine code for the given code is as follows:

addi $s5, $zero, 0
Binary: 001000 10000 10001 0000 0000 0000 0000 0000
Decimal: 848977408

  1. sll $t1, $s3, 2
    Binary: 000000 00000 01100 01001 00000 00000 00001 00000
    Decimal: 30976
  2. add $t1, $t1, $s6
    Binary: 000000 01000 01100 10001 00000 100000
    Decimal: 67177344
  3. lw $t0, 0($t1)
    Binary: 100011 01100 01000 0000000000000000
    Decimal: 12582912
  4. bne $t0, $s5, Exit
    Binary: 000101 01000 10001 0000 0000 0000 0000 0000
    Decimal: 262983168
  5. sll $s4, $s3, 1
    Binary: 000000 00000 01100 10000 00000 000010
    Decimal: 2048
  6. sub $s4, $s4, $10
    Binary: 000000 10000 10000 10011 00000 100010
    Decimal: 16912976
  7. j Loop
    Binary: 000010 00000000000000000000001111
    Decimal: 671088592
  8. Exit
    Binary: 000000 00000 00000 00000 00000 101000
    Decimal: 40

The given MIPS code consists of various instructions including addi (I format), sll (R format), add (R format), lw (I format), bne (I format), sub (R format), and j (J format).

The given code in C would be:

int save[];
int i, j, k;
k = 0;
while(true) {
j = i << 2;
j = j + save[j];
if(save[j] != k) {
break;
}
k = i << 1;
k = k - 10;
}
Exit:

User Asibahi
by
8.8k points