Author Topic: Invalid Effective Address  (Read 22940 times)

nobody

  • Guest
Invalid Effective Address
« on: January 08, 2008, 12:56:00 AM »
lea eax, [ebp-ecx*4] gives a compiler error: invalid effective address.
What is wrong ?

nobody

  • Guest
Re: Invalid Effective Address
« Reply #1 on: January 08, 2008, 02:05:11 AM »
> What is wrong ?  

Probably you :-(

> lea eax, [ebp-ecx*4]

Seems one can add only, no sub: lea eax, [ebp+ecx*4]

Zolaerla

  • Guest
Re: Invalid Effective Address
« Reply #2 on: January 31, 2008, 05:49:30 AM »
The easy way to do what you are trying to do is to use negative values in ecx instead of positive values.

for example, if your sample looks something like:
; Run through four local vars
  mov ecx, 1
.l1:
  lea eax, [ebp - ecx*4]
  ; do something with eax as a pointer
  inc ecx
  cmp ecx, 4
  jb .l1

You will get an error on the lea as you described.

But you can do the same thing using negative values, like this:
; Run through four local vars
  mov ecx, -1
.l1:
  lea eax, [ebp + eax*4]
  ; do something with eax as a pointer
  dec ecx
  cmp ecx, -4
  jg .l1

Alternatively, if you're just trying to optimize your code and just using the lea to calculate "ebp - ecx*4", the same basic thing works if you do
  ; EAX = EBP - ECX * 4
  neg ecx ; Switch ecx's sign
  lea eax, [ebp + ecx*4]
instead of what you are doing

Not totally efficient, but it works

Hope that helps!