开发者

Does MASM change instructions behind your back?

开发者 https://www.devze.com 2023-04-10 02:32 出处:网络
I was surprised by the following when reading the HLA faq: MASM has a nasty habit of changing instructions 开发者_开发知识库behind your

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?


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.


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消