If the assembly language to achieve the following C language functions, the compilation environment Ubuntu14.04 (32-bit).
#include <stdio.h>voidSwapint*p,int*q) { intTMP = *p; *p = *Q; *q =tmp;}intMain () {intA[] = {3,0,5,1,4,6,2,9,8,7}; intI, J; for(i =0; I <Ten; i++) { for(j = i +1; J <Ten; j + +) { if(A[i] >A[j]) {Swap (&a[i], &A[j]); } } } intK; for(k =0; K <Ten; k++) {printf ("%d\n", A[k]); } return 0;}
. Section. Dataarray:.int 3,0,5,1,4,6,2,9,8,7 Len:.int Ten format:. Asciz"%d\n". Section. Text.global _start_start:PUSHL%ebp movl%esp,%EBP Subl $ -,%esp #allocate space movl $array,%eax movl%eax, (%ESP) #store &array on the stack Movl Len,%eax movl%eax,4(%ESP) #store len on the stackPagerSortPagerParray MOVL $0, (%ESP) #deallocate spacePagerExitSort:#3,0,5,1,4,6,2,9,8,7PUSHL%ebp movl%esp,%EBP pushl%ebx Subl $ -,%esp #allocate space Movl8(%EBP),%edx #get &array movl A(%EBP),%ebx #get len Movl $0,%ecx #init i=0 CMP%ecx,%ebxJle. Done Leal1(%ECX),%eax #init j=i+1 CMP%eax,%ebxJle . L1.L2:movl (%edx,%ECX,4),%esi #get A[i] MOVL (%edx,%eax,4),%edi #get A[j]CMP%edi,%esiJL . C1 Leal (%edx,%ECX,4),%esi movl%esi, (%ESP) Leal (%edx,%eax,4),%esi movl%esi,4(%ESP)PagerSwapInc%eaxCMP%eax,%ebxJle . L1jmp . L2.C1: Inc%eaxCMP%eax,%ebxJle . L1jmp . L2.L1: Inc%ecxCMP%ecx,%ebxJle. Done Leal1(%ECX),%eax #init j=i+1 CMP%eax,%ebxJle . L1jmp . L2Swap:PUSHL%ebp movl%esp,%ebp pushl%eax pushl%ebx pushl%ecx PUSHL%edx MOVL8(%EBP),%edx #get p MOVL A(%EBP),%ecx #get q movl (%edx),%ebx movl (%ECX),%eax movl%eax, (%ed x) Movl%ebx, (%ecx) popl%edx popl%ecx popl%ebx popl%eax Pop L%EBPret .Done :Addl $ -,%esp popl%ebx popl%ebpret Parray: #打印数组PUSHL%ebp movl%esp,%EBPPush%EBX MOVL8(%EBP),%edx #get &array movl A(%EBP),%ebx #get len Movl $0,%ecxCMP%ecx,%ebxJle. Done2.Loop:movl (%edx,%ECX,4),%eaxPagerPrintInc%ecxCMP%ecx,%ebxJG. Loop.Done2:popl%ebx popl%ebpret Print:pushl%edx pushl%ecx pushl%eax pushl $formatPagerprintf Addl $8,%esp popl%ecx popl%edxret
As Sort.s-o SORT.O
Ld-lc-i/lib/ld-linux.so.2 Sort.o-o Sort
./sort
The implementation of assembly language learning series bubble sort