Skip to content

Instantly share code, notes, and snippets.

@cypok cypok/bubble.c
Created Feb 12, 2009

Embed
What would you like to do?
#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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.