3

I haven't seen anyone touch on this as far as i can tell, so i'm using a flat assembler and if i try to do something like this "lea eax,testing" it won't work but if i do this "lea eax,[testing]" it works? Lots of examples i come across i see people being able to do it without the brackets but i don't know if that's possible to do on a flat assembler since i keep getting the error invalid operands, also "testing" is a dd (double word).

Last thing lea eax,[testing] and mov eax,testing is equivalent on my flat assembler. I tested the eax register with ollydbg debugger, in both cases eax equals address 00401006. So in this case they are equivalent correct?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
noob
  • 51
  • 6
  • 1
    Including the name of the assembler you are using is paramount when asking a question about syntax. The "right answer" here varies widely between x86 assemblers. Nasm/Yasm mandate the use of brackets for memory accesses (which includes LEA). – Jonathon Reinhart Feb 08 '15 at 04:06
  • 1
    not to sound too noobish but it's called "flat assembler 1.71.22" i guess that's correct, also syntax is intel – noob Feb 08 '15 at 04:10
  • 1
    As far as I know FASM uses a syntax similar to what I just mentioned. Which means the brackets are always required for memory accesses. If you just want the address of a label in a register, use MOV without brackets. – Jonathon Reinhart Feb 08 '15 at 04:13
  • 1
    Thank you, assembly is confusing i just don't want to overlook anything, it will unfortunately take lots of time for me to wrap my head around it. – noob Feb 08 '15 at 04:29
  • 1
    Note that there are cases where you can't easily replace `lea` with a `mov`, and that's where it's more useful. Such as `lea eax, [esp+8]` to get the address of a local variable, or `lea eax, [eax + 4*ecx + foo]` to get the address of an array item. – Jester Feb 08 '15 at 14:24
  • Thanks for your comment jester but when i use FASM similar to the way you show your examples, after turning "testing" into an array of double words and using lea eax,[testing+4] i would get 0040100b in eax but if i do that with mov eax,[testing+4] i get the second element of the array, eax looks like 00000004 which is the second element. – noob Feb 08 '15 at 16:13
  • So basically if i wanted to i could use mov eax,testing+4 and get 0040100b which is what your saying i can't do correct? – noob Feb 08 '15 at 16:21
  • 1
    You **can** do `testing+4` because that happens to be an immediate constant. You can't use registers, such as `mov eax, esp+8` is invalid, but `lea eax, [esp+8]` is valid. – Jester Feb 08 '15 at 16:53
  • Thanks a lot man, understanding addresses is always been my biggest hurdle but thanks to this community i'm leaping over it. – noob Feb 08 '15 at 17:09
  • Don't worry, assembly isn't confusing... *x86* assembly is confusing. – Jonathon Reinhart Feb 09 '15 at 02:28
  • Possible duplicate of [What's the purpose of the LEA instruction?](https://stackoverflow.com/questions/1658294/whats-the-purpose-of-the-lea-instruction) – phuclv Aug 09 '18 at 01:44

1 Answers1

1

Because LEA is like the "&" in C, it expects that you pass a variable to it, not an address to that variable. In FASM, when you write "[a]", you are reffering to the variable 'a', and when you write just "a", you are reffering to its address. So, "lea eax,[a]" and "mov eax,a" mean exactly the same. I hope this helps.

FlatAssembler
  • 667
  • 7
  • 30