Tuesday, July 14, 2009

C programming calling pentium assembly x86 to do bubble sort?

Hi.. I need a program that is using C programming to call assembly to do bubble sort to sort integer from smallest to biggest and return the answer back to C. Anyone can help? Thanks

C programming calling pentium assembly x86 to do bubble sort?
Why? C compiles to just about as efficient code as assembly language. C compiles into assembly language before it compiles into machine code.





Also a bubble sort is not the best sort. Use qsort() to do an in-memory sort of items. It implements the quick sort algorithm.


--------


test.c


____


int compare(int *a, int *b)


{


return *a - *b;


}





int myfunc(int *array, int n_elements)


{


qsort(array, n_elements, sizeof(int), compare);


}








Here is the assembler code generated when I compiled the C code with the -S option.


----


test.s


-----


.file "test.c"


.version "01.01"


gcc2_compiled.:





.text


.align 4


.globl compare


.type compare,@function


compare:


pushl %ebp


movl %esp,%ebp


movl 8(%ebp),%eax


movl 12(%ebp),%edx


movl (%eax),%eax


movl (%edx),%ecx


movl %eax,%edx


subl %ecx,%edx


movl %edx,%eax


jmp .L2


.p2align 4,,7


.L2:


leave


ret


.Lfe1:


.size compare,.Lfe1-compare


.align 4


.globl myfunc


.type myfunc,@function


myfunc:


pushl %ebp


movl %esp,%ebp


subl $8,%esp


pushl $compare


pushl $4


movl 12(%ebp),%eax


pushl %eax


movl 8(%ebp),%eax


pushl %eax


call qsort


addl $16,%esp


.L3:


leave


ret


.Lfe2:


.size myfunc,.Lfe2-myfunc


.ident "GCC: (GNU) 2.95.3 20010315 (release)"





-----

peacock plant

No comments:

Post a Comment