Skip to content

Instantly share code, notes, and snippets.

@cypok
Created February 12, 2009 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cypok/62739 to your computer and use it in GitHub Desktop.
Save cypok/62739 to your computer and use it in GitHub Desktop.
#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;
}
.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