我昨天开始学习MASM32,如果这是一个愚蠢的问题,请耐心等待。
我对addr
和offset
的理解如下:两者都返回变量的内存地址,但offset
仅适用于全局变量(其汇编时知道其内存地址)并且addr
将适用于全局变量和局部变量。我知道更高级语言中的全局和局部变量是什么;我的理解是在.data
,.data?
和.const
块中声明的变量是全局的,并且在过程内使用local
关键字声明的变量是本地的。
我的整个代码是:
.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
.data
szCap db "Hello", 0 ;caption for the MessageBox
szMsg db "Hello, world!", 0 ;text for the MessageBox
.code
;Procedure for creating a MessaheBox with NULL parent and variable text
TestProcedure proc msg:dword, cap:dword
invoke MessageBox, NULL, msg, cap, MB_OK
ret
TestProcedure endp
main:
; error on these lines
push addr szCap ; C:\masm32\My files\HelloWorld.asm(35) : error A2008: syntax error : addr
push addr szMsg ; C:\masm32\My files\HelloWorld.asm(36) : error A2008: syntax error : addr
call TestProcedure
invoke ExitProcess, 0
end main
但是,如果我将addr
更改为offset
,则代码可以正常运行。为什么addr
在这种情况下无法汇编?
答案 0 :(得分:1)
在平面模式下,所有地址都是偏移量。
在最新版本的MASM中添加了ADDR运算符,据我所知,它仅与INVOKE关键字一起使用。在INVOKE中,ADDR与全局标识符的偏移量相同,但也可以计算本地基于堆栈的变量的相对地址(通常它会发出LEA指令)。