Search code examples
assemblyx86masmirvine32

What happens if I initialize a BYTE or SBYTE with a value that is too large to fit?


I'm currently studying x86 assembly language by following Kip Irvine's assembly language book.

In the book, the author stated:

3.4.4 Defining BYTE and SBYTE Data

The BYTE (define byte) and SBYTE (define signed byte) directives allocate storage for one or more unsigned or signed values. Each initializer must fit into 8 bits of storage.

I was just wondering, what if I accidentally assigned a value that is too large for the storage area? What kind of behaviour should I expect?

Due to my inexperience, I couldn't come up with an example that demonstrate the behaviour, so it would be great if anyone could provide an explanation with code sample.


Solution

  • So let's say you have a label MyMemoryLocation, among other labels, and you've written it like this:

    .DATA 
    Before           BYTE 0
    MyMemoryLocation BYTE 0
    After            BYTE 0
    

    And you've got code that abuses the label and tries to use it in a 16-bit operation:

    .CODE
    MOV AX, 1234H
    MOV MyMemoryLocation, AX
    

    If you don't get an assembler error (MASM will give you "Operand size mismatch"), the value in AX will be written to the address starting at MyMemoryLocation.

    Since 80x86 is little-endian, the least significant byte will be written first, at MyMemoryLocation. The second byte will be written to the memory immediately afterward, at After. So you'd end up with:

    Before            BYTE 0
    MyMemoryLocation  BYTE 34H
    After             BYTE 12H