NASM Assembly - what is the “, 0” after this variable for?

大兔子大兔子 提交于 2021-02-04 07:25:10

问题


Just before I was following a guide to use the MessageBoxA function in assembly, and when creating variables, they used a ", 0" after the variable contents. What is this for?

The code looks like this:

paramText  db  "this is text", 0

回答1:


db is "define byte" and this code will produce these bytes (in hexadecimal formatting):

74 68 69 73 20 69 73 20 74 65 78 74 00

The "string" in quotes is split into ASCII character codes (UTF8 in NASM is possible too I believe, so then one character may produce multiple bytes) for individual characters, and the last value ", 0" is just compiled as that, as zero.

I.e. db 1, 2, 3 would produce 3 machine code bytes 01 02 03.

The zero is put after last letter as "string nul terminator" for the use by other code, which accepts strings terminated by zero (like MessageBoxA code).

The "variable" is quite high level concept in programming, and the machine doesn't directly support it, what you get at compile time is "symbol/symbolic name" paramText, which equals to the memory address of first byte defined after it (that 0x74) = this can be used at compile time to work with that address. Then the db produces actual binary machine code = which will exist at runtime, as values loaded into memory by OS. And the last zero is part of the "variable content" in this context (not "after" it), if you want to think about it like that.

But it's just binary values in computer memory. The "variable" logic (including the type and/or formatting) is created by the code, which manipulates with memory, and by the programming language + compiler, allowing such constructs in the source code, but CPU itself is not aware of that concept, and it operates only with bits (usually grouped into bytes, words, ...).


EDIT: you can actually use C-like string escaped values in NASM too, but the string must be enclosed in backticks, like:

paramText:  db  `this is text\0`

Quotes and apostrophes don't scan the string literal for escape sequence, and would compile "\0" as two characters. But using ", 0" as next byte definition is IMO easier to remember, than to remember the difference between backtick/quote defined strings which is NASM-specific feature, not supported by all x86 assemblers.




回答2:


It is literally the byte zero, most likely for the purpose of creating a null-terminated string.




回答3:


The MessageBoxA function is expecting a null-terminated byte string. The zero is simply the null-terminator that tells all C functions accepting string where the string ends.



来源:https://stackoverflow.com/questions/51081447/nasm-assembly-what-is-the-0-after-this-variable-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!