Chapter 4 - Data Movement Instructions
Data movement instructions do not affect flags.
4-1 MOV Revisited
三种操作数模式
模式 | 默认地址大小 | 默认操作数大小 |
---|---|---|
16-bit模式 | 16-bit | 16-bit |
32-bit保护模式 | 32-bit | 32-bit |
64-bit模式 | 64-bit | 32-bit |
在 code segment descriptor 中, L-bit and D/B-bit indicate the operation mode:
- L=1 for 64-bit instruction mode
- L=0 and D/B =1 for 32-bit instruction mode
- L=0 and D/B =0 for 16-bit instruction mode
指令不能超过 15B,否则发生 general-protection exception
如何分辨 - 操作数前缀
指令前缀
REX 前缀
Legacy Prefixes - 4 组,同组只能存在一个
- Group 1
- LOCK - 原子操作,只有某些指令能用,否则 undefined opcode exception (#UD) occurs
- REPNE/REPNZ - Repeat 相应 string instruction rCX 次
- REP or REPE/REPZ
- Group 2: segment override prefix
- CS/ES/SS/FS/DS/GS segment override
- 有默认值 Instructions: CS, Local Data: DS, Stack: SS, Destination Strings: ES
- Group 3: Operand-size override prefix
- REX (REX.W) prefix - 64 位操作数
- 66H prefix - 16 位
- Group 4: Address-size override prefix
Escape sequence/Opcode
- 增加操作码长度来提供其他操作码(1B to 2B)
4–2 LOAD EFFECTIVE ADDRESS
LEA: Loads a near pointer (offset)
- load 一个偏移地址到 16/32 位寄存器中
LEA BX,[DI]
loads the offset address specified by[DI]
(contents of DI) into the BX registerSEG
andOFFSET
操作返回一个内存位置的段地址和偏移量LEA SI, DATA1
相当于MOV SI, OFFSET DATA1
(DATA1
是一个 label)OFFSET
一般比LEA
快
LDS, LES, LFS, LGS, and LSS: Loads a far pointer (segment selector and offset)
不是基本指令,先跳了
4–3 STRING DATA TRANSFERS
字符串数据传输指令
- LODS
- implicit operands (AL, AX, EAX)
- STOS - 连续内存的初始化
- implicit operands (AL, AX, EAX)
- MOVS - 内存数据之间的传输(x86 中唯一的一条)
- only memory-to-memory transfer
- SI and DI 指向的内存的数据交换
- A suffix (B, W or D) indicates the data size to operate on
字符串比较指令
- SCAS
- CMPS
用到的寄存器和 flag
- DI - ES 段的偏移地址,cannot be overridden
- SI - DS 段的偏移地址,can be overridden
- direction flag (D, located in the flag register) - 通过
CLD
SLD
清零/设定- D=0, auto-increment
- D=1, auto-decrement
- REP and CX/ECX - REP 前缀可以让指令重复执行 n 次,n 存在 CX/ECX 中
- Permissible forms with suffix: B - byte, W - word, D - double word
- e.g. MOVSB, byte-sized MOVS
INS - 从设备端口(如 disk drives)取数据到内存
- explicit-operands form -
INS WORD PTR [DI], DX
(必须要 WORD PTR,因为 DX 代表端口,长度不确定) - no-operands form -
INSB
,INSW
,INSD
(目的地址必须放在 DI/EDI,端口一定放在 DX,可省略,需要用后缀指定长度) - 可以用 REP prefix 重复执行
inputs 50 bytes of data from an I/O device:
OUTS - 从内存取数据送到设备端口
- explicit-operands form -
OUTS DX, WORD PTR [SI]
- no-operands form - 类似 INS
4-4 MISCELLANEOUS DATA TRANSFER INSTRUCTIONS
XCHG
- 把一个寄存器和另一个寄存器/内存位置的数据交换
- 不能交换 segment registers or memory-to-memory data
- 用来实现
semaphores
LAHF and SAHF
什么东西,懒得看
XLAT (Table Look-up Translation)
- implicit operands (AL, BX)
- works like
MOV AL, [seg:BX + AL]
- XLAT writes AL without changing EAX[31:8]
IN & OUT
- port
- IN -
IN AL, 19H
: 把 19H 接口处的数据放入 AL - OUT -
OUT 32H, AX
: 把 AX 的数据放到输出端口 32H - Fixed-port addressing: 输入输出端口是一个长度为 8bit 的地址
- Variable-port addressing:
IN AL, DX, OUT DX, AX
(16-bit)
MOVSX & MOVZX - move and sign-extend/move and zero-extend
BSWAP
- reverses the byte order
- 大小端转换
CMOV
- CMOVcc - 当 condition code (cc) 被满足时执行 MOV
CMOVZ
- ZF = 1 时 MOV- The purpose of CMOV is to avoid a branch
- 把控制依赖转换成数据依赖,extends instruction scheduling space
4–5 ASSEMBLER DETAIL
Directives vs Instructions
- Directives: tell assembler how to do
- Instructions: tell CPU what to do
Directives in MASM
- Data Allocation – DB, DW, DD, DQ, DT
DUP
可以初始化一个数据多次DB 100 DUP(6)
- reserves 100 bytes of 6
- Structure – STRUCT, RECORD
- Code Labels – ALIGN, ORG
- ALIGN directive aligns the next data element or instruction on an address that is a multiple of its parameter (必须是 2 的整数次方)
- Segment – SEGMENT, ENDS, ASSUME
- Simplified Segment – .CODE, .DATA, .STACK, .MODEL, .EXIT
- Procedures – PROC, ENDP
- 定义函数
- Macros – MACRO, ENDM
- inline expansion
- inline expansion
- Miscellaneous – EQU, INCLUDE
CONSTANT_NAME EQU expression
- e.g.TEN EQU 10
, TEN 会被替换成 10- THIS BYTE, THIS WORD, THIS DWORD, or THIS QWORD
ORG
- change the starting offset addressASSUME
- tells the assembler what names have been chosen for the code, data, extra, and stack segments
MACRO and ENDM - 宏
Memory Organization