Search code examples
stringassemblyx86attendianness

Accessing and moving bytes in X86 assembly


I have several questions about memory and registers in X86 assembly:

  1. I have a string "abcdefgh", and register %eax holds a pointer to the string. Now I use movl (%eax), %edx to grab the first four bytes of the string into %edx. How are they stored in the register? Is the character d in the %dl register, or is it the character a?

  2. When using movb %eax, %dl for example, which of %eax's bytes does it actually move? The one in %al or the opposite one? Is it even possible to do this? Or should I use a pointer like this - movb (%eax), %dh - to take the first byte the pointer points to?


Solution

  • Assuming you're using the unusual GAS' syntax (source is the first operand, destination is the second one) and not Intel's :

    How are they stored in the register? Is the character d in the %dl register, or is it the character a?

    Since you're accessing the string as if it was a 32-bit number, endianness applies. x86 is little-endian so you get the least-significant byte at the lowest address, so DL will hold 'a' (0x61), and the whole EDX would be 0x64636261.

    When using movb %eax, %dl for example, which of %eax's bytes does it actually move? The one in %al or the opposite one? Is it even possible to do this?

    That would give an syntax error because operands are of different size. You can't move 32 bits to 8 bits.

    Or should I use a pointer like this - movb (%eax), %dh - to take the first byte the pointer points to?

    If you want to access the data pointed by EAX and not EAX itself, then yes, that should work.