Intel汇编-XCHG指令实现冒泡排序

# An eaxmple of the XCHG instruction

.section .data
values:
    .int 105, 235, 61, 315, 134, 221, 53, 145, 117, 5

output:
    .asciz "The bubble value is %d\n"


.section .text

.globl main

main:
    movl $values, %esi
    movl $9, %ecx
    movl $9, %ebx

loop:
    movl (%esi), %eax
    cmp %eax, 4(%esi)
    jge skip
    xchg %eax, 4(%esi)
    movl %eax, (%esi)

skip:
    add $4, %esi
    dec %ebx
    jnz loop
    dec %ecx
    jz end
    movl $values, %esi
    movl %ecx, %ebx
    jmp loop

end:
    xor %edi, %edi

loop_out:
    movl values(, %edi, 4), %eax    
    push %eax            # esp-4
    push $output            # esp-4
    call printf
    add $8, %esp            # esp+8,回退8个字节
    inc %edi
    cmp $10, %edi
    jne loop_out

    movl $1, %eax
    movl $0, %ebx
    int $0x80


# gcc -o bubble 07-bubble.s -m32
# The bubble value is 5
# The bubble value is 53
# The bubble value is 61
# The bubble value is 105
# The bubble value is 117
# The bubble value is 134
# The bubble value is 145
# The bubble value is 221
# The bubble value is 235
# The bubble value is 315

# C 语言
# for (out = array_size-1; out > 0; out--) {
#     for (in = 0; in < out; in++) {
#         if (array[in] > array[in + 1]
#             swap(array[in], array[in + 1]);
#     }
# }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值