NASM 大会 - 什么是“,0"?这个变量之后呢? [英] NASM Assembly - what is the ", 0" after this variable for?

查看:59
本文介绍了NASM 大会 - 什么是“,0"?这个变量之后呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就在我按照指南在汇编中使用 MessageBoxA 函数之前,在创建变量时,他们在变量内容后使用了, 0".这是干什么用的?

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?

代码如下:

paramText  db  "this is text", 0

推荐答案

db 是定义字节",此代码将产生这些字节(以十六进制格式):

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

引号中的字符串"被拆分为单个字符的 ASCII 字符代码(我相信 NASM 中的 UTF8 也是可能的,所以一个字符可能会产生多个字节),最后一个值, 0" 被编译为那个,为零.

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.

db 1, 2, 3 将产生 3 个机器码字节 01 02 03.

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

零放在最后一个字母后作为字符串空终止符"供其他代码使用,它接受以零结尾的字符串(如MessageBoxA代码).

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).

变量"是编程中相当高级的概念,机器并不直接支持它,编译时得到的是符号/符号名称"paramText,它等于在它之后定义的第一个字节的内存地址(那个 0x74)= 这可以在编译时使用来处理该地址.然后 db 生成实际的二进制机器代码 = 它将在运行时存在,作为操作系统加载到内存中的值.如果你想这样想的话,最后一个零是这个上下文中可变内容"的一部分(不是之后").

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.

但它只是计算机内存中的二进制值.变量"逻辑(包括类型和/或格式)是由代码创建的,它使用内存进行操作,并由编程语言 + 编译器创建,允许在源代码中使用这种结构,但 CPU 本身并不知道这个概念, 并且它仅对位进行操作(通常分为字节、字等).

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, ...).

您实际上也可以在 NASM 中使用类似 C 的字符串转义值,但字符串必须用反引号括起来,例如:

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`

引号和撇号不会扫描转义序列的字符串文字,并且会将\0"编译为两个字符.但是使用 ", 0" 作为下一个字节定义更容易记住 IMO,而不是记住反引号/引号定义的字符串之间的区别,这是 NASM 特定的功能,并非所有 x86 汇编程序都支持.

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.

这篇关于NASM 大会 - 什么是“,0"?这个变量之后呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆