Bubble sort in 16 bytes of x86 assembly language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; | |
;; 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