assemblyx86

What does mov eax, dword ptr [eax] do?


I understand that dword ptr is a size directive that indicates the size of what is being moved where and I know that mov eax, eax is a form of nop code but what does this do?

I think it swaps the address of eax with the hex value inside but I am not too sure or even know why this would happen.


Solution

  • The instruction mov eax, eax may be a no-operation code but that is not what you have here. You're loading from memory, as indicated by the [] "contents-of" characters.

    It loads eax with the contents of memory (a 32-bit dword in this case) that is currently pointed to by eax.

    Perhaps a graphical picture would help:

    Before:
        eax:                 0x12345678
        memory @ 0x12345678: 0xffffffff
    
    After:
        eax:                 0xffffffff
        memory @ 0x12345678: 0xffffffff
    

    As to possible uses, there are no doubt many. One that pops into mind immediately is a linked list structure where you have something like this for a single element in the list (pseudo-assembly):

    next:      word     ?         ; one word.
    payload:   byte     ?(32)     ; 32 bytes.
    

    If eax is used as a pointer to one of those elements, getting the next element would be done with the instruction you see:

    mov eax, dword ptr [eax]