Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

x86

assembly

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.

like image 457
user2893243 Avatar asked Sep 04 '14 05:09

user2893243


People also ask

What does MOV EAX EAX mean?

mov eax, ebx – 2 operands, source and destination. mov eax, 5 – one operand is a literal. mov y, eax – memory to register movement. add eax, 5 – 2 operands for add. mul value – 1 operand for mul, other operand is eax.

What does mov eax do?

"eax" is the destination of the move, also known as the "destination operand". It's a register, register number 0, and it happens to be 32 bits wide, so this is a 32-bit move. 5 is the source of the moved data, also known as the "source operand".

What is dword ptr in assembly?

Basically, it means "the size of the target operand is 32 bits", so this will bitwise-AND the 32-bit value at the address computed by taking the contents of the ebp register and subtracting four with 0.

How big is a dword ptr?

The size directives BYTE PTR, WORD PTR, and DWORD PTR serve this purpose, indicating sizes of 1, 2, and 4 bytes respectively.


2 Answers

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"

like image 121
stiabhan Avatar answered Oct 12 '22 18:10

stiabhan


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]
like image 25
paxdiablo Avatar answered Oct 12 '22 20:10

paxdiablo