Question

In: Computer Science

Compile the following C code to MIPS assembly. a. Assume that i and j are stored...

Compile the following C code to MIPS assembly.

a. Assume that i and j are stored in register $s1 and $s2, respectively. i = i – 2 + j;

b. Assume base address of Array B and the variable i are stored in registers $s3 and $s1, respectively. B[1] = (B[0] x 4) - I;

Explain each step in detail!

my professor has the following answer: 1) addi $t0, $s1, -2 add $s1, $$t0, $s2

2) lw $t0, 0 ($s3) #load B[0] into a register sll $t0, $t0, 2 #shift left logical A[2] x 4 #(may have several different ways) sub $t0, $t0, $s1 # (B[0] x 4) - i sw $t0, 4($s0)

Can you explain in detail why?

Solutions

Expert Solution

(a)Register $s1 stores value of variable i
Register $s2 stores value of variable j
C code
i = i – 2 + j;
i = (i – 2) + j;

Here $t0 is temporary register, which stores result of intermediate values
MIPS code
addi $t0, $s1, -2    # $t0 = i+(-2) = i-2
add $s1, $$t0, $s2   #$s1 = $t0 + $s2 = (i-2) + j

(b)$s3 stores base address of Array B
$s1 = i
Here $t0 is temporary register, which stores result of intermediate values
C code
B[1] = (B[0] x 4) - i;

size of int = 4 bytes
each element in array take 4 bytes of memory, And memory is byte addressable(i.e each address has 1 byte of data)
Address of B[0] = (base address of Array B) + 0
Address of B[1] = (base address of Array B) + 4

shifting a binary number left by 1 bit , will result multipltying by 2 with number.
For example,
Let number = 0100 (Decimal value = 4)
shifting left by 1 bit, number = 1000 (Decimal value = 8)

MIPS code
lw $t0, 0 ($s3)    #load B[0] into a register
sll $t0, $t0, 2    #shift value at register $t0 by 2 bit left, $t0 = B[0] * 4
sub $t0, $t0, $s1    # (B[0] x 4) - i
sw $t0, 4($s0)       #store value of register $t0 in memory at address (base address of Array B) + 4

Therefore, B[1] = (B[0] * 4) - i

(B[0] * 4) This can also be done in this way
add $t0, $t0, $t0   //$t0 = B[0] + B[0] = 2*B[0]
add $t0, $t0, $t0   //$t0 = 2*B[0] + 2*B[0] = 4*B[0]


Related Solutions

4.Translate the following C code to MIPS assembly code. Assume that the value of i is...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray if (i > 10) MemArray[i] = 0; else MemArray[i] = -MemArray[i]; 6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds...
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g,...
1.) Translate the following C code to MIPS assembly code. 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   B[8] = A[i-j]; 2.Translate the following C code to MIPS assembly code. Assume that the values of v in $a0, k in $a1, temp in $t0.    // leaf procedure that...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this function is called with the parameter n = 5… int summarize(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += i; } return sum; } c) Compile the code using the ARM gcc 8.2 compiler. Add the –O1 compiler option, which asks the compiler to optimize for speed (at least to one level). What is...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this function is called with the parameter n = 5… int summarize(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += i; } return sum; } a) Accounting for delay slots, what is the dynamic instruction count of this routine? What is the dynamic instruction count if you add the –O1 compiler option, which asks the...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0...
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0 and initially containing 0 . Also assume base of array Arr is in $s3 while ( k < = 10 ) { Arr[k] = k ; k = k + 1; }
Translate the following C code to MIPS assembly. The main function and subfunction are translated to...
Translate the following C code to MIPS assembly. The main function and subfunction are translated to two separate .asm files. Finish the assembly code segment for the above requirement. int main() { int x=2; int y=1; int z=0; z=Subfunc(x,y); printf(“Value of z is: %d”, z); } int Subfunc(int x, int y) { int t1=0; t1=x+y+100; return t1;} File 1: .data str: .asciiz "The value of z:" .text #.globl main main: addi $s0, $0,2 #x addi $s1, $0,1 #y addi $s2,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT