x86 assembly extreme novice inquiry: “invalid instruction operands”?

前端 未结 2 756
醉酒成梦
醉酒成梦 2021-01-22 06:20

The code below is only a small fraction of the program I am currently attempting to write, but no other parts of the program are relevant, so I only pasted what was necessary. A

相关标签:
2条回答
  • 2021-01-22 06:55

    In MASM, foo BYTE -1 is treated as declaring a "variable" with a fixed size. Using that symbol later implies an operand-size for instructions that access it.

    So MASM is trying to save you from yourself, stopping you from doing a dword (4-byte) load from a 1-byte variable. This happens even if you have multiple bytes, like foo db "foobar" and want to load multiple characters; that's when mov eax, dword ptr [foo] is useful.

    The other major flavour of Intel-syntax assembly language (NASM), will happily assemble an instruction that loads 4B from [inputLoopCounter], regardless of what inputLoopCounter is a label for.

    In NASM, mov [inputLoopCounter], 0 is a syntax error, because there's no implied operand-size from either operand. (And in MASM, it would be a mov byte ptr [inputLoopCounter], 0.)


    semi-related: Confusing brackets in MASM32 - foo ptr [123] works as an alternative to ds:123 for indicating a memory operand, not an immediate, where insanely [123] would still be an immediate. Also related: Assembly difference between [var], and var


    If MASM allows it in the data section, foo: db ... would just declare a label without an implied size, separate from any data declaration.

    But apparently MASM does not support that in the data section, so you're stuck with variables, unless you want to switch assemblers. How to store 4 characters in a define doubleword in assembly language?

    0 讨论(0)
  • 2021-01-22 07:06

    One possible solution would be to replace inputLoopCounter BYTE -1 by inputLoopCounter DWORD -1.

    0 讨论(0)
提交回复
热议问题