Question

In: Computer Science

Convert the C program shown below to MIPS program. You may choose the $t registers, but...

Convert the C program shown below to MIPS program. You may choose the $t registers, but must correctly apply the $a and $v registers used in function calls. Please do not include an exit syscall in your MIPS solution. Hint: review jr and jal instructions.

int fun( int a, int b ) { if ( a >= b )

           return b + 2*a;
      else

return b + a;

}
void main() {

}

int a = 10;
int b = 20;
int c = fun( a, b );

Solutions

Expert Solution

Greetings!!

.text
main:
#load arguements into a0 and a1
addi $a0,$0,10
addi $a1,$0,20
#function call
jal fun
#termination
addi $v0,$0,10
syscall
#function definition
fun:
blt $a0,$a1,else
sll $a0,$a0,1       #2*a
add $v0,$a0,$a1       #b+2*a into v0 for return
jr $ra
else:
add $v0,$a0,$a1       #a+b into v0 for return
jr $ra

Output:

Hope this helps


Related Solutions

Translate the following C program into MIPS assembly language, allocate registers as you need, but document...
Translate the following C program into MIPS assembly language, allocate registers as you need, but document the allocation. int A[10]; // assume initialized elsewhere int sum = 0; int I = 0; while (i<0) { sum += A[i++]; sum+= 2 ;} plz do not copy from other answer, thx.
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:" <<...
For the registers values shown below, what are the values of $t2 & $t3, for the...
For the registers values shown below, what are the values of $t2 & $t3, for the following sequence of instructions? $s0=0xFFFF0011 $s1=0x00002136 sll $t2, $s0, 2 and $t3, $t2, $s1
In this project you will be writing a C++ program that uses a stack to convert...
In this project you will be writing a C++ program that uses a stack to convert an evaluate infix expression. Compilers often use this type of parsing. Note that the infix expression might not be syntactically correct. Before evaluating an infix expression, you need to check if its balanced, then convert it to a postfix expression then finally evaluate the postfix. You need to implement three methods: 1. public static Boolean checkBalance(String infixExpression) 2. public static String convertToPostfix(String infixExpression) 3....
Convert the following C++ code into MIPS assembely. For testing I will be change the values...
Convert the following C++ code into MIPS assembely. For testing I will be change the values for q,y,x with few different values. //q -> $s0 //y -> $s1 //x -> $s2 int main(){ int q = 5; int y = 17; int x = 77; if ( q < 10){ cout << "inside if"; } elseif ( x > 0 || y < 10) { cout << "inside elseif"; } else { cout << "inside else"; } }
Write a program in MIPS assembly language to convert an ASCII number string containing positive and...
Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a nullterminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. v import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else...
Write a C program to create a series of processes, as shown below in the diagram....
Write a C program to create a series of processes, as shown below in the diagram. P |------ ---- > c1 |------------>c2                              |------- ------>c3 ---------->c4                             In other words, the parent process p creates process c1, c1 creates c2 and c3, and c3 creates c4. A sample run of the program shows Your program should output something similar to what is shown above. You could optionally use wait/waitpid/sleep/exit in your program. Comment your code to show where you created...
Write a C program to create a series of processes, as shown below in the diagram....
Write a C program to create a series of processes, as shown below in the diagram. P - ------------------ > c1 ------------>c3    |                                          |    |------------------> c2 |----- >c4 In other words, the parent process p creates processes c1 and c2, c1 creates c3 and c4.
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT