You are on page 1of 7

.

text
main:
#printing welcome message
la $a0, welcome
li $v0, 4
syscall

enterSize:

#taking array size


la $a0, enterArrSize
li $v0, 4
syscall

li $v0, 5
syscall

#if 0, quit. if < 0, ask again


beq $v0, 0, quit
blt $v0, 0, notNegative
j validSize

notNegative:

la $a0, cannotBeNegative
li $v0, 4
syscall
j enterSize

validSize:

#creating array
sw $v0, arraySize
move $t0, $v0
mul $t0, $t0, 4

move $a0, $t0


li $v0, 9
syscall

sw $v0, arrayAddress

jal monitor

monitor:

lw $s0, arraySize #register s0 holds array size


lw $s1, arrayAddress #register s1 holds address of the first element
of the array

loopMonitor:

beq $s0, 0, doneMonitor

#prompting the user to enter array elements


la $a0, enterElem
li $v0, 4
syscall
li $v0, 5
syscall

move $t0, $v0 #register t0 holds the input


sw $t0, 0($s1) #loading the input to the array

addi $s1, $s1, 4 #next element


addi $s0, $s0, -1 #updating loop control variable
j loopMonitor

doneMonitor:

jal displayArray #displaying array


jal displayMenu #displaying menu
jr $ra

displayArray:

lw $s0, arraySize #register s0 holds array size


lw $s1, arrayAddress #register s1 holds address of the first element
of the array

la $a0, printArray #printing array


li $v0, 4
syscall

loopDisplay:

beq $s0, 0, doneDisplay


lw $t0, 0($s1) #loading the array element to register t0

move $a0, $t0 #taking elem at index


li $v0, 1 #printing the elem
syscall

#if at not element, put a comma


bgt $s0, 1, printComma
j commaPrinted

printComma:

la $a0, comma
li $v0, 4
syscall

commaPrinted:

addi $s1, $s1, 4 #next element


addi $s0, $s0, -1 #updating loop control variable
j loopDisplay

doneDisplay:

jr $ra

displayMenu:
#printing menu
la $a0, welcomeToMenu
li $v0, 4
syscall

la $a0, enterToSort
li $v0, 4
syscall

la $a0, enterToMaxMin
li $v0, 4
syscall

la $a0, enterToQuit
li $v0, 4
syscall

#prompting the user to enter choice


la $a0, yourChoice
li $v0, 4
syscall

li $v0, 5
syscall

move $t0, $v0 #choice is stored in register t0

beq $t0, 1, bubbleSortJAL #if choice = 1, bubble sort


beq $t0, 2, minMaxJAL #if choice = 2, find min max
beq $t0, 3, quit #if choice = 3, quit

bubbleSortJAL:

jal bubbleSort
jal displayArray
j enterSize

minMaxJAL:

jal minMax
j enterSize

la $a0, invalidChoice #if it is not 1, 2 or 3 print invalidChoice string


and display the menu again
li $v0, 4
syscall
j displayMenu

bubbleSort:

lw $s0, arraySize #loading the array size to register s0


lw $s3, arrayAddress #register s3 address of the first element of
the array
mul $t0, $s0, 4
addi $t0, $t0, -4
add $s1, $s3, $t0 #register s1 (initially) holds the address of the
last element of the array
addi $s2, $s1, -4 #register s2 (initially) holds the address of the
element that is one previous that the last

loopSort:

blt $s2, $s3, nextLoop #inner loop control


j nextLoopDone

nextLoop:

addi $s3, $s3, 4


lw $s1, arrayAddress
mul $t0, $s0, 4
addi $t0, $t0, -4
add $s1, $s1, $t0
addi $s2, $s1, -4
blt $s2, $s3, sortDone #outer loop control

nextLoopDone:
lw $t0, 0($s1) #register t0 holds element on the smaller array
index
lw $t1, 0($s2) #register t1 holds element on the bigger array
index

blt $t1, $t0, swap #swap elements if t1 < t0


j swapDone

swap:

sw $t0, 0($s2) #swapping


sw $t1, 0($s1) #elements

swapDone:

addi $s1, $s1, -4 #next element (smaller)


addi $s2, $s2, -4 #next element (bigger)
j loopSort

sortDone:

#printing sorted array


la $a0, arraySorted
li $v0, 4
syscall

jr $ra

minMax:

lw $s2, arrayAddress #register s2 holds the address of the first


element of the array
lw $s0, 0($s2) #register s0 holds min value
lw $s1, 0($s2) #register s1 holds max value
lw $s3, arraySize #register s3 holds the array size
move $t0, $s3
mul $t0, $t0, 4
add $s4, $s2, $t0 #register s4 holds the boundary address of the array
minMaxLoop:

beq $s2, $s4, minMaxDone


lw $t0, 0($s2) #register t0 holds the element at array index

#if element at array index is less than min, assign min to t0


#if element at array index is greater than max, assign max to t0
blt $t0, $s0, setLess
j setLessDone

setLess:

move $s0, $t0

setLessDone:

bgt $t0, $s1, setGreater


j setGreaterDone

setGreater:

move $s1, $t0

setGreaterDone:

addi $s2, $s2, 4


j minMaxLoop

minMaxDone:

#printing max and min values


la $a0, foundMinMax
li $v0, 4
syscall

la $a0, minVal
li $v0, 4
syscall

move $a0, $s0


li $v0, 1
syscall

la $a0, maxVal
li $v0, 4
syscall

move $a0, $s1


li $v0, 1
syscall
jr $ra

quit:

la $a0, goodbye
li $v0, 4
syscall

li $v0, 10
syscall

.data

arraySize:
.word 0

arrayAddress:
.word 0

welcome:
.asciiz "Welcome!\n"

enterArrSize:
.asciiz "\nPlease enter an array size(0 to quit): "

enterElem:
.asciiz "\nEnter elements to add to the array: "

printArray:
.asciiz "Array: "

comma:
.asciiz ", "

welcomeToMenu:
.asciiz "\n---------------Welcome to the menu---------------\n"

enterToSort:
.asciiz "1) Sort the array using bubble sort\n"

enterToMaxMin:
.asciiz "2) Find maximum and minimum values in the array\n"

enterToQuit:
.asciiz "3) Quit\n"

yourChoice:
.asciiz "Your choice: "

invalidChoice:
.asciiz "Invalid option! Enter again\n"

goodbye:
.asciiz "Goodbye!"

arraySorted:
.asciiz "Your array has been sorted!\n"

foundMinMax:
.asciiz "Minimum and meximum elements are found!\n"

minVal:
.asciiz "Minimum: "
maxVal:
.asciiz "\nMaximum: "

cannotBeNegative:
.asciiz "Size cannot be negative! Enter again\n"

You might also like