Search code examples
assemblyx86-16emu8086

Why does this sequence of MOV and ADD with memory produce AX=90FFh?


I actually have a problem with MOV AX,[SI]. I don't understand why the final result is AH=90h AL=FFh.

ORG 100h
MOV [110h],80h
MOV SI,110h
ADD [SI], 7Fh
MOV AX, [SI]

Solution

  • Instructions like mov [110h], 80h and add [si], 7Fh are ambiguous because the assembler does not know what size you desire them to have. Do you want to process bytes (1 byte) or do you want to deal with words (2 bytes)?

    Apparently emu8086 chooses the byte size by default!

    The byte at address 110h was filled with 80h, later raised by 7Fh which produced FFh, but then you started reading a whole word at address 110h. You received the correct sum in AL and AH got whatever garbage was in the memory at address 111h.

    Better always specify the size you need:

    • words

      ORG     100h
      mov     word ptr [0110h], 0080h
      mov     si, 0110h
      add     word ptr [si], 007Fh
      mov     ax, [si]        >>>> AX=01FFh  AH=01h AL=FFh
      
    • bytes

      ORG     100h
      mov     byte ptr [0110h], 80h
      mov     si, 0110h
      add     byte ptr [si], 7Fh
      mov     ax, [si]        >>>> AX=??FFh  AH=??h AL=FFh
      

      Maybe using mov al, [si] would be more appropriate in this case.


    Pay attention to the program length.

    Depending on the version this little program has 13 or 14 bytes. This means it doesn't run into the memory at address 110h. If it were then the instructions would corrupt the program and maybe crash the computer.
    You could store the numbers elsewhere to be safe, e.g. address 1000h, leaving more room to experiment with the instruction set...