Instantly share code, notes, and snippets.

Embed
What would you like to do?
Assembler simulator bubble sort
; Runs on https://schweigi.github.io/assembler-simulator/
; https://en.wikipedia.org/wiki/Bubble_sort
MOV C, data ; C tracks the address of end
ADD C, 16 ; of the unsorted section of data.
start:
DEC C ; One byte is sorted each pass
MOV D, data ; Start of the data
bubble:
MOV A, [D]
CMP A, [D+1] ; Compare two adjacent values
JNA noswap ; jump if the don't need swapping
MOV B, [D+1] ; swap the two values
MOV [D+1], A
MOV [D], B
noswap:
INC D ; Move along the data one byte
CMP D, C
JB bubble ; loop if there's more unsorted data
CMP C, data
JNB start ; start again unless we've sorted all values.
HLT
DB "_______" ; Align the sorted data nicely.
data:
DB "QWERTYUIOPASDFGH" ; 16 characters to sort.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment