Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Assembler - Insert sort

This is a straightforward implementation of the insert sort algorithm in assembler (NASM).

Compile and link it with:
nasm -f elf isort.s
ld -s -o isort isort.o

Insert sort: http://en.wikipedia.org/wiki/Insert_sort

   1  
   2  ; void isort(int *a, int n)
   3  ;   sorts the first n elements of a
   4  ;
   5  ; Parameters
   6  ;   a - pointer to the array
   7  ;   n - number of elements to sorts
   8  
   9  %define a [ebp + 8]
  10  %define n [ebp + 12]
  11  isort:
  12    enter 0, 0
  13    pusha
  14  
  15    mov ecx, 1
  16    for:
  17      mov ebx, ecx
  18      imul ebx, 4
  19      add ebx, a
  20      mov ebx, [ebx]
  21  
  22      mov edx, ecx
  23      dec edx
  24  
  25      while:
  26        cmp edx, 0
  27        jl while_quit
  28  
  29        mov eax, edx
  30        imul eax, 4
  31        add eax, a
  32  
  33        cmp ebx, [eax]
  34        jge while_quit
  35  
  36        mov esi, [eax]
  37  
  38        mov dword [eax + 4], esi
  39  
  40        dec edx
  41        jmp while
  42      while_quit:
  43  
  44      mov [eax], ebx
  45  
  46      inc ecx
  47      cmp ecx, n
  48      jl for
  49  
  50    popa
  51    leave
  52    ret
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS