5
votes

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.

4

4 Answers

11
votes

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]
9
votes

It loads EAX with the DWORD value that EAX was originally pointing to.

In C terms its dereferencing the value that was originally held in EAX as follows: "eax = *eax"

4
votes

dword ptr [eax] - points to memory, which address is eax, so this statement copies 32 bit value from memory to eax

1
votes

About the why this would happen, it depends on the context.

For example, this could be used to load the value of [eax] on the stack, to use it as an argument for a function call. This operation cannot be done at once (as in mov dword [esp + 4], dword [eax], cannot have two memory references in the same instruction), so it is split in two instructions:

mov eax, dword [eax]
mov dword [esp + 4], eax