2

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. Anyway, what I am trying to do is move the value stored within inputLoopCounter into ecx in order to determine how many times a loop should execute. However, when I attempt to assemble this program, I get the error mentioned in the question title. Can anybody explain the reason for this?

.data
inputLoopCounter BYTE -1

.code
mov   ecx,inputLoopCounter
Proto
  • 99
  • 5
  • Not an x86 expert, but perhaps the issue is placing a "BYTE" into a register that expects a 32-bit value? Perhaps just load `cl` instead. Actually, may need to use brackets [] as you are loading from memory too. I'll leave it to the real experts who will be here shortly :) – Michael Dorgan May 04 '16 at 18:12
  • 1
    Or `movsx ecx, byte ptr [inputLoopCounter]` if you really want your variable to be a byte but still want to load it into a dword register. – Michael May 04 '16 at 18:13
  • @Michael If it isn't absolutely necessary for it to be a byte, is it possible for me to change BYTE to DWORD in order to make it work? EDIT: Thank you, Jose Manuel Abarca Rodriguez, you answered my question. That was an extremely amateur mistake. – Proto May 04 '16 at 18:26
  • 1
    @JoseManuelAbarcaRodríguez Sure, why not. – Proto May 04 '16 at 18:30

2 Answers2

3

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

0

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847