4 Mar 2007 fejj   » (Master)

I came across something cool the other day that I thought I'd share: a 25-byte long integer sort routine (that is to say, the routine itself is only 25 bytes long).

Here it is:

;---------------------------------------------------------------
; Sorts an array of ints. C-callable (small model). 25 bytes.
; void sort (int n, int a[]);
;
; Courtesy of David Stafford.
;---------------------------------------------------------------

.model small .code public _sort

top: mov dx,[bx] ; swap two adjacent ints xchg dx,[bx+2] xchg dx,[bx]

cmp dx,[bx] ; in the right order? jl top ; no, swap them back

inc bx ; go to the next integer inc bx loop top

_sort: pop dx ; get return address (entry point) pop cx ; get count pop bx ; get pointer push bx ; restore pointer dec cx ; decrement count push cx ; save count push dx ; save return address jg top ; if cx > 0

ret

end

Latest blog entries     Older blog entries

New Advogato Features

FOAF updates: Trust rankings are now exported, making the data available to other users and websites. An external FOAF URI has been added, allowing users to link to an additional FOAF file.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!