[地址] 是取地址指向的內容:
; Test14_1.asm
.386
.model flat, stdcall
include Windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
.data
ddVal dd 123
.code
main proc
PrintDec offset ddVal ;4206592 - 這是變量 ddVal 的地址
PrintDec ddVal ;123 - 這是變量值
lea ebx, ddVal ;Lea 是專門獲取地址的指令, 這同 mov ebx, offset ddVal
PrintDec ebx ;4206592
mov eax, [ebx] ;現在 ebx 中放著 ddVal 變量的地址, [ebx] 則表示地址指向的值
PrintDec eax ;123
ret
main endp
end main
地址指向的是什麼數據?
; Test14_2.asm
.386
.model flat, stdcall
include Windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
.data
dwVal dw 123
.code
main proc
lea ebx, dwVal ;把變量 dwVal 的地址放到 ebx
;但現在 dwVal 中放著的是 word 類型的數據, 而 [ebx] 默認是取 32 位的數據
;這可通過偽指令 ptr 指定數據大小
;同時需要把接收著換成 16 的(如 ax), 因為 mov 要求兩個操作數的大小須一致
xor eax, eax ;清空 eax
mov ax, Word ptr [ebx]
PrintDec eax ;123
;也可以直接使用 movzx, 它可以從小到大(movzx r16/r32, r/8/r16/m8/m16)
movzx eax, Word ptr [ebx]
PrintDec eax ;123
ret
main endp
end main
[] 一般用於數組:
; Test14_3.asm
.386
.model flat, stdcall
include Windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
.data
dwArr dw 11,22,33
.code
main proc
;把數組 bArr 的起始地址放到 ebx, 並獲取數組元素:
lea ebx, dwArr
movzx eax, Word ptr [ebx]
movzx ecx, Word ptr [ebx+2]
movzx edx, Word ptr [ebx+4]
PrintDec eax ;11
PrintDec ecx ;22
PrintDec edx ;33
;這樣做更好些:
lea ebx, dwArr
mov esi, type dwArr ;把元素大小放在 esi
movzx eax, Word ptr [ebx]
movzx ecx, Word ptr [ebx + esi * 1]
movzx edx, Word ptr [ebx + esi * 2]
PrintDec eax ;11
PrintDec ecx ;22
PrintDec edx ;33
;這樣也行:
lea ebx, dwArr
mov esi, type dwArr ;把元素大小放在 esi
movzx eax, Word ptr [ebx]
movzx ecx, Word ptr [ebx][esi * 1]
movzx edx, Word ptr [ebx][esi * 2]
PrintDec eax ;11
PrintDec ecx ;22
PrintDec edx ;33
;如果直接使用變量就更像高級語言裡的數組了:
lea ebx, dwArr
mov esi, type dwArr ;把元素大小放在 esi
movzx eax, Word ptr dwArr
movzx ecx, Word ptr dwArr[esi * 1]
movzx edx, Word ptr dwArr[esi * 2]
PrintDec eax ;11
PrintDec ecx ;22
PrintDec edx ;33
ret
main endp
end main
當然也可以寫入數組:
; Test14_4.asm
.386
.model flat, stdcall
include Windows.inc
include kernel32.inc
include masm32.inc
include debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib
.data
dwArr dw 11h ,22h, 33h
.code
main proc
DumpMem offset dwArr, sizeof dwArr ;11 00 22 00 33 00
lea ebx, dwArr
mov Word ptr [ebx], 44h
mov Word ptr [ebx+2], 55h
mov Word ptr [ebx+4], 66h
DumpMem offset dwArr, sizeof dwArr ;44 00 55 00 66 00
;或者:
mov Word ptr dwArr, 77h
mov Word ptr dwArr[2], 88h
mov Word ptr dwArr[4], 99h
DumpMem offset dwArr, sizeof dwArr ;77 00 88 00 99 00
ret
main endp
end main
好像 [] 不能直接是變量或常數(變量後面跟的 [] 除外).