PTR: 指定要操作的數據尺寸
; Test12_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
val db 11h, 22h, 33h, 44h, 55h, 66h, 77h, 88h
.code
main proc
xor eax, eax ;清空 EAX, 同 mov eax, 0
mov eax, dWord ptr val ;
PrintHex eax ;44332211
xor eax, eax ;
mov eax, dWord ptr val+1 ;
PrintHex eax ;55443322
xor eax, eax ;
mov ax, word ptr val ;
PrintHex eax ;00002211
xor eax, eax ;
mov al, byte ptr val ;
PrintHex eax ;00000011
ret
main endp
end main
OFFSET: 獲取全局變量或標號的偏移地址
; Test12_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
v1 db 'abcdefg', 0
v2 dd 11223344h
.code
main proc
PrintHex offset v1 ;00403000
PrintHex offset v2 ;00403008
PrintHex offset main ;00401000 - 這裡的 main 是個標號
ret
;本例中的 offset 不能用 addr 代替
main endp
end main
ADDR: 類似 offset 也是獲取變量的地址...
; Test12_3.asm
.386
.model flat, stdcall
;include Windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
.data
v1 dd 00434241h ;ABC
v2 dd 00636261h ;abc
.code
main proc
invoke MessageBox, 0, offset v1, offset v2, 0 ;現在 v1、v2 是全局變量
invoke MessageBox, 0, addr v2, addr v1, 0 ;使用 offset 和 addr 均可
invoke ExitProcess, 0
main endp
end main
獲取局部變量的地址只能使用 ADDR:
; Test12_4.asm
.386
.model flat, stdcall
;include Windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
.code
main proc
LOCAL v1,v2
mov v1, 00434241h
mov v2, 00636261h
;invoke MessageBox, 0, offset v1, offset v2, 0 ;offset 不能獲取局部變量的地址
invoke MessageBox, 0, addr v2, addr v1, 0
invoke ExitProcess, 0
main endp
end main
OFFSET 和 ADDR 的異同:
1、offset 不能獲取局部變量的地址;
2、addr 只能用於調用函數(invoke)時, 不能用於賦值操作;
3、addr 面對局部變量時會轉換為 lea 等指令, addr 面對全局變量時則直接調用 offset;
4、在 invoke 中應盡量使用 addr, 其他只用 offset.