# Assembly 寻址模式和MOV指令

• 寄存器寻址

• 立即寻址

• 存储器寻址

## 寄存器寻址

```MOV DX, TAX_RATE ; Register in first operand
MOV COUNT, CX ; Register in second operand
MOV EAX, EBX ; Both the operands are in registers```

## 立即寻址

```BYTE_VALUE  DB  150    ; A byte value is defined
WORD_VALUE  DW  300    ; A word value is defined
MOV  AX, 45H           ; Immediate constant 45H is transferred to AX```

## 直接存储器寻址

```ADD	BYTE_VALUE, DL ; Adds the register in the memory location
MOV	BX, WORD_VALUE ; Operand from the memory is added to register```

## 直接偏移量寻址

```BYTE_TABLE DB 14, 15, 22, 45 ; Tables of bytes
WORD_TABLE DW 134, 345, 564, 123 ; Tables of words```

```MOV CL, BYTE_TABLE[2] ; Gets the 3rd element of the BYTE_TABLE
MOV CL, BYTE_TABLE + 2 ; Gets the 3rd element of the BYTE_TABLE
MOV CX, WORD_TABLE[3] ; Gets the 4th element of the WORD_TABLE
MOV CX, WORD_TABLE + 3 ; Gets the 4th element of the WORD_TABLE```

## 间接寻址

```MY_TABLE TIMES 10 DW 0  ; Allocates 10 words (2 bytes) each initialized to 0
MOV EBX, [MY_TABLE]     ; Effective Address of MY_TABLE in EBX
MOV [EBX], 110          ; MY_TABLE[0] = 110
ADD EBX, 2              ; EBX = EBX +2
MOV [EBX], 123          ; MY_TABLE[1] = 123```

## MOV指令

### 语法:

MOV指令的语法是：

`MOV  destination, source`

MOV指令可以具有以下五种形式之一：

```MOV  register, register
MOV  register, immediate
MOV  memory, immediate
MOV  register, memory
MOV  memory, register```

• MOV操作操作数应该是同样大小

• 源操作数的值保持不变

MOV指令产生引起歧义次数。例如，下面语句：

```MOV  EBX, [MY_TABLE] ; Effective Address of MY_TABLE in EBX
MOV [EBX], 110 ; MY_TABLE[0] = 110```

BYTE 1
WORD 2
DWORD 4
QWORD 8
TBYTE 10

### 例子:

```section	.text
global  _start    ;must be declared for linker (ld)

;writing the name 'Zara Ali'
mov	edx,9       ;message length
mov	ecx, name   ;message to write
mov	ebx,1       ;file descriptor (stdout)
mov	eax,4       ;system call number (sys_write)
int	0x80        ;call kernel

mov	[name],  dword 'Nuha'    ; Changed the name to Nuha Ali
;writing the name 'Nuha Ali'
mov	edx,8       ;message length
mov	ecx,name    ;message to write
mov	ebx,1       ;file descriptor (stdout)
mov	eax,4       ;system call number (sys_write)
int	0x80        ;call kernel
mov	eax,1       ;system call number (sys_exit)
int	0x80        ;call kernel

section	.data
name db 'Zara Ali '```

`Zara Ali Nuha Ali`