Bubble Sort Java to MIPS assembly language Java sample code

Bubble Sort ( Java to MIPS {assembly language})

Java (sample code)

++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++

// arr[i] will be in the correct spot after every iteration

for (int i = n-1; i > 0; i--)  

   for (int j = 0; j < i; j++) // Put arr[j] and arr[j+1] in order

       if (arr[j] > arr[j+1]) { // If they are out of order, swap them

          int tmp = arr[j];

          arr[j] = arr[j+1];

          arr[j+1] = tmp;

       }

++++++++++(Bubble Sort _ while loop _ sample)+++++++++++++++++

int i = n-1;

while (i > 0) {

   int j = 0;

   while(j < i){

       if (arr[j] > arr[j+1]) { // If they are out of order, swap them

          int tmp = arr[j];

          arr[j] = arr[j+1];

          arr[j+1] = tmp;

       }

i--;

}

++++++++++++++++++++++++++++++++

To complete the MIPS (assemble code) code, you essentially must translate the two nested for loops and if statement above from Java to MIPS.

add your code to the area marked “PUT CODE HERE.”

Do not modify anything outside that area.

do not write anything to registers $s0 or $s5.

You may use temporaries as necessary, and saved registers for other variables (like i and j).

After you add the code, re-assemble and run it

++++++++++++++ (MIPS Code _ Assemble language)+++++++++++++

      .data

nums: .word   0 : 12        # \"array\" of 12 words to contain values

size: .word 12             # size of \"array\"

      .text

      la $s0, nums

      la   $s5, size        # load address of size variable

      lw   $s5, 0($s5)

      # Populate fibs with twelve values

      addi $t1, $zero, 55

      sw $t1, 0($s0)

      addi $t1, $zero, 88

      sw $t1, 4($s0)

      addi $t1, $zero, 0

      sw $t1, 8($s0)

      addi $t1, $zero, 22

      sw $t1, 12($s0)

      addi $t1, $zero, 77

      sw $t1, 16($s0)

      addi $t1, $zero, 44

      sw $t1, 20($s0)

      addi $t1, $zero, 99

      sw $t1, 24($s0)

      addi $t1, $zero, 33

      sw $t1, 28($s0)

      addi $t1, $zero, 110

      sw $t1, 32($s0)

      addi $t1, $zero, 66

      sw $t1, 36($s0)

      addi $t1, $zero, 121

      sw $t1, 40($s0)

      addi $t1, $zero, 11

      sw $t1, 44($s0)

      ##################################################################

      # AT THIS POINT: $s0 is the address of the start of the array

      #                $s5 is the size (n)

      #################################################################

      # PUT CODE HERE

                        

      ##################################################################

      la   $a0, nums        # first argument for print (array)

      add   $a1, $s5, $zero      # second argument for print (size)

      jal print            # call print routine.

      li   $v0, 10          # system call for exit

      syscall               # we are out of here.

######### routine to print the numbers on one line.

######### don\'t touch anything below this line!!!!

      .data

space:.asciiz \" \"          # space to insert between numbers

head: .asciiz \"Sorted array:\ \"

      .text

print:add $s0, $zero, $a0 # starting address of array

      add $t1, $zero, $a1 # initialize loop counter to array size

      la   $a0, head        # load address of print heading

      li   $v0, 4           # specify Print String service

      syscall               # print heading

out: lw   $a0, 0($s0)      # load fibonacci number for syscall

      li   $v0, 1           # specify Print Integer service

      syscall               # print fibonacci number

      la   $a0, space       # load address of spacer for syscall

      li   $v0, 4           # specify Print String service

      syscall               # output string

      addi $s0, $s0, 4      # increment address

      addi $t1, $t1, -1     # decrement loop counter

      bgtz $t1, out         # repeat if not finished

      jr   $ra              # return

           

Solution

.data

nums: .word   0 : 12        # \"array\" of 12 words to contain values

size: .word 12             # size of \"array\"

      .text

      la $s0, nums

      la   $s5, size        # load address of size variable

      lw   $s5, 0($s5)

      # Populate fibs with twelve values

      addi $t1, $zero, 55

      sw $t1, 0($s0)

      addi $t1, $zero, 88

      sw $t1, 4($s0)

      addi $t1, $zero, 0

      sw $t1, 8($s0)

      addi $t1, $zero, 22

      sw $t1, 12($s0)

      addi $t1, $zero, 77

      sw $t1, 16($s0)

      addi $t1, $zero, 44

      sw $t1, 20($s0)

      addi $t1, $zero, 99

      sw $t1, 24($s0)

      addi $t1, $zero, 33

      sw $t1, 28($s0)

      addi $t1, $zero, 110

      sw $t1, 32($s0)

      addi $t1, $zero, 66

      sw $t1, 36($s0)

      addi $t1, $zero, 121

      sw $t1, 40($s0)

      addi $t1, $zero, 11

      sw $t1, 44($s0)

      move $t0, $a0       #move address of array into $t0

      li $s4,1                  # Boolean swap

      li $t1,0                  #j=0;

    li $t2,0                  #i=0;

      li $s1,12                #array length

      loop:

             beqz    $s0,exit            #exit if swap=false

            li $s4,0                         #swap if false

            addiu $t1,$t1,1       # j++

             move $t2,0            #i=0;

             subu $s2,$s1,$t1   # s2=length-j

for loop:

bge      $t2,$s2, exit for loop   # if i>=s2, exit

lw        $a0,0($t0)                    #a0=array[i]

lw        $a1,4($t0)                    #a1=array[i+1]

ble       $a0,$a1,update

sw        $a1,0($t0)                    # a[i+1] = a[i]

li          $s4,1                            # swap=true

update :

addiu   $t2,$t2,1                      #i++

sll         $t3,$t2,2                      #t3=i*4

addu    $t0, $t0,$t3                 #for next element

j           forloop

            exitforloop:

                        j   loop

exit:

      la   $a0, nums        # first argument for print (array)

      add   $a1, $s5, $zero      # second argument for print (size)

      jal print            # call print routine.

      li   $v0, 10          # system call for exit

      syscall               # we are out of here.

// routine to print the numbers on one line.

//don\'t touch anything below this line!!!!

      .data

space:.asciiz \" \"          # space to insert between numbers

head: .asciiz \"Sorted array:\ \"

      .text

print:add $s0, $zero, $a0 # starting address of array

      add $t1, $zero, $a1 # initialize loop counter to array size

      la   $a0, head        # load address of print heading

      li   $v0, 4           # specify Print String service

      syscall               # print heading

out: lw   $a0, 0($s0)      # load fibonacci number for syscall

      li   $v0, 1           # specify Print Integer service

      syscall               # print fibonacci number

      la   $a0, space       # load address of spacer for syscall

      li   $v0, 4           # specify Print String service

      syscall               # output string

      addi $s0, $s0, 4      # increment address

      addi $t1, $t1, -1     # decrement loop counter

      bgtz $t1, out         # repeat if not finished

      jr   $ra              # return

Bubble Sort ( Java to MIPS {assembly language}) Java (sample code) ++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++ // arr[i] will be in the correct
Bubble Sort ( Java to MIPS {assembly language}) Java (sample code) ++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++ // arr[i] will be in the correct
Bubble Sort ( Java to MIPS {assembly language}) Java (sample code) ++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++ // arr[i] will be in the correct
Bubble Sort ( Java to MIPS {assembly language}) Java (sample code) ++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++ // arr[i] will be in the correct
Bubble Sort ( Java to MIPS {assembly language}) Java (sample code) ++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++ // arr[i] will be in the correct
Bubble Sort ( Java to MIPS {assembly language}) Java (sample code) ++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++ // arr[i] will be in the correct

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site