views:

219

answers:

2

Title says it all. The error NASM gives (dispite my working OS) is "invalid effective address".

Now i've seen many examples of how to use LEA and i think i gots it right but yet my NASM dislikes it. I tried "lea cx, [cx+9]" and it worked; "lea cx, [bx+cx]" didn't.

Now if i extended my registers to 32-bits (i.e. "lea ecx, [ecx*8+ecx]") everything would be well but i am restricted to use 16- and 8-bit registers only.

Is here anyone so knoweledgeable who could explain me WHY my assembler doesn't let me use lea the way i supposed it should be used?

Thanks.

Edit

Thank you for the link. How ever the 0x67 prefix confused me: It allows me to use 32-bit addresses thus overriding real mode's 1MB memory limitation??? I it still doesn't enable all possible register conbinations for e.g. LEA...

-Larz

You can override real mode's 1MB memory limitation even with 16 bytes, for instance with this sequence: mov ax, 0xffff; mov ds, ax; mov bx, ds:[ax]. Now ds:[ax] = 10FFEF > 0xfffff, so you've accessed more than 1MB. – Nathan Fellman

+5  A: 

This is because [bx+cx] isn't valid in any addressing mode on 16-bit x86, see this site for more info.

lea cx, [bx+di] or lea cx, [bx+si] should work.

Michael
+2  A: 

lea cx,[cx*8+cx] doesn't work because "scale-index-base" addressing is only available with 32-bit registers. It's not a limitation of the assembler--it's a limitation of the processor.

I. J. Kennedy
Not true. It's available with 16-bit registers as well using the 0x67 prefix - address size override. The problem is that the you can't use all combinations of registers.
Nathan Fellman
Nathan, it's a bit complicated, but I really don't think you can get lea cx,[cx*8+cx]. The opcode for this instruction is 8D 0C C9. If you're using 16-bit addressing, either when running in a 16-bit code segment, or a 32-bit code segment with an address override, the instruction "devolves" into lea cx,[si] (the 8D 0C part), and the C9 byte will be interpreted as a "leave" instruction.Such is the legacy of the x86 instruction format.
I. J. Kennedy
You're right. There's no SIB byte in 16-bit addressing.
Nathan Fellman