-1

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
Michael
  • 57,169
  • 9
  • 80
  • 125
Amy.C
  • 19
  • 4
  • 1
    _"I'm having no luck"_ is not a good problem description. What is the expected output, and what is the actual output you're getting? Also, since you're running this in a simulator, what have done in terms of using the debugging features available to inspect the runtime behavior of your code? – Michael Aug 31 '19 at 10:34
  • You need to learn debugging skills. Figure out how to single step, and, each time you single step, observe that the proper data is being produced, and that the proper control flow is happening. Single step just one time through the sorting loop and you'll see that something is messed up. – Erik Eidt Aug 31 '19 at 15:25
  • For example, the label `pass_loop` is defined but never used. Once you fix that though, you'll find that comparing consecutive elements is not sufficient to accomplish sorting. It is a start, but it will only move the largest element to the end leaving the rest unsorted. – Erik Eidt Aug 31 '19 at 15:26

1 Answers1

0

In your code, only the swapping of the first two elements was taking place. There was no counter which traversed the entire array and swapped elements accordingly. There are several different types of sorting algorithms that can be used to swap elements, such as bubble sort, selection sort, insertion sort etc. I would suggest that you look up a few sorting algorithms and see if you can implement them on C and then translate them in MIPS assembly language. Here, I've slightly modified your code, using the bubble sort, which contains 2 loops, an outer loop for the number of passes and an inner loop for swapping between each pass. I've included the explanation in the comments. If you are having trouble understanding the code, kindly let me know.

.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 $t0, 0 #i counter
    lw $t6, arraylength
loop:
    la $t1, array
    beq $t0, $t6, exitLoop #outer loop for each pass
    sub $t5, $t6, $t0
    addi $t5, $t5, -1 #in each pass, the length of the sub array would become smaller as the elements after the sub array will arrive at their correct positions (suppose array is 3, 2, 1. After 1st pass, the array will become 2, 1, 3. we exclude the last element as it is in place and then the next pass would include array 2,1 and so on)
    li $t4, 0 #j counter
    addi $t0, $t0, 1
    j pass_loop

pass_loop:
    beq $t4, $t5, loop #loop for swapping consecutive elements in each pass
    lw $t2, 0($t1)
    lw $t3, 4($t1)
    bgt $t2, $t3, swap
    j next
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
    addi $t4, $t4, 1
    j pass_loop
exitLoop: #copy of the display array code to print the sorted array
li $v0, 0
    la $t1, array #get array address
    li $v0, 4      
    la $a0, newline
    syscall

    #reset counter of array
    add $t0, $zero, 0

loop2:
    bge $t0, 128, exit

    #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 loop2
exit:
    li $v0, 10
    syscall 
R-D
  • 1
  • 1