Pejman Moghadam / Assembly

Linux Assembly - Sort an array

Public domain


Sorting an array of 8 bytes length signed integer numbers

; Filename : sort-8B.asm
; Compile  : nasm -g -f elf sort-8B.asm -l sort-8B.lst
; Link     : ld -dynamic-linker /lib/ld-linux.so.2 -o  sort-8B -lc sort-8B.o


extern  printf
extern  exit 


SECTION .data
; Define Quad = 8 Bytes
array:  dq      3752,  4329,  -1795,  -5203,  2001, 28942, 14070, -14120, 22279, 11275
count:  equ     10                  ; Array length
format: db      "%6d", 0xa, 0       ; printf output format

SECTION .text
global  _start
_start:
        nop

        ; Sort Array

        mov     esi, array
        mov     ecx, count-1
s1:
        push    ecx

        mov     eax, [esi]
        mov     edi, esi
s2:
        add     edi, 8
        mov     ebx, [edi]
        cmp     eax, ebx
        jge     noxchg
        mov     [edi], eax
        mov     [esi], ebx
        mov     eax, ebx
noxchg:
        loop    s2

        add     esi, 8
        pop     ecx
        loop    s1

        ; Show Sorted Array

        mov     esi, array
        mov     ecx, count
show:
        push    ecx             ; Backup counter

        mov     eax, [esi]      ; Take a number from array

        push    eax             ; Second printf parameter
        push    format          ; First printf parameter
        call    printf
        add     esp, 8          ; Clear stack after printf

        add     esi, 8          ; Jump to next number in array
        pop     ecx             ; Restore counter
        loop    show


        push    0               ; Return value
        call    exit

Sorting an array of 4 bytes length signed integer numbers

; Filename : sort-4B.asm
; Compile  : nasm -g -f elf sort-4B.asm -l sort-4B.lst
; Link     : ld -dynamic-linker /lib/ld-linux.so.2 -o  sort-4B -lc sort-4B.o


extern      printf
extern      exit 


SECTION     .data
; Define Double word = 4 Bytes
array:      dd      3752,  4329,  -1795,  -5203,  2001, 28942, 14070, -14120, 22279, 11275
count:      equ     10              ; Array length
format:     db      "%6d", 0xa, 0   ; printf output format

SECTION .text
global      _start
_start:
        nop

        ; Sort Array

        mov     esi, array
        mov     ecx, count-1
s1:
        push    ecx

        mov     eax, [esi]
        mov     edi, esi
s2:
        add     edi, 4
        mov     ebx, [edi]
        cmp     eax, ebx
        jge     noxchg
        mov     [edi], eax
        mov     [esi], ebx
        mov     eax, ebx
noxchg:
        loop    s2

        add     esi, 4
        pop     ecx
        loop    s1

        ; Show Array

        mov     esi, array
        mov     ecx, count
show:
        push    ecx             ; Backup counter

        mov     eax, [esi]      ; Take a number from array

        push    eax             ; Second printf parameter
        push        format      ; First printf parameter
        call        printf
        add esp, 8              ; Clear stack after printf

        add     esi, 4          ; Jump to next number in array
        pop     ecx             ; Restore counter
        loop    show


        push    0               ; Return value
        call    exit

Sorting an array of 2 bytes length signed integer numbers

; Filename : sort-2B.asm
; Compile  : nasm -g -f elf sort-2B.asm -l sort-2B.lst
; Link     : ld -dynamic-linker /lib/ld-linux.so.2 -o  sort-2B -lc sort-2B.o


extern      printf
extern      exit 


SECTION     .data
; Define Word = 2 Bytes
array:      dw      3752,   4329,   -1795,  -5203,   2001,  28942,   14070, -14120,  22279,  11275
count:      equ     10              ; Array length
format:     db      "%6d", 0xa, 0   ; printf output format

SECTION .text
global      _start
_start:
        nop

        ; Sort Array

        mov     esi, array
        mov     ecx, count-1
s1:
        push    ecx

        mov     ax, [esi]
        mov     edi, esi
s2:
        add     edi, 2
        mov     bx, [edi]
        cmp     ax, bx
        jge     noxchg
        mov     [edi], ax
        mov     [esi], bx
        mov     ax, bx
noxchg:
        loop    s2

        add     esi, 2
        pop     ecx
        loop    s1

        ; Show Array

        mov     esi, array
        mov     ecx, count      ; Array length
show:
        push    ecx             ; Backup counter

        mov     eax,0
        mov     ax, [esi]       ; Take a number from array

        ; printf is poping a dword instead of word so 
        ; i have to change negative word to a negative dword
        cmp     eax,0x8000
        jb      positive
        xor     eax,0xFFFF0000

positive:
        push    eax             ; Second printf parameter
        push    format          ; First printf parameter
        call    printf
        add     esp, 8          ; Clear stack after printf

        add     esi, 2          ; Jump to next number in array
        pop     ecx             ; Restore counter
        loop    show


        push    0               ; Return value
        call    exit

BY: Pejman Moghadam
TAG: asm, sort
DATE: 2013-01-07 23:30:37


Pejman Moghadam / Assembly [ TXT ]