Can I do this in assembly mov eax, [ax]
or must I specify the size mov eax, dword [ax]
.

- 328,167
- 45
- 605
- 847

- 185
- 1
- 2
- 11
-
Depends on the assembler, so, which assembler are you using? – harold Nov 11 '12 at 20:31
-
Using Nasm, and working in 32bit mode – Weigel Gram Nov 11 '12 at 21:11
2 Answers
[ax]
is not a valid 16-bit addressing mode. Change it to mov ebx, [bx]
and you could do it. The ebx
determines the size of the operation, so you wouldn't need to say dword
. In 32-bit mode, [bx]
is unlikely to be a "useful" address, but it's "valid" code. In 32-bit mode, mov al, [eax]
, mov ax, [eax]
and mov eax, [eax]
are all valid, and the sizes are determined by the size of the destination register, but you might want to say "byte", "word", or "dword" for clarity. In this(!) case, the sizes do not have to match.
FWIW, it is possible to use 32-bit instructions - and 32-bit addressing modes - in 16-bit code. The entire address needs to be within the segment limit - usually 64k - but mov eax, [eax + ecx * 4]
is valid code. Nasm (or other "competent" assembler) will generate the required "operand size override prefix" and "address size override prefix" (0x66 and 0x67).
Don't be afraid to try these things... although it may not be clear WHY Nasm is refusing, and if Nasm DOES accept it that doesn't mean it will do what you intend...

- 1,462
- 1
- 8
- 4
-
So the hole problem was that [ax] is not a valid 16-bit addressing mode? – Weigel Gram Nov 11 '12 at 23:06
-
-
I just keep forgetting what the valid 16bit addressing modes are.. oh well – harold Nov 12 '12 at 08:19
-
offset + base + index (all optional), where "base" registers are `bx` and `bp` (with `bp` defaulting to `ss:bp`) and "index" registers are `si` and `di`. Quite limited! In 32-bit modes, any register can be "base" and any but `esp` can be "index", and adds an optional "scale" (2, 4, or 8) to be multiplied by "index". Once we start with 32-bit code, we forget the 16-bit modes... gladly! – Frank Kotler Nov 12 '12 at 16:53
No, those operands don't match. On a 32-bit system, where you can access EAX, you would also have to use the same size for the address.
mov eax, [eax]
would replace the pointer in EAX with the value it points to.

- 90,663
- 31
- 146
- 203