in Search
     
Latest post 03-08-2007 7:35 PM by modchip. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 03-07-2007 12:29 AM

    • modchip
    • Top 10 Contributor
    • Joined on 02-09-2006
    • Ivalice
    • Posts 710
    • Points 10,110

    LEA & MOV Stuff

    Hi all!

    I was just wondering, is there a big difference between LEA and MOV?

    Also, I did some test;

        lea eax, buffer
        invoke SetDlgItemText, hWin, IDC_EDIT_OUTPUT, eax

    and

        mov eax, offset buffer
        invoke SetDlgItemText, hWin, IDC_EDIT_OUTPUT, eax

    Same results. What's the difference?

    Thanks in advance peeps!

    Chao!

    Filed under:
    • Post Points: 35
  • 03-07-2007 12:59 AM In reply to

    • cruizer
    • Top 10 Contributor
    • Joined on 12-14-2005
    • Singapore
    • Posts 944
    • Points 22,590

    Re: LEA & MOV Stuff

    the "OFFSET" (in the MOV) is not really a part of assembly language. it will just be replaced by the actual address of "buffer" upon assembly/compilation by the assembler. so parang macro sya since may substitution, but I'm sure it's not called a macro

    on the other hand, LEA is a real assembly language instruction. I think the benefit to using LEA is that you can do sort of pointer/index arithmetic on it, like LEA eax, buffer+SI...if I remember it right Stick out tongue

    but certainly, the MOV ... OFFSET in my opinion is better. ganyan talaga pag complex instruction set (CISC), maraming redundancy sa mga instructions. yan ang isa sa mga nagpapabagal ng CPUs natin ngayon, yun legacy baggage Stick out tongue
    http://devpinoy.org/blogs/cruizer
    Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
    • Post Points: 35
  • 03-07-2007 5:00 PM In reply to

    • modchip
    • Top 10 Contributor
    • Joined on 02-09-2006
    • Ivalice
    • Posts 710
    • Points 10,110

    Re: LEA & MOV Stuff

    Aha! The gave me some "enlightenment"! Thanks.

    Time for more tests! Thanks again!

    Filed under:
    • Post Points: 5
  • 03-08-2007 1:14 AM In reply to

    • cvega
    • Top 10 Contributor
    • Joined on 11-25-2005
    • Posts 497
    • Points 8,455

    Re: LEA & MOV Stuff

    cruizer:
    the "OFFSET" (in the MOV) is not really a part of assembly language. it will just be replaced by the actual address of "buffer" upon assembly/compilation by the assembler. so parang macro sya since may substitution, but I'm sure it's not called a macro

    It is Smile

    mov eax, offset variable

    is the same as:

    lea eax, variable

    The only difference is that OFFSET is resolved during assemble, while LEA is during run-time, hence MOV ..., OFFSET ... can't resolve forward references.

    .code

         mov eax, offset forward_var  ; looks valid, but will cause assemble error
         ret 0

    forward_var dd 0


    cruizer:
    on the other hand, LEA is a real assembly language instruction. I think the benefit to using LEA is that you can do sort of pointer/index arithmetic on it, like LEA eax, buffer+SI...if I remember it right Stick out tongue


    Yes, because LEA resolve effective address during the runtime.
    The ADDR special MASM keyword is actually a LEA.

    cruizer:
    but certainly, the MOV ... OFFSET in my opinion is better. ganyan talaga pag complex instruction set (CISC), maraming redundancy sa mga instructions. yan ang isa sa mga nagpapabagal ng CPUs natin ngayon, yun legacy baggage Stick out tongue


    MOV...OFFSET is not a legacy baggage, and it's commonly used most of the time. Whichever you prefer, both of these are only using 1 clock cycle Stick out tongue


    Cheers,

    -chris
    Chris Vega This posting is provided "AS IS" with no warranties, and confers no rights My Weblog|Visit MSDN Community
    • Post Points: 5
  • 03-08-2007 1:25 AM In reply to

    • cvega
    • Top 10 Contributor
    • Joined on 11-25-2005
    • Posts 497
    • Points 8,455

    Re: LEA & MOV Stuff

    Answer
    modchip:
    Hi all!
    I was just wondering, is there a big difference between LEA and MOV?


    Depending on what you want to use the command for, but most safe answer is, these two commands are equivalent in both clock cycle and functionality. It's only for preference, actually.

    However, sometimes you may find OFFSET handy in some places, like calculating the OFFSET of a member in a stuct:

    my_struc struct
       memberA dd 0
       memberB dd 0
    my_struc ends

    my_var my_struc <0, 0>

    .code

       push ebx
       mov ebx, offset my_var
       assume ebx: my_struct

       ; to reference memberA
       mov eax, [ebx].memberA



    Cheers,

    -chris
    Chris Vega This posting is provided "AS IS" with no warranties, and confers no rights My Weblog|Visit MSDN Community
    • Post Points: 20
  • 03-08-2007 5:30 PM In reply to

    • modchip
    • Top 10 Contributor
    • Joined on 02-09-2006
    • Ivalice
    • Posts 710
    • Points 10,110

    Re: LEA & MOV Stuff

    Thanks, now it's more clear.

    Funny, cause you actually read my mind, I was hoping to ask about that struct stuff too that I see in other major asm sites.
    my_struc struct
       memberA dd 0
       memberB dd 0
    my_struc ends

    my_var my_struc <0, 0>

    Is this like sort of an array? What are some practical uses for this?

    Thanks again!

    Regards,
    m0ds

    Filed under:
    • Post Points: 35
  • 03-08-2007 5:43 PM In reply to

    • cruizer
    • Top 10 Contributor
    • Joined on 12-14-2005
    • Singapore
    • Posts 944
    • Points 22,590

    Re: LEA & MOV Stuff

    syntactic sugar lang yan, to allow you to treat structured blocks of memory as a whole (rather than referring to it using the first item in the structure/memory block)
    http://devpinoy.org/blogs/cruizer
    Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
    • Post Points: 20
  • 03-08-2007 5:54 PM In reply to

    • modchip
    • Top 10 Contributor
    • Joined on 02-09-2006
    • Ivalice
    • Posts 710
    • Points 10,110

    Re: LEA & MOV Stuff

    Hmmmm... can you please elaborate on that, or maybe a simple example could do the trick. It's not really that clear to me. :D

    Filed under:
    • Post Points: 5
  • 03-08-2007 6:43 PM In reply to

    • cvega
    • Top 10 Contributor
    • Joined on 11-25-2005
    • Posts 497
    • Points 8,455

    Re: LEA &amp; MOV Stuff

    Answer
    modchip:
    Is this like sort of an array? What are some practical uses for this?

    Nope, structures are usually used to "give meaning" to block of memory. And to elliminate to problem of little-endian, big-endian notation. Meaning, it's all about "looking up" to the memory.

    Let's say we point EBX to some memory:

    mov ebx, offset some_memory_address

    Initially, you don't know what's in the memory, because that's only a set of binary data! So, you'll need structure to give meaning to it:

    assume ebx: my_structure


    That gives assembler an idea that EBX is working as my_structure block, rather than just a pointer. Hence, when you get a member from the structure, the assembler automatically can calculate the OFFSET:

    mov eax, [ebx].LastMember

    Is similar to:

    lea eax, some_memory_address + my_structure.LastMember

    However, since LEA is resolved at runtime, it's usually larger than MOV..OFFSET.

    Hope this helps,

    -chris
    Chris Vega This posting is provided "AS IS" with no warranties, and confers no rights My Weblog|Visit MSDN Community
    • Post Points: 20
  • 03-08-2007 7:35 PM In reply to

    • modchip
    • Top 10 Contributor
    • Joined on 02-09-2006
    • Ivalice
    • Posts 710
    • Points 10,110

    Re: LEA & MOV Stuff

    Thanks Chris & cruizer, your answers really help. :D

    Now it's time to apply my newly received knowledge! Wink

    Thanks again dude!

    Filed under:
    • Post Points: 5
Page 1 of 1 (10 items)