学 Win32 汇编[12]: PTR、OFFSET、ADDR、THIS

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.



THIS:


; Test12_5.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
    TextAddr equ this byte   ;伪指令 this 可让当前变量和下一个变量同址 
    szText db 'Asm', 0
.code
main proc
    PrintHex offset szText   ;00403000
    PrintHex offset TextAddr ;00403000
    
    PrintString szText       ;Asm
    mov [TextAddr], 'a'      ;给 TextAddr 赋值
    PrintString szText       ;asm
    ret
main endp
end main

你可能感兴趣的:(Win32)