Created
July 21, 2013 19:02
-
-
Save dekay/6049573 to your computer and use it in GitHub Desktop.
Listing 4 from http://www.ibm.com/developerworks/library/l-gas-nasm/ translated to GNU as and written using intel syntax.
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
# Example adapted from http://www.ibm.com/developerworks/library/l-gas-nasm/ | |
# Assemble with: as --gstabs+ -o nasm2gas4.o nasm2gas4.s | |
# Link with: ld --dynamic-linker /lib/ld-linux.so.2 -lc -o nasm2gas4 nasm2gas4.o | |
# | |
# Support intel syntal vs. ATT and don't use % before register names | |
.intel_syntax noprefix | |
.section .data | |
array: .byte 89, 10, 67, 1, 4, 27, 12, 34, 86, 3 | |
.set ARRAY_SIZE, . - array | |
array_fmt: .asciz " %d" | |
usort_str: .asciz "unsorted array:" | |
sort_str: .asciz "sorted aray:" | |
newline: .asciz "\n" | |
.section .text | |
# Program entry point | |
.globl _start | |
_start: | |
push OFFSET FLAT:usort_str | |
call puts | |
add esp, 4 | |
push ARRAY_SIZE | |
push OFFSET FLAT:array | |
push OFFSET FLAT:array_fmt | |
call print_array10 | |
add esp, 12 | |
push ARRAY_SIZE | |
push OFFSET FLAT:array | |
call sort_routine20 | |
# Adjust the stack pointer | |
add esp, 8 | |
push OFFSET FLAT:sort_str | |
call puts | |
add esp, 4 | |
push ARRAY_SIZE | |
push OFFSET FLAT:array | |
push OFFSET FLAT:array_fmt | |
call print_array10 | |
add esp, 12 | |
jmp _exit | |
print_array10: | |
push ebp | |
mov ebp, esp | |
sub esp, 4 | |
mov edx, [ebp + 8] | |
mov ebx, [ebp + 12] | |
mov ecx, [ebp + 16] | |
mov esi, 0 | |
push_loop: | |
mov [ebp - 4], ecx | |
mov edx, [ebp + 8] | |
xor eax, eax | |
mov al, BYTE PTR [ebx + esi] | |
push eax | |
push edx | |
call printf | |
add esp, 8 | |
mov ecx, [ebp - 4] | |
inc esi | |
loop push_loop | |
push OFFSET FLAT:newline | |
call printf | |
add esp, 4 | |
mov esp, ebp | |
pop ebp | |
ret | |
sort_routine20: | |
push ebp | |
mov ebp, esp | |
# Allocate a word of space in stack | |
sub esp, 4 | |
# Get the address of the array | |
mov ebx, [ebp + 8] | |
# Store the array size | |
mov ecx, [ebp + 12] | |
dec ecx | |
# Prepare for ourter loop here | |
xor esi, esi | |
outer_loop: | |
# This stores the min index | |
mov [ebp - 4], esi | |
mov edi, esi | |
inc edi | |
inner_loop: | |
cmp edi, ARRAY_SIZE | |
jge swap_vars | |
xor al, al | |
mov edx, [ebp - 4] | |
mov al, BYTE PTR [ebx + edx] | |
cmp BYTE PTR [ebx + edi], al | |
jge check_next | |
mov [ebp - 4], edi | |
check_next: | |
inc edi | |
jmp inner_loop | |
swap_vars: | |
mov edi, [ebp - 4] | |
mov dl, BYTE PTR [ebx + edi] | |
mov al, BYTE PTR [ebx + esi] | |
mov BYTE PTR [ebx + esi], dl | |
mov BYTE PTR [ebx + edi], al | |
inc esi | |
loop outer_loop | |
mov esp, ebp | |
pop ebp | |
ret | |
_exit: | |
mov eax, 1 | |
mov ebx, 0 | |
int 0x80 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment