movl %eax, %ecx
movl %ebx, %eax
movl %ecx, %ebx
指令 | 描述 |
XCHG | 在两个寄存器之间或者寄存器和内存位置之间交换值 |
BSWAP | 反转一个 32 位寄存器中的字节顺序 |
XADD | 交换两个值并且把总和存储在目标操作数中 |
CMPXCHG | 把一个值和一个外部值进行比较,并且交换它和另一个值 |
CMPXCHG8B | 比较两个 64 位值并且交换它们 |
xchg openrand1, operand2
# swaptest.s - An example of using the BSWAP instruction
.section .data
output1:
.asciz "Before BSWAP,the value in EBX is: 0x%x\\n"
output2:
.asciz "After BSWAP, the value in EBX is: 0x%x\\n"
.section .text
.globl _start
_start:
nop
movl $0x12345678, %ebx
pushl %ebx
pushl $output1
call printf
addl $8, %esp
bswap %ebx
pushl %ebx
pushl $output2
call printf
addl $8, %esp
pushl $0
call exit
$ as -o swaptest.o swaptest.s
$ ld -dynamic-linker /lib/ld-linux.so.2 -lc -o swaptest swaptest.o
$ ./swaptest
Before BSWAP,the value in EBX is: 0x12345678
After BSWAP, the value in EBX is: 0x78563412
xadd source, destination
# xadd.s - An example of the XADD instruction
.section .data
output1:
.asciz "Before XADD, the value in EAX is: %d, the value in EBX is: %d\\n"
output2:
.asciz "After XADD, the value in EAX is: %d, the value in EBX is: %d\\n"
.section .text
.global _start
_start:
nop
movl $7, %eax
movl $8, %ebx
pushl %ebx
pushl %eax
pushl $output1
call printf
addl $12, %esp
movl $7, %eax
movl $8, %ebx
xadd %eax, %ebx
pushl %ebx
pushl %eax
pushl $output2
call printf
addl $12, %esp
pushl $0
call exit
$ as -gstabs -o xadd.o xadd.s
$ ld -dynamic-linker /lib/ld-linux.so.2 -lc -o xadd xadd.o
$ ./xadd
Before XADD, the value in EAX is: 7, the value in EBX is: 8
After XADD, the value in EAX is: 8, the value in EBX is: 15
cmpxchg source, destination
#cmpxchg.s - An example of the cmpxchg instruction
.section .data
data:
.int 10
.section .text
.global _start
_start:
nop
movl $10, %eax
movl $5, %ebx
cmpxchg %ebx, data
movl $1, %eax
int $0x80
cmpxchg8b destination
# cmpxchg8b.s - An example of the cmpxchg8b instruction
.section .data
data:
.byte 0x11, 0x23, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
.section .text
.globl _start
_start:
nop
movl $0x44332211, %eax
movl $0x88776655, %edx
movl $0x11111111, %ebx
movl $0x22222222, %ecx
cmpxchg8b data
movl $0, %ebx
movl $1, %eax
int $0x80
(gdb) info registers
eax 0x44332311 1144202001
ecx 0x22222222 572662306
edx 0x88776655 -2005440939
ebx 0x11111111 286331153
esp 0xbffff480 0xbffff480
Starting program: /home/beyes/programming/assembly/movx/cmpxchg8b
Breakpoint 1, _start () at cmpxchg8b.s:12
12 movl $0x44332211, %eax
Current language: auto
The current source language is "auto; currently asm".
(gdb) n
13 movl $0x88776655, %edx
(gdb) n
14 movl $0x11111111, %ebx
(gdb) n
15 movl $0x22222222, %ecx
(gdb) n
17 cmpxchg8b data
(gdb) n
18 movl $0, %ebx
(gdb) x/8xb &data
0x804909c <data>: 0x11 0x11 0x11 0x11 0x22 0x22 0x22 0x22
# bubble.s - An example of the XCHG instruction
.section .data
values:
.int 105, 235, 61, 315, 134, 221, 53, 145, 117, 5
.section .text
.global _start
_start:
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:
movl $1, %eax
movl $0, %ebx
int $0x80
$ as -gstabs -o bubble.o bubble.s
$ ld -o bubble bubble.o
Breakpoint 1, end () at bubble.s:32
32 movl $1, %eax
Current language: auto
The current source language is "auto; currently asm".
(gdb) x/9d &values
0x80490b0 <values>: 5 53 61 105
0x80490c0 <values+16>: 117 134 145 221
0x80490d0 <values+32>: 235 315
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |