1
votes

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.

1
You can't, this is a compile time directive, the assembler will report an error. In your code you use the proper sized accesses, so normally the worst that can happen is truncation/overflow. If you somehow mess up data sizes, then you can overwrite other stuff in memory.Jester
@Jester thank you very much for the comment. It seems to me that you are experienced at assembly language programming, do you mind if I ask - what is the learning path you took when studying assembly and in what area do you find assembly mostly useful? Any answer would be greatly appreciated, it doesn't have to long, one or two sentences will be more than enough. Thanks again!Thor
The path I took was studying disassembly generated by optimizing compilers, with the Intel reference manuals in hand. It is a rather tedious path, and takes a while to get going, but it results in very practical knowledge: being able to understand what and why compilers generate. Books are also an excellent way to learn. Michael Abrash's classic Zen of Assembly Language is worth a read. It is an old book that focuses mainly on 16-bit assembly-language programming for the 8088, so you don't read it for the syntax, but for the timeless insights.Cody Gray
We also have lots of resources on x86 assembly language in the x86 tag wiki. Reading all of those will undoubtedly get you ahead! :-) And for things like this, it really can't hurt to just try it. Pick a very large value, larger than 8 bytes, and then try to define it as a BYTE. What happens? I can't believe that you need to be very experienced to be able to think of large numbers, >255.Cody Gray

1 Answers

1
votes

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