3
votes

Why does lea esi, dword [edx + ecx * 4] produce an error when assembled using NASM?

I get the error:

insertion_sort.asm:19: error: mismatch in operand sizes

However, the purpose of lea is just to load the address of dword [edx + ecx * 4], so even thus the reference is for a DWORD value it should not matter?

On the other hand,

mov eax, dword [edi] 

works perfectly fine as expected. Here I declare that [edi] references a DWORD value.

2

2 Answers

3
votes

In the case of lea, the size specifier is not relevant. You're not loading anything from memory - you're just calculating an address (doesn't even need to be something you plan to use as an address - you can use lea for general arithmetic). There would be no difference between lea esi, byte [edx] and lea esi, dword [edx], so there's no point in having a size specifier at all.

It's actually redundant in your mov example, because the size can be deduced from the destination operand. But in the case of mov it's actually relevant in some cases (e.g. mov byte [edi], 1), whereas in the case of lea it isn't.

Note that you can have lea calculate a 16-bit address and zero-extend it into a 32-bit destination register (or calculate a 32-bit address and truncate it into a 16-bit destination register). But the address size is according to Intel's manual determined by "the attribute of the code segment", rather than something you specify in the instruction.

0
votes

After fiddling around with this a little, I believe that the dword prefix is what trips up nasm. Just leave it out and nasm is happy.