Author Topic: Label address arithmetic  (Read 3657 times)

nobody

  • Guest
Label address arithmetic
« on: April 15, 2009, 02:13:28 AM »
Hello. I'm facing a phenomenon looks strange.
In the following code, I want to let esi be the address of start:

1: start:
2:    call here
3: here:
4:    pop  eax
5:    push esi
6:    lea  esi, [eax - here]

Line 6 causes the error 'beroset-p-637-invalid effective address', but the following instructions are successfully assembled.
    lea  esi, [eax + here]
    lea  esi, [eax - 5]
    lea  esi, [eax - 5]

What is the difference between them?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2446
  • Country: us
Re: Label address arithmetic
« Reply #1 on: April 15, 2009, 11:29:20 AM »
Good question! A label is a "relocatable address". Nasm generally won't do calculations involving a label, unless it's the difference between labels - which is what you've got, but Nasm apparently doesn't recognize it as such. lea esi, [eax - (here - $$)] will assemble, and is the same as what you've got. BUT... that's only going to tell you the length of the call instruction, not the address you want. lea esi, [eax - (start - here)] will do what you want, I think.

Best,
Frank

nobody

  • Guest
Re: Label address arithmetic
« Reply #2 on: April 15, 2009, 11:50:17 AM »
Thank you! I've got clear about it.
Finally, I modifyed my code like this. It works as I want.

6: lea esi, [eax - (here - start)]