Question

In: Computer Science

1. Translate the following code into MIPS code. B[i + 10] = B[i -2] + 40;...

1. Translate the following code into MIPS code.

B[i + 10] = B[i -2] + 40;

i = i + 10;
B[3] = B[i - 1];

a) Assume B is an array of integers (each integer takes 4 bytes). B's address
is stored at register $10. Also assume that the compiler associates the
variable i to the register $11.
b) Assume B is an array of characters (each character takes one byte). B's address
is stored at register $10. Also assume that the compiler associates the variable i to   the register $11.


2. Translate the following code into MIPS code.
       

B [0] = 5;

for (i = 1 ; i < 50 ; i = i + 2)

{

           

            B[i] =i + B[i-1];

}

Assume the compiler associates the variable i to the register $t0. Also, assume B is an array of integers and its address is stored at register $s1.   


3. Translate the following code into MIPS code.

           

            for (i=0; i<=5; i=i+1)

                        {

                        if (i != k)

                                    k=(k *2)-1;

                                    else

                                    k=(k *4)+1;

                        }

Assume the compiler associates the variables i and k to the registers $s0 and $s1, respectively.

4. Translate the following code into MIPS code.

Test (int i, int j)

                        {

                        int k;

                        k = Double(i+1) + Double (j-10)

                        return k;

                        }

Sub (int m)

                        {

                        int g;

                        g = m + m;

                        return g;

                        }

Assume the compiler associates the variable k to the register $s0. Assume the compiler associates the variable g to the register $t0.

Solutions

Expert Solution

Question 1

a) Translate

    la $10,B
#a0 hold the value of i-2
addi $4,$11,-2
#That add to $10 to get B[i-2] index
add $10,$0,$4
#a0 hold the value in index B[i-2]
lw $4,($10)
#a0 hold value of B[i-2]+40
addi $4,$4,40
#$8 hold value of i+10
addi $8,$11,10
#Get the base address of the array in $10
la $10,B
#Increment address to get B[i+10]
add $10,$0,$8
#store value into that address,B[i+10]=B[i-2]+40
sw $4,($10)
#Increment i value into i=i+10
addi $11,$11,10
#$4 store the value i-1
add $4,$11,-1
#Get base address of B
la $10,B
#address offset , means B[i-1]
add $10,$0,$4
#B[i-1] value in $4
lw $4,($10)
#Get base address in $10
la $10,B
#Change into B[3]
addi $10,$0,12
#store B[i-1] into B[3], B[3]=B[i-1]
sw $4,($10)

b) Translate

#Base address of B in $10
la $10,B
#a0 hold the value of i-2
addi $4,$11,-2
#That add to $10 to get B[i-2] index
add $10,$0,$4
#a0 hold the value in index B[i-2]
lb $4,($10)
#a0 hold value of B[i-2]+40
addi $4,$4,40
#$8 hold value of i+10
addi $8,$11,10
#Get the base address of the array in $10
la $10,B
#Increment address to get B[i+10]
add $10,$0,$8
#store value into that address,B[i+10]=B[i-2]+40
sb $4,($10)
#Increment i value into i=i+10
addi $11,$11,10
#$4 store the value i-1
add $4,$11,-1
#Get base address of B
la $10,B
#address offset , means B[i-1]
add $10,$0,$4
#B[i-1] value in $4
lb $4,($10)
#Get base address in $10
la $10,B
#Change into B[3]
addi $10,$0,3
#store B[i-1] into B[3], B[3]=B[i-1]
sb $4,($10)

-----------------------------------------------------------------------------------

Question 2

#Get base address of B
la $s1,B
#$t1=5
addi $t1,$0,5
#B[0]=5
sw $t1,($s1)
#$t0=i=1
addi $t0,$0,1
#For loop
loop:
#i<50
blt $t0,50,exit
#i-1 in $t2
addi $t2,$t0,-1
la $s1,B
#Get address of B[i-1]
add $s1,$0,$t2
#Value of B[i-1]
lw $t2,($s1)
#1+B[i-1]
addi $t2,$0,1
la $s1,B
#Get B[i] address
add $s1,$0,$t0
#B[i]=1+B[i-1]
sw $t2,($s1)
#i=i+2
addi $t0,$t0,2
#Repeat
j loop
exit:

----------------------------------------------------------------------

Question 3

#i=0
addi $s0,$0,0
#For loop
loop:
#i<=5
ble $s0,5,exit
# if (i != k)
bne $s0,$s1,if
# k=(k *4)+1;
mul $s1,$s1,4
addi $s1,$s1,1
#i=i+1
addi $s0,$s0,1
#Repeat
j loop
#k=(k *2)-1
if:
#k*2
mul $s1,$s1,2
#k=(k *2)-1
addi $s1,$s1,-1
#i=i+1
addi $s0,$s0,1
#return to for loop
j loop
exit:

-------------------------------------------------------------------------

Question 4

Sub (int m)

                        {

                        int g;

                        g = m + m;

                        return g;

                        }

Translate

#m in a0
jal Sub
Sub:
#g = m + m;
add $t0,$a0,$a0
#return value in v0
move $v0,$t0
#Return g
jr $ra

------------------------------------------------------------------------

Note

    Question 4's first part "Test" method, there is "Double()" is that means a new method or conversion.

     I have doubt in that, so i skipped that.


Related Solutions

Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
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...
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++;
I have to translate C++ code into MIPS. It is expected to have an output of:...
I have to translate C++ code into MIPS. It is expected to have an output of: Value of a: 25 Value of b: 31 Value of c: 18 Value of d: 49 Here is the C++ code: I need to translate this C++ into MIPS code. #include using namespace std; int main(void) { int a = 5; int b = 6; int c = 7; int d; d = -1; if ( a < 10){ a++; }else{ a--; } d...
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...
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; }
Question: In the following MIPS assembly code, translate all the instructions to their corresponding machine code...
Question: In the following MIPS assembly code, translate all the instructions to their corresponding machine code in hexadecimal format. This code is stored in the memory from address 0x2fff0004. Loop: lw $t0, 4($s0)             addi $t1, $t1, -15             sll $t1, $t1, 2             beq $t1, $s1, Exit             addi $s0, $s0, 4             j Loop Exit: …
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
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