algorithmsortingassemblylc3

How to make a sorting algorithm in Assembly code, in LC3


I have a simple loop that takes a name and prints the name without saving it.

    looptext getc         ;starts text get loop for name
                          ;since name isn't re-used, we don't have to save it
    add r1, r0, -10       ;Test for enter character
    brz finishloop1       ;if enter, cancel the text loop
    OUT                   ;If it's not enter, print out the character typed
    br looptext           ;Go back to loop
finishloop1

The program then asks for an ID number separated by spaces. All these values are saved into an array and each loop, it checks if the new input is the 'new' lowest value or 'new' highest value and sets it into the respectable register.

[Deleted code for copyright sake]

At the end of the code, where I need to add a sorting algorithm, I am left with an array of only characters.

I need to go through each index of the array and rearrange the characters in the numerical order (smallest to largest).


Solution

  • Thank you very much all of you for the tips and tricks. Thank you specifically @Ped7g for linking me that sorting algorithms page. I ended up searching around and actually finding someone on gitub that had a bubble algorithm already written out in Assembly code. So thanks for indirectly giving me the answer.

    Note: For any future people coming here to find answer, here is the link the bubble sorting algorithm code: https://github.com/oc-cs360/s2014/blob/master/lc3/bubblesort.asm. This is part of the lecture notes for a university course.

    ; Implementing bubble sort algorithm
    ;   R0  File item
    ;   R1  File item
    ;   R2  Work variable
    ;   R3  File pointer
    ;   R4  Outer loop counter
    ;   R5  Inner loop counter
    
    
                .ORIG   x3000
    
    ; Count the number of items to be sorted and store the value in R7
    
                AND     R2, R2, #0  ; Initialize R2 <- 0 (counter)
                LD      R3, FILE    ; Put file pointer into R3
    COUNT       LDR     R0, R3, #0  ; Put next file item into R0
                BRZ     END_COUNT   ; Loop until file item is 0
                ADD     R3, R3, #1  ; Increment file pointer
                ADD     R2, R2, #1  ; Increment counter
                BRNZP   COUNT       ; Counter loop
    END_COUNT   ADD     R4, R2, #0  ; Store total items in R4 (outer loop count)
                BRZ     SORTED      ; Empty file
    
    ; Do the bubble sort
    
    OUTERLOOP   ADD     R4, R4, #-1 ; loop n - 1 times
                BRNZ    SORTED      ; Looping complete, exit
                ADD     R5, R4, #0  ; Initialize inner loop counter to outer
                LD      R3, FILE    ; Set file pointer to beginning of file
    INNERLOOP   LDR     R0, R3, #0  ; Get item at file pointer
                LDR     R1, R3, #1  ; Get next item
                NOT     R2, R1      ; Negate ...
                ADD     R2, R2, #1  ;        ... next item
                ADD     R2, R0, R2  ; swap = item - next item
                BRNZ    SWAPPED     ; Don't swap if in order (item <= next item)
                STR     R1, R3, #0  ; Perform ...
                STR     R0, R3, #1  ;         ... swap
    SWAPPED     ADD     R3, R3, #1  ; Increment file pointer
                ADD     R5, R5, #-1 ; Decrement inner loop counter
                BRP     INNERLOOP   ; End of inner loop
                BRNZP   OUTERLOOP   ; End of outer loop
    SORTED      HALT
    
    FILE        .FILL   x3500       ; File location
                .END