Search code examples
assemblyx86-16tasm

Result of MOV CX, DAT[4] indexing into array of words (DW)


I have the following commands

DAT DW 128,256,64,32
MOV CX, DAT[4]

As I know, DAT[4] means we will get value of 4th index, but in this case we don't have such an index.
Could you please tell me what I will have in the CX register after MOV CX, DAT[4] ?


Solution

  • As I know, DAT[4] means we will get value of 4th index,

    In assembly language we don't use array indexing based on whole elements like in the high level languages. In a high level language DAT[4] would retrieve the fifth array element (if indexing started at 0).

    In assembly language the number between the square brackets is an offset from the start of the array and measured in bytes. Your example:

    DAT DW 128,256,64,32
    

    rewritten using hex:

    DAT DW 0080h, 0100h, 0040h, 0020h
    

    presents itself in memory like (x86 being little endian):

    80h, 00h, 00h, 01h, 40h, 00h, 20h, 00h
    ^                   ^
    offset 0            offset 4
    

    The word at offset 4 contains a low byte of 40h and a high byte of 00h.
    Therefore MOV CX, DAT[4] will load CX with 0040h which is 64 in decimal.