在32位MASM中声明变量的技术机制和操作是什么? [英] What are the technical mechanics and operation of declaring variables in 32-bit MASM?
问题描述
在MASM版本11 SDK中使用32位MASM程序集,我在编译期间发现了一个错误.错误指向我在其中声明了双字(dd)大小的变量的行.消息说变量对于我尝试分配给它的字符串来说太小了.当我将变量定义为字节而不是(db)时,程序已编译且没有错误.这意味着用db指令声明变量比声明双倍数据大小可以允许更多的存储.下面是错误消息指向的双字变量声明的代码:
Using 32 bit MASM assembly with the MASM version 11 SDK, I discovered an error during compiling. The error pointed to the line where I declared a variable with a double-word (dd) size. The message said the variable was too small for the string I tried to assign to it. When I defined my variable as a byte instead (db) the program was compiled with no error. This implied that declaring a variable with the db instruction could allow more storage than declaring a double-data size. Below is the code for the declaration of a double-word variable that the error message pointed to:
.data
msg_run dd "Ran a function.", 0
我将msg_run的数据大小更改为一个字节:
I changed the data size of msg_run to a byte:
.data
msg_run db "Ran a function.", 0
当我尝试使用第二行进行编译时,该程序已编译并成功运行.为什么该错误意味着声明为字节大小的变量比声明为双字大小的变量具有更大的容量?尾部的,0"是否有效?
When I tried to compile with the second line, the program compiled and ran with no problems. Why did the error imply that a variable declared to be byte-sized has more capacity than a variable declared to be double-word-sized? Does the trailing " ,0" have any effect?
我查看过的资料来源:
https://www.cs.virginia.edu/〜evans/cs216/guides/x86.html https://www.shsu.edu/~csc_tjm/fall2003/cs272/intro_to_asm.html
推荐答案
具有严格的数据定义语法,要求程序员编写用逗号分隔的每个元素会使声明字符串乏味:
Having a strict data definition syntax that requires the programmer to write each element separated by a comma would make declaring a string tedious:
myString db 'M', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g', 0
因此,MASM(和所有其他主流汇编程序)放宽了其中的语法
so MASM (and all other mainstream assemblers) relaxes the syntax in
myString db "My string", 0
注意我使用引号'
表示字符(即数字),使用双引号"
表示字符串,我不知道MASM使用的确切语法,它可能会转换1 -char字符串转换为char.
Note that I used quotes '
for characters (i.e. numbers) and double quotes "
for strings, I don't know the exact syntax used by MASM and it will possibly convert 1-char string to char.
在dd
情况下看到的内容与上面的简写非常相似,但是它不是声明字符串的语法,实际上,它创建了 numbers .
What you saw with the dd
case looks very similar to the shorthand above but it is not a syntax to declare strings, in fact, it creates numbers.
在需要数字的地方使用"ABCD"之类的字符串时(例如在dd
中或作为立即数),MASM会将其转换为0x44434241
.这些是字符D,C,B,A的值.
之所以进行反向操作,是因为该语法主要用于指令立即执行,例如在mov eax, "ABCD"
或cmp eax, "ABCD"
中.
这样,由于x86的字节序,将eax
存储到内存将创建字符串"ABCD"(以正确的顺序).
这对于检查表的签名也非常有用,因为这些签名旨在在内存中正确拼写,但是,一旦加载到寄存器中,这些签名当然会反转.
When a string like "ABCD" is used where a number is expected (like in a dd
or as an immediate) MASM converts it to 0x44434241
. These are the value of the characters D, C, B, A.
The reversing is done because the syntax is mostly used for instruction immediates, like in mov eax, "ABCD"
or cmp eax, "ABCD"
.
This way, storing eax
to memory will create the string "ABCD" (in the correct order) thanks to the x86 endianness.
This also works great with checking the signatures of tables since these signatures are designed to spell correctly in memory but, of course, reversed once loaded in a register.
在NASM中,您甚至可以用mov eax, ("ABCD" + "EFGH") / 2
之类的东西惹恼所有人,从而增强了这些字符串作为数字的观点.这也应适用于MASM.
In NASM you can even piss everybody off with things like mov eax, ("ABCD" + "EFGH") / 2
, reinforcing the view of these strings as numbers. This should also apply to MASM.
我不记得曾经使用过myVar dd "ABCD"
的情况,但是当结构中的固定字符串在内存中拼写为 reversed 时,这可能会很有用.
I don't remember a case where I've used myVar dd "ABCD"
but it may be useful when a structure has a fixed string that is spelled reversed in memory.