Created
February 12, 2009 17:13
-
-
Save cypok/62739 to your computer and use it in GitHub Desktop.
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
#include <stdlib.h> | |
int sort_buble(int *array, size_t size) | |
{ | |
int *r = array + size - 1; | |
int *i; | |
while (array < r) | |
{ | |
for(i = array; i < r; ++i) | |
if( *i > *(i+1) ) | |
{ | |
int t; | |
t = *(i+1); | |
*(i+1) = *i; | |
*i = t; | |
} | |
--r; | |
} | |
return 0; | |
} |
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
.text | |
.globl sort_buble_asm | |
.type sort_buble_asm, @function | |
/* int sort_buble_asm(int *array, size_t size) */ | |
sort_buble_asm: | |
pushl %ebp | |
movl %esp, %ebp | |
movl 8(%ebp), %eax /* eax - array */ | |
movl 12(%ebp), %edx /* edx - size */ | |
leal (,%edx,4), %edx /* edx - size in bytes */ | |
addl %eax, %edx | |
subl $4, %edx /* edx - r */ | |
loop_big: | |
cmp %edx, %eax | |
jnl end_big /* jmp if (array >= r) */ | |
movl %eax, %ecx /* i = array */ | |
/* ecx - i(terator) */ | |
loop_forward: | |
cmp %edx, %ecx | |
jnl end_forward /* jmp if (i >= r) */ | |
movl (%ecx), %esi | |
cmp %esi, 4(%ecx) | |
jnl skip_swap | |
/* swap them */ | |
movl 4(%ecx), %edi | |
movl %esi, 4(%ecx) | |
movl %edi, (%ecx) | |
skip_swap: | |
addl $4, %ecx /* ++i */ | |
jmp loop_forward | |
end_forward: | |
subl $1, %edx | |
jmp loop_big | |
end_big: | |
xor %eax, %eax | |
popl %ebp | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment