Question

In: Computer Science

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";
  }
}

Solutions

Expert Solution

Note: Input values, statement data and label names are arbitrary. These can be changed according the user's choice. Please refer to code snippets for a better understanding of comments and indentations.

IDE Used: MARS

Program Code

.data
   if_state : .asciiz "inside if"
   elseif_state : .asciiz "inside elseif"
   else_state : .asciiz "inside else"
  
.text
   # initialise registers
   li $s0, 11
   li $s1, -1
   li $s2, 77
  
   # check if $s0 < 9, if true then branch to
   # if segment
   blt $s0, 9, if_seg
  
   # elseif check $s1 > 0 or $s2 < 10
   # if anyone true then branch to elseif segment
   bgt $s1, 0, elseif_seg
   blt $s2, 10, elseif_seg
  
   # else directly branch to else segment
   else_seg :
       # print else statement
       li $v0, 4
        la $a0, else_state
        syscall  
       
        # end the progrsm
        li $v0, 10
        syscall
   
    # if segment label
    if_seg :
        # print the if statement
        li $v0, 4
        la $a0, if_state
        syscall  
       
        # end the progrsm
        li $v0, 10
        syscall
   
    # elseif segment branch      
    elseif_seg :
        # print the else if statement
        li $v0, 4
        la $a0, elseif_state
        syscall  
       
        # end the progrsm
        li $v0, 10
        syscall  
   
   

Code Snippets

Sample Output Run

for $s0 = 5, $s1 = 17, $s2 = 77

for $s0 = 11, $s1 = -1, $s2 = 5

for $s0 = 11, $s1 = -1, $s2 = 77


Related Solutions

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);
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++;
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; }
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...
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)...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following:...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following: I have made numerous attempts on my own to no avail, any assistance is appreciated. Also, template code for this solution is provided below: #include int moveRobots(int *, int *, int, int ); int getNew(int, int); int main() { int x[4], y[4], i, j, myX = 25, myY = 25, move, status = 1; // initialize positions of four robots x[0] = 0; y[0]...
Convert the attached C++ code to working Java code. Be judicious in the change that you...
Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment: #include <iostream> #include <string> using namespace std; class pizza { public: string ingrediants, address; pizza...
Convert the following to machine code and then back to MIPS instructions: 1) a. addi $s0,...
Convert the following to machine code and then back to MIPS instructions: 1) a. addi $s0, $zero, -15 b. slt $t0, $s0, $s1 c. beq $t0, $zero, LEEQ d. j GRT
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT