Question

In: Computer Science

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:" << endl;

for (int i=0; i<10; i++)

{

     If (x[i] == occur)

                                     {       

cout << i << endl;

                                                count ++;            

                                  }

}

cout << "Number of occurrences found:" << endl;

                                cout << count;

return 0;

}

Solutions

Expert Solution

Please up vote ,comment if any query .Thanks for question , Be safe .

Note : check attached image for output ,code compiled and tested in MARS MIPS simulator.

Program Plan :

  1. reserve space of 40 bytes in data section .
  2. declare all string to print in program
  3. run a loop to get input inside array
  4. now get occurence input
  5. run a loop and count how many time occurences found .
  6. print occurences .
  7. terminate program .

Program :

.data#variable section
   x : .space 40           #size 10 and word takes 4 bytes so 10*4 =40 bytes space reserved
   #string declaration
   arrayInput:       .asciiz "Type in array numbers:\n"
   occurencePrompt : .asciiz "Type in occurrence value:\n"
   occurenceIndex : .asciiz "Occurrences indices are:\n"
   occurenceFound : .asciiz "Number of occurrences found:\n"
  
  
   .text #code section
  
       jal main #call main function of program
      
       #terminate program
       li $v0,10 #syscall 10 to terminate program
       syscall
.globl main #declare main as global
main: #main function start from here
   la $a0,arrayInput #$a0=address of arrayInput String
   li $v0,4 #syscall 4 to print string
   syscall
  
   li $t0,0 #loop count i =0 $t0(i)=0
   la $s0,x #$s0=address of x array
  
    loop:#loop label
        #$t0==10(length of array) go to endOfLoop label
        beq $t0,10,endOfLoop
       
        li $v0,5 #read integer input using syscall 5
        syscall #input stored in $v0
       
        mul $t1,$t0,4 #t1=$t0(loop count i)*4 = 0*4
        add $t1,$t1,$s0 #t1=$t1+base address of array x
        #store value of $v0(user input) at address stored in $t1 register
        sw $v0,0($t1) #store word sw register ,address
           
        #$t0=$t0+1
        addi $t0,$t0,1 #increment i loop count
        j loop #jump to loop label
       
endOfLoop: #endof loop label
      la $a0,occurencePrompt #$a0=address of occurencePrompt string
      li $v0,4 #syscall 4 to print string
      syscall
     
      li $v0,5 #read user input occurence saved in $v0
      syscall
     
      addi $s1,$v0,0 #occurence saved in $s1 from $v0
     
       la $a0,occurenceIndex #a0=address of occurenceIndex
       li $v0,4
       syscall
      
       li $t0,0 #loop index i =0   
       li $t1,0 #count occurence =0
      
   secondLoop:
           beq $t0,10,printOccurence#if loop indexi ==10(length of array) go to printOccurence label
          
           mul $t2,$t0,4 #$t2=$t0*4 (4=word offset)
           add $t2,$t2,$s0 #$t2=$t2+base address of array x
          
           lw $t3,0($t2) #load value from x[$t0] into $t3
          
           bne $s1,$t3,skip #if occurence not equal to x[$t0]($t3) go to skip label
           #else
           addi $t1,$t1,1 #t1(count)=$t1(count)+1
          
       skip: #skip label
           addi $t0,$t0,1 #increment loop index i
           j secondLoop #jump to secondLoop
      
         
       printOccurence:#label
              la $a0,occurenceFound #$a0=address of occurenceFound string
              li $v0,4 #syscall 4 to print string
              syscall
     
              addi $a0,$t1,0 #move value of count into $a0=t1(count)
              li $v0,1 #syscall 1 to print integer
              syscall
             
      jr $ra #return to address from main function called
             
             
     
     

Output :

Please up vote ,comment if any query .


Related Solutions

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 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                      ...
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...
Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main()...
Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main() { int i = 2, j = 2 + i, k; k = i * j; printf("%d\n", k + j); return 0; } 2) #include <stdio.h> int main() { int i = 2, j = 2 + i, k = j / 2; if (k == 1) { printf("%d\n", j) k = k * 2; if ( k == j) { printf("%d\n|, j); }...
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,...
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; }
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...
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);
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"; } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT