# 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]);
# }
# }