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
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...
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...
Using double precision floating point arithmetic, write a( MIPS )program that will convert input temperatures in...
Using double precision floating point arithmetic, write a( MIPS )program that will convert input temperatures in Fahrenheit to Celsius. The program should prompt the user for a temperature in Fahrenheit and then print the corresponding temperature in Celsius. The formula for the conversion is: C = 5/9 * (F – 32)
Write a MIPS program that executes the statement: s = (a + b) – (c +...
Write a MIPS program that executes the statement: s = (a + b) – (c + 101), where a, b, and c are user provided integer inputs, and s is computed and printed as an output. Answer the following: a. Suppose the user enters a = 5, b = 10, and c = -30, what is the expected value of s? b. Which instruction in your program computed the value of s and which register is used? c. What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT