Question

In: Computer Science

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 implementation of function f, if you need to use registers $s0 through $s7, use the lower-numbered registers first.

Solutions

Expert Solution

1). ANSWER :

GIVENTHA :

To translate the fuction f to MIPS assembly code

#int f(int a, int b, int c, int d)
#{
#return func(func(a,b), func(b+c,d));
#}

f:
sub $sp, $sp, 20 #allocate memory on stack to save ra, b, c, d and s0. a is not needed to be saved
sw $ra, 0($sp) #save return address
sw $a1, 4($sp) # save b
sw $a2, 8($sp) #save c
sw $a3, 12($sp) #save d
sw $s0, 16($sp) #save s0, we will use to store intermediate results


#already $a0 has a and $a1 has b, so simply call func(a,b)
jal func
move $s0, $v0 #now s0 = func(a, b)


#now set up arguments for func(b+c, d)

#calculate b + c by loading their saved values from stack
#store b + c into $a0
lw $t0, 4($sp)
lw $t1, 8($sp)
add $a0, $t0, $t1


#store value of d into $a1
lw $a1, 12($sp)


#call func(b+c, d)
jal func
move $a1, $v0 #store the return value into $a1 to make next func() call

#now setup arguments for the final func( func(a,b), func(b+c, d)) call
#set $a0 = func(a,b). func(a,b) is avaialbe in $s0

move $a0, $s0

#already $a1 is setup to the value of func(b+c, d)
jal func

#now result is in $v0


#now restore all registers and return
lw $ra, 0($sp) #restore return address
lw $a1, 4($sp) # restore b
lw $a2, 8($sp) # restore c
lw $a3, 12($sp) #restore d
lw $s0, 16($sp) #restore s0,
add $sp, $sp, 20 #deallocate memory on stack

jr $ra


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);
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} 4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+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,...
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...
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;            &nbs
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;                                 int x[] = {18, 12, 6, 500, 54, 3, 2, 122};                 int i;                                 for (i=0; i<8; i++)                 {                                                 cout << x[i] << endl;                 }                 return 0; } below is my code: .data        str1: .ascii "Numbers:"     str2: .ascii "\n"    x: .word 18,12,6,500,54,3,2,122       .text                      ...
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:" <<...
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++;
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...
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; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT