Skip to content

Instantly share code, notes, and snippets.

@jibsen
Created November 15, 2015 09:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jibsen/8afc36995aadb896b649 to your computer and use it in GitHub Desktop.
Save jibsen/8afc36995aadb896b649 to your computer and use it in GitHub Desktop.
Bubble sort in 16 bytes of x86 assembly language
;;
;; The "worlds smallest" bubble sort (16 bytes)
;;
;; Copyright (c) 1998 by Joergen Ibsen / Jibz
;; All Rights Reserved
;;
bits 32
section .text
;=====================================================================
; enter: edi -> array
; ecx = #elements - 1
; exit: edi -> array
; ecx = 0
; modify: ecx
;=====================================================================
bsort32:
.outerloop:
pusha ; save edi and ecx
mov esi, edi
.innerloop:
lodsd
cmp eax, [esi] ; do we need to swap?
jge short .order_ok
xchg eax, [esi] ; if so, this is first step
.order_ok:
stosd ; second step, or just write back eax
loop .innerloop
popa ; pop edi and ecx
loop .outerloop ; ecx is used for both loops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment