Friday, April 4, 2008

Sorting in ASM - Non Optimized snippet

# sorting - no optimized snippet

.section .data
list: .long 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
dis: .ascii "\0\0\0\0\0"
newline: .ascii "\n\0"
space: .ascii " \0"
bsm: .ascii "Before sorting ... \n\0"
asm: .ascii "\nAfter sorting ... \n\0"

.section .text
.globl _start

_start:

pushl $bsm
pushl $20
call _printstr
addl $8, %esp

movl $10, %ecx
movl $0, %edi
bs:
cmpl $0, %ecx
je end_bs

movl list(, %edi, 4), %eax
# print the list
# *****************************************
pushl $dis
pushl %eax
call _int2str
addl $8, %esp

pushl $dis
pushl $1
call _printstr
addl $8, %esp

decl %ecx
incl %edi
jmp bs
# ****************************************
# end of print list

end_bs:

pushl $list
pushl $10
call _sort
addl $8, %esp

pushl $asm
pushl $21
call _printstr
addl $8, %esp

movl $10, %ecx
movl $0, %edi
as:
cmpl $0, %ecx
je end_as

movl list(, %edi, 4), %eax

# print the list
# *****************************************
pushl $dis
pushl %eax
call _int2str
addl $8, %esp

pushl $dis
pushl $1
call _printstr
addl $8, %esp

decl %ecx
incl %edi
jmp as
# ****************************************
# end of print list

end_as:
pushl $newline
pushl $1
call _printstr
addl $8, %esp

call _exit


.type _sort, @function
_sort:
pushl %ebp
movl %esp, %ebp
subl $20, %esp

# -4 = outer loop counter, -8 = = inc of 4, -12 = addr1, -16 = addr2, -20 = main loop

movl 12(%ebp), %esi # address of the list
movl 8(%ebp), %edi # number of items

movl $0, -8(%ebp) # incrementer of 4
movl $0, -12(%ebp)

movl %edi, -4(%ebp)
movl %edi, %ecx # outer loop counter
movl %ecx, -20(%ebp) # main loop counter

main_loop:
movl -20(%ebp), %ecx
cmpl $0, %ecx
je end_main_loop
decl %ecx
movl %ecx, -20(%ebp)

movl 12(%ebp), %esi
movl 8(%ebp), %edi

movl %edi, -4(%ebp)
movl %edi, %ecx
movl $0, -8(%ebp)

outer:
movl -4(%ebp), %ecx
cmpl $0, %ecx
#je end_outer
je main_loop

decl %ecx
movl %ecx, -4(%ebp)

movl -8(%ebp), %edx
addl %edx, %esi
movl (%esi), %eax # retrieve data
movl %esi, -12(%ebp) # store addr1

cmpl $0, -4(%ebp)
je yes
addl $4, %esi # next address jump
movl (%esi), %ebx # retrieve next data
movl %esi, -16(%ebp) # store addr2

movl 12(%ebp), %esi # restore base address

cmpl %eax, %ebx
jl less
jmp not_less
less:
xchg %eax, %ebx
movl -12(%ebp), %esi
movl %eax, (%esi)
movl -16(%ebp), %esi
movl %ebx, (%esi)
movl 12(%ebp), %esi # restore base address
not_less:

yes:
movl -8(%ebp), %edx
addl $4, %edx
movl %edx, -8(%ebp)

jmp outer

end_main_loop:
end_outer:
movl %ebp, %esp
popl %ebp
ret



.type int2str, @function
_int2str:
pushl %ebp
movl %esp, %ebp

# store original registers
pushl %eax
pushl %ebx
pushl %ecx
pushl %edx
pushl %esi
pushl %edi

movl 8(%ebp), %eax # original number
movl $0, %ecx # count, for length of the string
movl $10, %edi # the divisor, base

start_conversion:
movl $0, %edx
divl %edi
addl $'0', %edx
pushl %edx
incl %ecx
cmpl $0, %eax
je end_conversion
jmp start_conversion

end_conversion:
movl 12(%ebp), %edx # stores the address of the buffer

start_reversing:
popl %eax
movb %al, (%edx)
incl %edx
decl %ecx
cmpl $0, %ecx
je end_reversing
jmp start_reversing

end_reversing:
movb $0, (%edx) # null terminating character

# retirieve original registers
popl %edi
popl %esi
popl %edx
popl %ecx
popl %ebx
popl %eax

movl %ebp, %esp
popl %ebp
ret

.type printstr, @function
_printstr:
pushl %ebp
movl %esp, %ebp

# store original registers
pushl %eax
pushl %ebx
pushl %ecx
pushl %edx
pushl %edi
pushl %esi


movl $4, %eax
movl $1, %ebx
movl 12(%ebp), %ecx # the string
movl 8(%ebp), %edx
int $0x80

# retirieve original registers
popl %esi
popl %edi
popl %edx
popl %ecx
popl %ebx
popl %eax

movl %ebp, %esp
popl %ebp
ret

.type _exit, @function
_exit:
pushl %ebp
mov %esp, %ebp

pushl %eax
pushl %ebx

movl $1, %eax
movl $0, %ebx
int $0x80

popl %ebx
popl %eax

movl %ebp, %esp
popl %ebp
ret

No comments: