王晓博 2010.05.15-1 汇编 冒泡算法

作者:2008级嵌入式  王晓博

#将下面汇编代码保存为 wxb1.s 文件
#功能:用冒泡算法去实现动一组数据的排列,然后输出

.section .rodata
    output: .asciz "ni yao de shu shi %d\n"
            #.asciz是在定义字符串的时侯在字符串结尾加上空字符(即C语言
            #的\0),这样做的目的是为了让printf能读懂字符串

.section .data
    msg: .long 55555555,33333,53,56,25,52,56,631,5667    #共有9个数

.section .text
.globl _start

_start:
    movl $9,%ecx
    decl %ecx        #外循环开始
cp1:
    movl $0,%eax
    pushl %ecx        #保存外循环的ecx值
cp2:                  #内循环
    movl  msg(,%eax,4),%edx        #冒泡算法的开始 保持小数在前大数在后
    incl %eax
    cmpl %edx,msg(,%eax,4)     
 
    jae  next

    xchg  msg(,%eax,4),%edx         #不符合就进行交换
    decl %eax          
    movl %edx,msg(,%eax,4)
    incl %eax                

next:
    loop cp2
    popl  %ecx
    loop cp1
    nop
    movl $0,%edi

loop:                            #用于循环打印共进行9次
    movl msg(,%edi,4),%eax
    pushl %eax
    pushl $output
    call printf
    addl $8 ,%esp
    inc %edi
    cmpl $9,%edi
    jne loop

    movl $0, %ebx        #quit
    mov $1,%eax
    int $0x80


[root@localhost ~]# as wxb1.s -o wxb1.o
[root@localhost ~]# ld wxb1.o -lc -dynamic-linker /lib/ld-linux.so.2  -o wxb1
[root@localhost ~]# ./wxb1
ni yao de shu shi 25
ni yao de shu shi 52
ni yao de shu shi 53
ni yao de shu shi 56
ni yao de shu shi 56
ni yao de shu shi 631
ni yao de shu shi 5667
ni yao de shu shi 33333
ni yao de shu shi 55555555
[root@localhost ~]#



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值