Skip to content

Instantly share code, notes, and snippets.

@stephenpaulger
Created June 3, 2017 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenpaulger/d80a423959f27d6c0f9a64af4f3bb46c to your computer and use it in GitHub Desktop.
Save stephenpaulger/d80a423959f27d6c0f9a64af4f3bb46c to your computer and use it in GitHub Desktop.
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