Answer to Question #327082 in Assembler for Faisal

Question #327082

write an assembly language program that alphabetically shorts and array of 10 integers.

1
Expert's answer
2022-04-13T13:18:05-0400

#MIPS QtSpim assembly

.data

DisplaySorted:   .asciiz "\nHere is the sorted list in ascending order: "

SpaceOut:      .asciiz " "


.align 4

array: .word 1,5,2,9,7,6,2,8,4,1


# code segment

   .text

   .globl main

main:

# Take integer N


    addi $a1, $0, 10     # Pass integer N to register $a1

   la $a0, array      # begining address in array   

    jal sort        # jmp the sorting Function.


# display the result of the Sortng Function

   li $v0, 4            # Print string

   la $a0, DisplaySorted   # "Here is the sorted list in ascending order:"

   syscall


   la $a0, array      # begining address in array   

   jal PrintArray   


exitP:    nop

   li $v0, 10        # finished .. stop .. return

   syscall           # make the syscall

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

# Function   sort. Sort the array (bubble_sort)

# Input:

# $a0 = array address

# $a1 = the number elements in array

sort:

   addi $sp, $sp, -8   # make space on stack to store registers

   sw $s1, 4($sp)      # save $s1 on stack

   sw $s0, 0($sp)      # save $s0 on stack


   add $s1,$zero, $a1      # $s1 = $a1 (the number of elements in array)


SortLoop3: 

       add $s2, $zero, $s1    # $s2 = the number of elements

       add $s2, $s2, -1       # number of swaps (counter outer loop)

      add $s0, $zero, $a0    # $s0 = address of array[0]


      li $t0,0            # counter of swaps =0 (inner loop)

SortLoop2:

      lw $t7, 0($s0)         # get a[i] current item )

       lw $t8, 4($s0)         # get a(i+1) next itemt

       slt $t1, $t8, $t7      # IF(a[i] < a(i+1)) -> $t1 is set to one

       beq $t1, $zero, noSwap # ELSE IF($t1=0) -> goto no swap


swap:   # swap current item and next item

       sw $t7, 4($s0)         # a[i] to position a(i+1)

       sw $t8, 0($s0)         # a(i+1) to position a[i]

       addi $t0, $t0, 1       # counter of swaps)++


noSwap:  

      add $s0, $s0, 4        # addres of next item in array (base + 4byte)

       addi $s2, $s2, -1      # (number of swaps)--

       bgt $s2, $zero, SortLoop2 # IF(number of swaps>0) THEN goto SortLoop2 (repeat)


       bne $t0, $zero, SortLoop3 # IF(counter of swaps>0) goto SortLoop3


sortEnd:   

   lw $s0, 0($sp)      # restore $s0 from stack   

   lw $s1, 4($sp)      # restore $s1 from stack   

   addi $sp, $sp, 8   # deallocate stack space   

   jr $ra      


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

# function   PrintArray

# input:

# $a0 = array address

# $a1 = the number elements in array

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

PrintArray:

   addi $sp, $sp, -8   # make space on stack to store registers

   sw $s1, 4($sp)      # save $s1 on stack

   sw $s0, 0($sp)      # save $s0 on stack


   li $t0,0         # counter elements of array

   add $s0,$0, $a0      # $s0 = address of array[0]

   add $s1,$0, $a1      # the number of elements in the array


arrayPrint:   

   slt   $t7, $t0, $s1      # IF(i < N) continue

   beq   $t7, $zero, printEnd   # ELSE end


   li   $v0, 1            # print integer

   lw $a0,0($s0)         # $a0 = a[i]

   syscall               # make the syscall               


   li $v0,4            # print string

   la $a0,SpaceOut         # SpaceOut " " after a[i]

   syscall               # make the syscall   


   addi   $t0, $t0, 1      # i++

   addi   $s0, $s0, 4      # next address of element in array

   j   arrayPrint   

printEnd:


   lw $s0, 0($sp)      # restore $s0 from stack   

   lw $s1, 4($sp)      # restore $s1 from stack   

   addi $sp, $sp, 8   # deallocate stack space   

   jr $ra      

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS