Question

MIPS Insertion program.........I could really use some help ASAP

I have this MIPS program and I'm having trouble with it. This program is user inputs numbers until zero and sorts and print the numbers in order. Please soove this issue. You can use any sorting algorithm except bubble sort.  Need it as soon as possible. Here is the code:


.data

num: .word 0

space: .byte ' '

 .text

 main:


  # la $t0, val # loads val into a register

  # li $t1, 0      #keeps track of how many numbers entered

 la $a0, num

 addi $a1, $0, 5 #length

 jal sort

 

 addi $a0, $v0, 0

 li $v0, 1

 syscall

 

  li $v0, 10

  syscall

  

sort:

    addi $t0, $0, 1 #i = 1

    li $v0, 5 #gets input

syscall

beq $v0, $zero print #checks if equal to 0

sw $v0, 0($a0)

addi $a0, $a0, 4 #changes pointer location

addi $a1, $a1, 1 #adds one to counter

j sort

Outerloop: 

    slt $t3, $t0, $a1

    beq $t3, $0, exit

    sll $t4, $t0, 2 #i*4

    add $t4, $t4, $a0 #base + offset

    lw, $t5, 0($t4) # t5 = value in code = a[i]

    addi $t1, $t0, -1 #j = t1 = i-1

Innerloop:

slt $t4, $t1, $0

bne $t4, $0, ExitInnerLoop

sll $t4, $t1, 2

add $t4, $t4, $a0 #a[j]

lw $t4, 0($t4)

slt $t6, $t5, $t4

beq $t6, $0, ExitInnerLoop

addi $t6, $t1, 1

sll $t6, $t6, 2

add $t6, $t6, $a0

sw $t4, 0($t6)

addi $t1, $t1, -1

j Innerloop


ExitInnerLoop:

addi $t6, $t1, 1

sll $t7, $t6, 2

add $t7, $t7, $a0

sw $t5, 0($t7)

addi $t0, $t0, 1

j Outerloop


print:

la $t0, val #loads val into regster $t0

li $t3, 0  #2nd counter =0

display:

lw $a0, 0($t0) 

li $v0, 1

syscall #prints number in list

addi $t3, $t3, 1

li $v0, 4

la $a0, space #prints space

syscall

beq $t3, $t1, exit

addi $t0, $t0, 4 #moves to next location

j display

exit:

li $v0, 10

syscall


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please rate the answer.

Thank You !


===========================================================================

#include<stdio.h>
#include<time.h>
#include<stdlib.h>

void bubblesort(int array[], int size){
   int i,j;
   for( i=0; i<size;i++){
       for( j=0; j<size-i-1;j++){
           if(array[j]>array[j+1]){
               int temp =array[j];
               array[j]=array[j+1];
               array[j+1]=temp;
           }
       }
   }
}

void getUserNumbers(int userNumbers[], int size){
   int num;int i, j;
   for(int i=0; i<size;i++){
   printf("Enter a number between 1 and 50: ");
   scanf("%d", &num);
   if(num<1 || num>50){
       printf("Error: Please enter a number between 1 and 50 only.\n");
       i--;
       continue;
   }
   for(j=0; j<i;j++){
       if(userNumbers[j]==num){
           i--;
           printf("Sorry, you already choose this number earlier. Please enter unique numbers only.\n");
           continue;
       }
   }
   userNumbers[i]=num;
}  
}  
void getRandomNumbers(int randomNumbers[] , int size){
   int i=0;
   srand(time(NULL));
   while(i<size){
       int num = rand()%50 + 1;
       int j; int flag = 0;
       for(j=0; j<i;j++){
           if(randomNumbers[j]==num){
               flag= 1;break;
           }
       }
       if(flag==0) {
           randomNumbers[i++]=num;
       }
   }
  
}
void displayNumbers(int array[], int size){
   int i;
   for(i=0;i<size;i++) printf("%3d ",array[i]);
   printf("\n");
}

void compareNumbers(int userNumbers[], int randomNumbers[],int size){
  
   int i,j; int matches =0;
   for(i=0; i<size;i++){
       for(j=0; j<size;j++){
           if(userNumbers[i]==randomNumbers[j]){
               printf("%3d ",userNumbers[i]);
               matches+=1;
           }
       }
   }
   printf("\nTotal matches : %3d\n",matches);
  
}
int main(){
   int size=7;
   int userNumbers[size];
   int randomNumbers[size];

   getUserNumbers(userNumbers,size);
   getRandomNumbers(randomNumbers,size);
   bubblesort(userNumbers,size);
   bubblesort(randomNumbers,size);
   printf("\n\nUser Numbers : ");displayNumbers(userNumbers,size);
   printf("Random Numbers : ");displayNumbers(randomNumbers,size);
   printf("\nMatch Results : ");
   compareNumbers(userNumbers, randomNumbers,size);
}

=====================================================================

Output Screenshot

if (array(j]>array[j+1]) { int temp =array[i]: C:\Users\User Documents\numberSaver.exe array[j]-array[j+1]; Enter a number be


answered by: ANURANJAN SARSAM

> Definitely not what i asked for. this is dead wrong

Alister Wed, Jun 9, 2021 5:33 AM

Add a comment
Know the answer?
Add Answer to:
MIPS Insertion program.........I could really use some help ASAP
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • what is the output of the following assembly code ? .data A: .word 84 111 116 97 108 32 105 115 32 . text main:​li $v0, 1l ​li $s0,0 ​la $s1, A ​li $t3, 0 loop: ​slti $t0, $s0, 17   ​be...

    what is the output of the following assembly code ? .data A: .word 84 111 116 97 108 32 105 115 32 . text main:​li $v0, 1l ​li $s0,0 ​la $s1, A ​li $t3, 0 loop: ​slti $t0, $s0, 17   ​beq: $t0, $zero, end ​sll $t1, $s0 2 ​add $t2 $s1 $t1   ​syscall ​addi $s0, $s0, 1 ​j loop end: ​li $v0, 1 ​add $a0, $zero, $t3 ​syscall

  • .data A: .word 84 111 116 97 108 32 105 115 32 . text main:​li $v0,...

    .data A: .word 84 111 116 97 108 32 105 115 32 . text main:​li $v0, 1l ​li $s0,0 ​la $s1, A ​li $t3, 0 loop: ​slti $t0, $s0, 17   ​beq: $t0, $zero, end ​sll $t1, $s0 2 ​add $t2 $s1 $t1   ​syscall ​addi $s0, $s0, 1 ​j loop end: ​li $v0, 1 ​add $a0, $zero, $t3 ​syscall /// whats the output of above assembly program

  • 1. For the MIPS assembly instructions below, what is the corresponding C statement? max:                    lw        &

    1. For the MIPS assembly instructions below, what is the corresponding C statement? max:                    lw          $t0, 0($a0)         #load the first array value into $t0 addi      $t1, $0, 1            #intialize the counter to one loop:                   beq       $t1, $a1, exit     #exit if we reach the end of the array addi      $a0, $a0, 4         #increment the pointer by one word addi      $t1, $t1, 1          #increment the loop counter lw          $t2, 0($a0)         #store the next array value into $t2 slt          $t3, $t0, $t2       beq      ...

  • The following MIPS program is to be run on a MIPS pipeline processor of 5 stages...

    The following MIPS program is to be run on a MIPS pipeline processor of 5 stages (IF-ID-EX-MEM-WB). Work out and diagram the optimal pipeline schedule using full forwarding from EX or MEM stages to any other stage, Draw the pipeline execution diagram for this code and then compute the pipeline CPI: addi $t6, $t6, 10 sub $t5, $t6, $t4 srl $t5, $t5, 2 sw $t5, 20($t5) lw $t2, 0($t6) add $t7, $t2, $t3 beq $t5, $t7, End

  • The task will be to write a program in assembler to enter a number and calculate...

    The task will be to write a program in assembler to enter a number and calculate its associated Fibonacci number using a procedure (subroutine) that is called recursively. Factorial and Fibonacci(outline Programs) # MIPS assembly assembly assemblyassemblycode .data n: .word 4 .text main: la $s0,n lw $a0, 0($s0) jal factorial # move what ever is returned into $a0 move $a0, $v0 li $v0,1 syscall b finished factorial: add $sp , $sp , -8 # make room sw $a0, 4($sp )...

  • im trying to complete mips program code about a calculator program that can calculate integer addition...

    im trying to complete mips program code about a calculator program that can calculate integer addition / subtraction written using the MIPS assembler. im having hard times to debug this. The input is given to the array of Formula char (base address $ s0) in the form of a formula. The null character (\ 0, ASCII code 0) is placed at the end. The calculation result is given to the register $ s1 and the overflow is ignored. For example,...

  • Assignment 4 File “quad_sol.s” contains a quadratic polynomial solver, which calculates the integer solution of a quadratic polynomial equation. 1. Rewrite the program using instructions reordering to...

    Assignment 4 File “quad_sol.s” contains a quadratic polynomial solver, which calculates the integer solution of a quadratic polynomial equation. 1. Rewrite the program using instructions reordering to reduce the number of cycles needed to execute the program. Indicate the number of cycle reduction. 2. Describe how forwarding would affect the execution of the program. CODE # quad_sol.s # This assembly program calculates the integer solutions of a quadratic polynomial. # Inputs : The coefficients a,b,c of the equation a*x^2 +...

  • Refer the program in the next page: Assume the starting address of data segment: 0x10010000. What...

    Refer the program in the next page: Assume the starting address of data segment: 0x10010000. What is the address of label num3? _______________________________ Write the common syscall code to print string for the blank marked as #Question 2 _________________________________ Write proper comments for the lines marked as #Question 3 ________________________________________________________ Write the machine code for the line marked as #Question 4 in both binary and hex. Binary:_______________________________________ Hex:___________________ Write the machine code for the line marked as #Question 5 in...

  • Please comment the MIPS code to help me understand. Here is what the code accomplishes. Here...

    Please comment the MIPS code to help me understand. Here is what the code accomplishes. Here is the code, partially commented. .data Matrix: .word 41,45,5, 34,8, 15,16,23,44,48,12,32,18,47,22,8,22 .word 46,40,42,33,13,38,27,6, 29,25,18,40,47,22,26,14,3 .word 7, 48,35,9, 43,38,9, 49,28,25,42,5, 44,10,5, 38,14 .word 46,33,16,6, 13,20,31,1, 8, 17,1, 47,28,46,14,28,7 .word 32,2, 48,25,41,29,14,39,43,46,3, 39,32,49,41,28,46 .word 5, 43,2, 48,13,4, 33,41,32,19,9, 25,30,22,2, 9, 40 .word 14,47,22,18,47,3, 35,44,18,6, 33,22,11,6, 47,50,4 .word 28,34,20,30,18,27,38,5, 26,40,37,23,16,13,37,8,7 .word 48,38,39,12,10,39,23,20,21,20,33,16,24,21,25,3,46 .word 49,38,40,38,13,47,5, 13,4, 13,23,26,12,30,29,29, 3 .word 8, 20,10,13,31,7, 12,41,12,21,28,26,43,14,35,10,19 .word 49,33,25,26,24,29,46,22,7, 5, 15,41,10,31,19,41,27 .word 48,9,...

  • (12 pts) The following code fragment processes an array and produces two important values in registers...

    (12 pts) The following code fragment processes an array and produces two important values in registers $v0 and $v1. Assume that the array consists of 5000 words indexed 0 through 4999, and its base address is stored in $a0 and its size (5000) in $a1. Describe what this code does. Specifically, what will be returned in $v0 and $v1? add $al, şal, $al add $al, şal, $al add $v0, şzero, $zero add $to, şzero, $zero add $t4, $a0, $to lw...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT