Im relatively new to programming, and I'm having trouble getting my head around mips.
I want to be able to arrange the given array in ascending order and print the arranged array. I've attempted to do this but I'm having no luck!
My program is to display the array for the user then user presses 2 to order the array and print this new array on screen.
I've included my attempt at this, sorry if it doesn't make sense.
.data
prompt:
.asciiz "Press 1 to display array, and Press 2 to display it in ascending order"
array: .word 0x00, 0x33, 0x44, 0x88, 0x56, 0x45, 0x56, 0x41, 0x00, 0x33, 0x44, 0x88, 0x56, 0x45, 0x56, 0x41, 0x00, 0x33, 0x44, 0x88, 0x56, 0x25, 0x58, 0x51, 0x03, 0x33, 0x24, 0x83, 0x52, 0x72, 0x16, 0x73, 0x85, 0x45, 0x47, 0x86, 0x36, 0x43, 0x52, 0x41, 0x74, 0x32, 0x04, 0x28, 0x26, 0x23, 0x46, 0x46, 0x06, 0x33, 0x34, 0x23, 0x21, 0x53, 0x15, 0x47, 0x77, 0x38, 0x41, 0x89, 0x58, 0x42, 0x51, 0x40, 0x86, 0x53, 0x40, 0x58, 0x36, 0x67, 0x53, 0x71, 0x03, 0x33, 0x74, 0x01, 0x89, 0x45, 0x12, 0x86, 0x60, 0x93, 0x42, 0x34, 0x66, 0x41, 0x51, 0x22, 0x60, 0x73, 0x41, 0x48, 0x46, 0x55, 0x52, 0x21, 0x00, 0x33, 0x64, 0x48, 0x66, 0x95, 0x53, 0x01, 0x03, 0x03, 0x24, 0x18, 0x16, 0x42, 0x53, 0x12, 0x40, 0x27, 0x47, 0x38, 0x56, 0x33, 0x58, 0x49, 0x09, 0x33, 0x04, 0x31, 0x34, 0x02, 0x22, 0x32
arraylength: .word 0x80
space: .asciiz ", "
newline: .asciiz "\n"
.globl main
.text
main:
#Show prompt message
la $a0, prompt
li $v0, 4
syscall
#Get users option/choice
li $v0, 5
syscall
#Store users input/option/choice in $t0
move $s0, $v0
li $v0, 4
la $a0, newline
syscall
addi $s1, $zero, 1
addi $s2, $zero, 2
beq $s0, $s1, displayArray
beq $s0, $s2, sort
displayArray:
li $v0, 0
la $t1, array #get array address
li $v0, 4
la $a0, newline
syscall
#reset counter of array
add $t0, $zero, 0
loop1:
bge $t0, 128, main
#load first element of array and goes to the next element
lw $t2, 0($t1)
addi $t1, $t1, 4
#syscall to print value
li $v0, 1
move $a0, $t2
syscall
#syscall for space in array
li $v0, 4
la $a0, space
syscall
#increment counter
addi $t0, $t0, 1
j loop1
sort:
li $v0, 0
#get array address
la $t1, array
add $t0, $zero, 0 #reset counter of array
pass_loop:
lw $t2, 0($t1) #load first element of array and goes to the next element
lw $t3, 4($t1)
bgt $t2, $t3, swap #if (t2 > t1) then swap elements
swap:
sw $t2, 4($t1) #put value of [i+1] in t2
sw $t3, 0($t1) #put value of [i] in t3
j next
next:
lw $t2, 0($t1)
addi $t1, $t1, 4
#syscall to print value
li $v0, 1
move $a0, $t2
syscall
bge $t0, 128, main
addi $t0, $t0, 1
exit:
li $v0, 10 #syscall to end program
syscall