The implementation of assembly language learning series bubble sort

Source: Internet
Author: User

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;}
    • Assembly Code SORT.S
. 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
    • Compile

As Sort.s-o SORT.O

    • Link

Ld-lc-i/lib/ld-linux.so.2 Sort.o-o Sort

    • Perform

./sort

The implementation of assembly language learning series bubble sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.