Search code examples
assemblyx86masmmemory-address

What does mov edx, [x+4] do in assembly language?


In assembly language, what does the second line of code do? What happened when this code was executed?

x DWORD 1, 2, 3, 4, 5, 6, 7, 8

mov edx, [x+4]

Solution

  • mov is an instruction which copies data between registers, or to/from a specified memory location.

    TLDR: mov edx, [x+4] loads a dword (4 bytes) starting from the fourth byte of x (which has value = 2), into the edx register.


    In the memory the x array can be seen as:

    0x00000001,
    0x00000002,
    0x00000003,
    0x00000004,
    0x00000005,
    0x00000006,
    0x00000007,
    0x00000008
    

    The address of x corresponds to the first byte of 0x00000001. The fourth byte after x is the first byte of 0x00000002. After this byte 32 bits are loaded into edx. So in the end the value 0x00000002 is inside edx.