Search code examples
assemblyx86-64att

what is the difference between these movw to AX and movb to AL load instructions, from an odd offset in .short array?


.global main

.data
arr: .short 1, 0xEA, 0x2, 0x3, 0b1010

.text
main:

      lea (arr), %rbx
      movb 3(%rbx),%al #1

      movw arr+3, %ax #2

when I try this code al gets the value 0x0 however ax gets the value 0x200 can you tell me why is that? what is the difference between #1 and #2 ?


Solution

  • Since x86 is a little-endian architecture, the data in arr looks like:

    arr+0   +1   +2   +3   +4   +5   +6   +7   +8   +9
     0x01 0x00 0xEA 0x00 0x02 0x00 0x03 0x00 0x0A 0x00
    

    #1 reads data to al. al is a one-byte register, so 0x00 at the +3 is loaded and the value becomes 0x0.

    #2 reads data to ax. ax is a two-byte register, so 0x00 0x02 is loaded and the value becomes 0x200.

    The difference is the size of the destination registers.