Question

how would we sort an array in MIPS assembly? (not bubble sort) The array is called...

how would we sort an array in MIPS assembly? (not bubble sort)

The array is called myArray and has 10 random elements in it.

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

Before going through the sorting process ,Let us understand the basic structure in MIPS programming, with comparison, with our basic C++ programming language, so that you can get the basic idea of the MIPS program

.data                      // #include<iostream.h>

.text                      # using namespace std;

main:                  # int main()

li $v0, 10            # return 0;

syscall

.data

myArray : .space        40       // size of array is 40 , because 1 int = 4 bytes (Therefore 10 integers = 40 bytes

prompt    :      .asciiz    "Please enter 10 elements in an myArray" // just a print message for the user

.text

.main :

#At start all registers will hold Zero value, Here Below is the declaration of all the #constants that we will use in the coding

  • addi    $t0   ,   $t0   , 40   # command for add, it works as $t0 = $t0 +40, Here it is used to put myArray size constant
  • addi    $t4   ,   $t0   , 90   #Loop counter
  • addi    $t9 ,   $t9 , 1     #input counter
  • addi    $s5 ,   $s5 , 10   #Division

#Here, Now we want to prompt the user what they must do

  • li    $v0,     4 #load immediate $v0,    4 , tells the compiler that we will pass a string value to $a0, which is a cout command in MIPS
  • la       $a0,     prompt
  • syscall # This system call will print the message as mentioned above prompt

# Now it will load the nxtline variable, which hold the value of \n to go to the next line, see below

  • li         $v0,     4
  • la       $a0,     nxtline
  • syscall

input :

      #user enter 10 integers

  • beq    $t1,   $t0, continue #it will compare the first register with the second register

# This below will count the number of integers entered, it will count the number of variables

  • move $s0,     $t9
  • li         $v0,      i
  • syscall

#Now below code will differentiate between the input counter and the input that will be entered.

  • li     $v0 , 4
  • la    $ao , colon
  • syscall

#Now to scan the values from the user, this command is same as the cin command in C++

  • li   $v0   , 5
  • syscall
  • move   $t2   , $v0

  • sw $t2, myArray($t1) #Store word, it will transfer the values inside first register into second register.
  • addi   $t1,   $t1,   4 #This is basically to update the array position, here it will add 4 which means 4 bytes for the integer to go to the next position in the array
  • addi    $t9,   $t9,   1 #Update for input counter
  • j input #jump command , means program will go to the address that comes after the command , Here it will go back to input.

continue:

#Reinitialize register

move $t1, $zero For array(x)

move $t2, $zero

addi $t2, $t2, 4 #For array(x+1)

move $so, $zero

addi $so, $s0, 1 #conditon check

Sort :

#This code will sort the array, and checks conditions for swapping elements

  • beg $t3, $t4, calculation    #finish loop
  • beg   $t2, $t0, continue #Reinitialize array

  • lw $t5, myArray($t1) #load word , description given above
  • lw $t6, myArray($t2)   #load word , description given above

  • addi $t3, $t3,   $t6 #update for the loop counter
  • slt $t7, $t5, $t6 #checks the condition (set less than), it will compare the n register to n+ 1 register
  • beg $t7,   $s0, swap # used to sort the element in the decending order and it will help to swap the elements to arrage the elements in order
  • addi $t1, $t1, 4
  • addi $t2, $t2, 4
  • j sort

swap:

#swap array elements

  • sw   $t5, myArray($t2)
  • sw   $t6 , myArray($t1)

#command to update array position below

  • addi $t1, $t1, 4
  • addi $t2, $t2, 4
  • j sort

calculation:

#After sorting reinitialize the register

  • move $t1, $zero                 #Array elements
  • move $t2, $zero             #Temporary holder
  • move $t3, $zero              #First array element
  • move $t4, $zero
  • move $t5, $zero
  • move $t6, $zero
  • move $t7, $zero

Hope This above program will help you to better understandment of the sorting program.

Add a comment
Know the answer?
Add Answer to:
how would we sort an array in MIPS assembly? (not bubble sort) The array is called...
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
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