MASM 会在背后改变指令吗?

发布于 2024-12-07 03:59:01 字数 263 浏览 1 评论 0原文

在阅读 HLA 常见问题解答时,我对以下内容感到惊讶:

MASM 有一个讨厌的习惯,会改变你背后的指令 后退。 HLA似乎也有同样的问题。

然后我在网上搜索,但没有发现任何与此相关的信息。这是真的还是只是一个神话?

I was surprised by the following when reading the HLA faq:

MASM has a nasty habit of changing instructions behind your
back. HLA also seems to have this same problem.

I then searched the web but found nothing regarding this. Is it true or just a myth?

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

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

发布评论

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

评论(4

白馒头 2024-12-14 03:59:01

MASM 可以改变你的一切。例如,它可以修复你的条件跳转。也就是说,如果您编写:

    add ax,bx
    jnz NotZero

    ...
NotZero:

如果 NotZero 超出了条件跳转的范围,MASM 可以将您的代码重写为:

    add ax,bx
    jz Zero
    jmp NotZero
Zero:
    ...
NotZero:

我记得,有命令行开关可以防止这种情况发生。您还可以明确地说,jnz near NotZero 来防止它。如果您这样做并且目标超出了条件范围,您将收到错误。

我记得 MASM 还做过一些其他类似的事情,但已经过去很长时间了。我不记得那些具体的东西是什么。

MASM can change things on you. For example, it can fix your conditional jumps. That is if you write:

    add ax,bx
    jnz NotZero

    ...
NotZero:

If NotZero is outside the range of a conditional jump, MASM can rewrite your code as:

    add ax,bx
    jz Zero
    jmp NotZero
Zero:
    ...
NotZero:

As I recall, there are command line switches to prevent that. You can also specifically say, jnz near NotZero to prevent it. If you do that and the target is outside the range of the conditional, you'll get an error.

I recall that MASM did some other, similar, things, but it's been a very long time. I don't recall what those specific things were.

顾挽 2024-12-14 03:59:01

这有点半真半假。有些指令可以用不止一种方式进行编码,有时(由于他们从未解释过的原因)微软会改变一些特定指令的编码方式。

从版本 6 开始,它还具​​有多通道模式,可以/将使用“到达”目的地的最小跳跃形式对跳跃进行编码。在 5.x 之前,您必须手动指定近跳和远跳,如果您猜错了,在第二遍时会产生“相位错误”。从 v6 开始,它会自动调整大小。由于这会改变指令的大小,因此可能会迫使其他周围的跳转也发生变化(同样,在另一个方向上,较小的跳转编码可能会减少代码大小,以便另一个跳转可以使用较小的编码到达其目的地)。

然而,从大多数人的角度来看,这并不是一个“令人讨厌的习惯”——恰恰相反,大多数人更喜欢它尽可能高效地自动编码指令。在某些情况下(例如,自我修改代码),您需要阻止它,但这些通常属于“令人讨厌的习惯”(至少如果您确实习惯这样做的话) 。

It's kind of half-true. Some instructions can be encoded in more than one way, and at times (for reasons they've never explained) Microsoft has changed how they encode a few particular instructions.

Since version 6, it's also had a multi-pass mode that can/will encode jumps using the smallest form of a jump that will "reach" the destination. Up through 5.x, you had to manually specify near vs. far jumps, and if you guessed wrong, on the second pass it would produce a "phase error". Starting with v6, it'll automatically adjust the size. Since that changes the size of the instruction, that can force other surrounding jumps to change as well (and, likewise, in the other direction, a smaller encoding for a jump may reduce code size that another jump could reach it's destination with a smaller encoding).

This is not, however, a "nasty habit" from most people's perspective -- quite the contrary, most people rather like it to automatically encode the instructions as efficiently as possible. There are a few cases (e.g., self-modifying code) where you need to prevent it, but those are the ones that usually qualify as "nasty habits" (at least if you really do them habitually).

趁微风不噪 2024-12-14 03:59:01

这可能不是您想要的,但我经历过 MASM 不会为相同的输入产生与其他类似汇编器(例如 NASM)相同的输出的情况。也就是说,稍微不同的字节序列分解为相同的东西。

通常,这根本不是问题,我注意到它的唯一原因是因为一种奇怪的情况,我需要指令中的某些位成为特定值;对于一个汇编器来说,情况很好,而对于另一个汇编器来说,则不然,我必须进行手动修复。

希望有帮助。

This probably is not what you're looking for, but I've experienced cases where MASM doesn't produce the same output for the same input as other similar assemblers (NASM, for example). That is, a slightly different byte-sequence that disassembles to the same thing.

Typically, it's not an issue at all though, and the only reason I even noticed it was because of a weird case that I needed certain bits in the instruction to be a particular value; with one assembler it was fine, and with the other, it wasn't and I had to apply manual fix-ups.

Hope that helps.

小情绪 2024-12-14 03:59:01

我认为这个“改变”是关于指令调度和窥视孔优化。当您使用“cat > pgm.o”编写代码时,您可以避免这种情况。

I think this "changing" is about instruction scheduling and peep-hole optimizations. You can escape this when you write your code with "cat >pgm.o".

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