问题
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
回答1:
One possible solution would be to replace inputLoopCounter BYTE -1
by inputLoopCounter DWORD -1
.
回答2:
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?
来源:https://stackoverflow.com/questions/37034904/x86-assembly-extreme-novice-inquiry-invalid-instruction-operands