Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

80x86 16-bit asm: lea cx, [cx*8+cx] causes error on NASM (compiling .com file)

The error NASM gives (despite my working OS) is "invalid effective address".

Now I've seen many examples of how to use LEA and I think I got 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 knowledgeable who could explain me WHY my assembler doesn't let me use lea the way I supposed it should be used?

like image 845
larz Avatar asked Apr 26 '10 19:04

larz


2 Answers

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.

If your code will run on 386 or later in 16bit mode, you can use lea cx, [ecx + 9] (address-size prefix but still 16bit operand-size).

See also this Q&A on x86 addressing modes (mostly discussing 32/64bit addressing modes, and the x86 tag wiki.

like image 59
Michael Avatar answered Sep 20 '22 00:09

Michael


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.

like image 31
I. J. Kennedy Avatar answered Sep 21 '22 00:09

I. J. Kennedy