汇编语言学习系列 冒泡排序实现

假如汇编语言要实现如下C语言的功能,编译环境Ubuntu14.04(32位)。

#include<stdio.h>



void swap(int *p, int *q)

{

    int tmp = *p;

    *p = *q;

    *q = tmp;

}



int main()

{

    int a[] = {3, 0, 5, 1, 4, 6, 2, 9, 8, 7};

    

    int i ,j;

    

    for(i = 0; i < 10; i++)

    {

        for(j = i + 1; j < 10;j++)

        {

            if(a[i] > a[j]){

                    swap(&a[i], &a[j]);

            }

        }

    }

    

    int k;

    for(k = 0; k < 10; k++)

    {

        printf("%d\n", a[k]);

    }

    

    return 0;

}
  • 汇编代码sort.s
.section .data

        array: .int 3, 0, 5, 1, 4, 6, 2, 9, 8, 7

        len: .int 10

        format: .asciz "%d\n"

.section .text

.global _start

_start:

        pushl %ebp

        movl %esp, %ebp

        subl $20, %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 stack

        

        call sort

        

        call parray

        

        movl $0, (%esp)    #deallocate space

        call exit

sort:                    #3, 0, 5, 1, 4, 6, 2, 9, 8, 7

        pushl %ebp

        movl %esp, %ebp

        pushl %ebx

        subl $20, %esp  #allocate space

        

        movl 8(%ebp), %edx    #get &array

        movl 12(%ebp), %ebx    #get len

        

        movl $0, %ecx    #init i=0

        cmp %ecx, %ebx

        jle .done



        leal 1(%ecx), %eax #init j=i+1



        cmp %eax, %ebx

        jle .L1    

.L2:

        movl (%edx, %ecx, 4), %esi    #get a[i]

        movl (%edx, %eax, 4), %edi    #get a[j]

        

        cmp %edi, %esi

        

        jl .C1

        

        leal (%edx, %ecx, 4), %esi

        movl %esi, (%esp)

        leal (%edx, %eax, 4), %esi

        movl %esi, 4(%esp)        

        

        call swap

        inc %eax

        cmp %eax, %ebx

        jle .L1        

        jmp .L2    

.C1:

        inc %eax

        cmp %eax, %ebx

        jle .L1

        

        jmp .L2

.L1: 

        inc %ecx

        cmp %ecx, %ebx

        jle .done

        

        leal 1(%ecx), %eax #init j=i+1

        cmp %eax, %ebx

        jle .L1

        jmp .L2

swap:

        pushl %ebp

        movl %esp, %ebp

        pushl %eax

        pushl %ebx

        pushl %ecx

        pushl %edx

        

        movl 8(%ebp), %edx    #get p

        movl 12(%ebp), %ecx    #get q

        

        movl (%edx), %ebx   

        movl (%ecx), %eax    

        

        movl %eax, (%edx)

        movl %ebx, (%ecx)

        

        popl %edx

        popl %ecx

        popl %ebx

        popl %eax

        

        popl %ebp

        ret    

.done:

        addl $20, %esp

        popl %ebx

        popl %ebp

        ret    



parray:  #打印数组

        pushl %ebp

        movl %esp, %ebp

        push %ebx        

        

        movl 8(%ebp), %edx    #get &array

        movl 12(%ebp), %ebx    #get len

        

        movl $0, %ecx

        cmp %ecx, %ebx

        jle .done2

.loop:

        movl (%edx, %ecx, 4), %eax

        

        call print

        

        inc %ecx

        cmp %ecx, %ebx

        jg .loop        

.done2:

        popl %ebx

        popl %ebp

        ret    

print:

        pushl %edx

        pushl %ecx

        

        pushl %eax

        pushl $format

        call printf

        

        addl $8, %esp

        popl %ecx

        popl %edx

        

        ret
  • 编译

 as sort.s -o sort.o

  • 链接

ld -lc -I /lib/ld-linux.so.2 sort.o -o sort

  • 执行

 ./sort

你可能感兴趣的:(冒泡排序)