为什么汇编器不报告错误?

发布于 2024-10-18 20:29:14 字数 212 浏览 2 评论 0原文

考虑一个非常简单的汇编语言程序中的以下两条语句:

DATA1 DB  +200
DATA2 DB  -130

当我尝试汇编它时,汇编器在第 2 条语句上给出错误,因为字节可以容纳超过 -128 十进制的错误。但是为什么汇编器没有在 no 1 语句上给出错误呢?毕竟,一个字节最多可以容纳 127 个正符号整数。相反,将值 C8 放入该字节中。

Consider the following two statements out of a very simple assembly language program:

DATA1 DB  +200
DATA2 DB  -130

when I try to assemble it, assembler gives error on no 2 statement, as it should since a byte can hold beyond -128 decimal. But why assembler didn't give error on no 1 statement? afterall, a byte can hold max 127 positive signed integer.. instead assemlber put the value C8 in that byte.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眸中客 2024-10-25 20:29:14

任何数字在汇编成可执行文件时都会转换为位数组。例如,-1 是 0xFF,-2 是 0xFE,等等。-1 和 255 之间的唯一区别在于它在代码中的使用方式。汇编器并不关心,它只是想存储数据供您使用。

Any number is converted to an array of bits when it is assembled into the executable. -1, for example, is 0xFF, -2 is 0xFE, etc. The only difference between -1 and 255 is how it is used in your code. The assembler doesn't care, it just wants to store the data for you to use.

那支青花 2024-10-25 20:29:14

也许它不知道文字是有符号的还是无符号的。对于汇编器来说,我并不觉得这太令人惊讶,两者都有用例。

-130 永远不适合一个字节,因为它必须有符号并且小于 -128。另一方面,200 非常适合无符号字节,这似乎确实是汇编器所采用的视图,如果将 0xC8 解释为无符号字节,则 0xC8 就是 200。

Perhaps it doesn't know whether or not the literal is signed or unsigned. For an assembler, I don't find that too surprising, there are use-cases for both.

-130 never fits into a byte, since it must be signed and is smaller than -128. 200, on the other hand, fits just fine into an unsigned byte, and that does seem to be the view the assembler takes, 0xC8 is 200 if interpreted as an unsigned byte.

面犯桃花 2024-10-25 20:29:14

所以要点是:

字段的内容意味着您想要它们的意思。所有这一切的结果是,您必须清楚地了解程序将处理的数字的大小,并且必须相应地定义字段大小。

Peter Abel 的《IBM PC 汇编语言和编程》。

同样来自同一作者“ADD和SUB指令不区分无符号和有符号数据,实际上,只是简单地添加和减去位”

so the gist is:

The content of a field mean whatever you intend them to mean. The upshot of all this is that you must have a good idea as to the magnitude of the numbers that your program will process, and you must define field sizes accordinly.

Peter Abel's "IBM PC assembly language and programming".

Also from the same author"ADD and SUB instructions do not distinguish between unsigned and signed data and, indeed, simply add and subtract bits"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文