X86 Assembly/Data Transfer
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Data transfer instructions
[edit] Move
| mov src, dest | GAS Syntax |
| mov dest, src | Intel syntax |
Move
The mov instruction copies the src operand into the dest operand.
Operands
src
- Immediate
- Register
- Memory
dest
- Register
- Memory
Modified flags
- No FLAGS are modified by this instruction
Example
.data
value:
.long 2
.text
.global _start
_start:
movl $6, %eax
# %eax is now 6
movw %ax, value
# value is now 6
movl $0, %ebx
# %ebx is now 0
movb %al, %bl
# %ebx is now 6
movl value, %ebx
# %ebx is now 6
movl $value, %esi
# %esi is now the address of value
xorl %ebx, %ebx
# %ebx is now 0
movw value(, %ebx, 1), %bx
# %ebx is now 6
# Linux sys_exit
mov $1, %eax
xorl %ebx, %ebx
int $0x80
[edit] Data Swap
| xchg src, dest | GAS Syntax |
| xchg dest, src | Intel syntax |
Exchange
The xchg instruction swaps the src operand with the dest operand.
Operands
src
- Register
- Memory
dest
- Register
- Memory
Modified flags
- No FLAGS are modified by this instruction
Example
.data
value:
.long 2
.text
.global _start
_start:
movl $54, %ebx
xchgl value, %ebx
# %ebx is now 2
# value is now 54
xchgw %ax, value
# Value is now 0
# %eax is now 54
xchgb %al, %bl
# %ebx is now 54
# %eax is now 2
xchgw value(%eax), %ax
# value is now 0x00020000 = 131072
# %eax is now 0
# Linux sys_exit
mov $1, %eax
xorl %ebx, %ebx
int $0x80
[edit] Move and Extend
| movz src, dest | GAS Syntax |
| movzx dest, src | Intel syntax |
Move zero extend
The movz instruction copies the src operand in the dest operand and pads the remaining bits not provided by src with zeros (0).
This instruction is useful for copying an unsigned small value to a bigger register.
Operands
src
- Register
- Memory
dest
- Register
Modified flags
- No FLAGS are modified by this instruction
Example
.data
byteval:
.byte 204
.text
.global _start
_start:
movzbw byteval, %ax
# %eax is now 204
movzwl %ax, %ebx
# %ebx is now 204
movzbl byteval, %esi
# %esi is now 204
# Linux sys_exit
mov $1, %eax
xorl %ebx, %ebx
int $0x80
| movs src, dest | GAS Syntax |
| movsx dest, src | Intel syntax |
Move sign extend.
The movs instruction copies the src operand in the dest operand and pads the remaining bits not provided by src the sign of src.
This instruction is useful for copying a signed small value to a bigger register.
Operands
src
- Register
- Memory
dest
- Register
Modified flags
- No FLAGS are modified by this instruction
Example
.data
byteval:
.byte -24 # = 0xe8
.text
.global _start
_start:
movsbw byteval, %ax
# %ax is now -24 = 0xffe8
movswl %ax, %ebx
# %ebx is now -24 = 0xffffffe8
movsbl byteval, %esi
# %esi is now -24 = 0xffffffe8
# Linux sys_exit
mov $1, %eax
xorl %ebx, %ebx
int $0x80
[edit] Move by Data Size
movsb
Move byte
The movsb instruction copies one byte from the location specified in esi to the location specified in edi. The direction flag is cleared to indicate that the pointers esi and edi should be incremented during the operation. Otherwise if the direction flag is set then the pointers are decremented. In that case the copy would happen in the reverse direction, starting at the highest address and moving toward lower addresses until ecx is zero.
Operands
None.
Modified flags
- No FLAGS are modified by this instruction
Example
; This code is for NASM. section .code ; copy mystr into mystr2 mov esi, mystr ; loads address of mystr into esi mov edi, mystr2 ; loads address of mystr2 into edi cld mov cx,6 rep movsb section .bss mystr2: resb 6 section .data mystr db "Hello", 0x0
movsw
Move word
The movsw instruction copies one word (two bytes) from the location specified in esi to the location specified in edi.
Operands
None.
Modified flags
- No FLAGS are modified by this instruction
Example
section .code ; copy mystr into mystr2 mov esi, mystr mov edi, mystr2 cld rep movsw ; due to endianess, the resulting mystr2 would be aAbBcC\0a section .bss mystr2: resb 8 section .data mystr db "AaBbCca", 0x0