Question

In: Computer Science

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 = a + c;
        c = a + d;

        if( b < 10 ) {
                b++;
                c--;
        }else{
                b--;
                c++;
        }

        a = c + b;
        b = c + d;

        if(b < c && b > a){
                d = a + b;
        }else if (b > c || c < a){
                d = b + c;
        }

        cout << "Value of a: " << a << endl;
        cout << "Value of b: " << b << endl;
        cout << "Value of c: " << c << endl;
        cout << "Value of d: " << d << endl;
        return 0;
}

Here is what was give.

.data
endl: .asciiz "\n" # used for cout << endl;
albl: .asciiz "Value of a: " # label for a
blbl: .asciiz "Value of b: " # label for b
clbl: .asciiz "Value of c: " # label for c
dlbl: .asciiz "Value of d: " # label for d
.text

# a --> $s0
# b --> $s1
# c --> $s2
# d --> $s3
main:

  

(fill in here)

exit:
la $a0, albl # puts albl into arg0 (a0 register) for cout
addi $v0, $0, 4 # puts 4 in v0 which denotes we are printing a string
syscall # make a syscall to system
  
move $a0, $s0 # puts a into arg0 (a0 register) for cout
addi $v0, $0, 1 # puts 1 in v0 to denote we are printing an int
syscall # make a syscall to system

la $a0, endl # puts the address of the string endl into a0
addi $v0, $0, 4 # puts 4 into v0 saying we are printing a string
syscall

la $a0, blbl # puts blbl into arg0 (a0 register) for cout
addi $v0, $0, 4 # puts 4 in v0 which denotes we are printing an string
syscall # make a syscall to system
  
move $a0, $s1 # puts b into arg0 (a0 register) for cout
addi $v0, $0, 1 # puts 1 in v0 to denote we are printing an int
syscall # make a syscall to system

la $a0, endl # puts the address of the string endl into a0
addi $v0, $0, 4 # puts 4 into v0 saying we are printing a string
syscall

la $a0, clbl # puts clbl into arg0 (a0 register) for cout
addi $v0, $0, 4 # puts 4 in v0 which denotes we are printing a string
syscall # make a syscall to system
  
move $a0, $s2 # puts c into arg0 (a0 register) for cout
addi $v0, $0, 1 # puts 1 in v0 to denote we are printing an int
syscall # make a syscall to system

la $a0, endl # puts the address of the string endl into a0
addi $v0, $0, 4 # puts 4 into v0 saying we are printing a string
syscall

la $a0, dlbl # puts dlbl into arg0 (a0 register) for cout
addi $v0, $0, 4 # puts 4 in v0 which denotes we are printing a string
syscall # make a syscall to system
  
move $a0, $s3 # puts d into arg0 (a0 register) for cout
addi $v0, $0, 1 # puts 1 in v0 to denote we are printing an int
syscall # make a syscall to system

la $a0, endl # puts the address of the string endl into a0
addi $v0, $0, 4 # puts 4 into v0 saying we are printing a string
syscall

addi $v0,$0, 10
syscall

Solutions

Expert Solution

Got your main code here

li   $s0, 5           # load value 5, into $s0 [i.e, a]
li   $s1, 6
li   $s2, 7
li   $s3, -1
li   $t0, 10

slt      $t1, $s0, $t0       # is a<10?
beq   $t1, 0, ELSE1       # if no, then go to else part
add   $s0, $s0, 1           # if yes, then increment a by 1
j         ENDIF1                # end of if
ELSE1 :
   addi   $s0, $s0, -1      # decrement a by 1
ENDIF1 :

add   $s3, $s0, $s2       # d=a+c
add   $s2, $s0, $s3       # c=a+d

slt      $t1, $s1, $t0       # is b<10?
beq   $t1, 0, ELSE2       # if no, then go to else part
add   $s1, $s1, 1          # if yes, then increment b by 1
addi   $s2, $s2, -1       # and decrement, c by 1
j   ENDIF2                    # end of if
ELSE2 :
   addi   $s1, $s1, -1       # decrement b by 1
   add   $s2, $s2, 1       # and increment, c by 1
ENDIF2 :

add   $s0, $s2, $s1       # a=c+b
add   $s1, $s2, $s3       # b=c+d

slt      $t1, $s1, $s2        # is (b<c)?
slt      $t2, $s0, $s1       # is (a<b)?
beq   $t1, 0, ELSEIF       # if false, then go to ELSEIF
beq   $t2, 0, ELSEIF       # if false, then go to ELSEIF
add   $s3, $s0, $s1       # if true, d=a+b
j         DONE                   # DONE
ELSEIF :
   slt       $t1, $s2, $s1       # is (c<b)?
   slt       $t2, $s2, $s0       # is (c>a)?
   beq   $t1, 1, LABEL       # if true, goto LABEL
   beq   $t2, 1, LABEL       # if true, goto LABEL
   j         DONE           # if false, then DONE
LABEL:
   add       $s3, $s1, $s2     # d=b+c
DONE :


Related Solutions

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++;
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 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,...
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.
Translate the C function code below to the MIPS True Assembler Language code (machine instructions only)....
Translate the C function code below to the MIPS True Assembler Language code (machine instructions only). The function code should follow the conventions for MIPS function calls including passing parameters and returning results. Your function code must be written with the minimum number of machine instructions to be executed and without any use of MIPS pseudo-instructions. Myfunction(unsigned int a, unsigned int b, unsigned int c) { int i=0; while (a > c) { a /= b; i++; } return i;...
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 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                      ...
4. Translate the following C code to MIPS assembly (in two separate files). Run the program...
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;} Submission file: Lab4_4a.asm and Lab4_4b.asm
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT