Question

I am reading the textbook entitled "Introduction to 80x86 assembly language and computer architecture" by Richard C. Detmer and on page 59 it talks about the BYTE directive in assembly language and using that for characters and strings. Here is the text:

"In addition to numeric operands, the BYTE directive allows character operands with a single character or string operands with many characters. Either apostrophes (') or quotation marks (") can be used to designate characters or delimit strings. They must be in pairs; you cannot put an apostrophe on the left and a quotation mark on the right. A string delimited with apostrophes can contain quotation marks, and one delimited with quotation marks can contain apostrophes, making it possible to have strings containing these special characters."

This is very understandable but my curiosity lines is the BYTE directive because I thought it only could hold a single byte, eight bits. I did a test and if I said

string BYTE "HelloWorld"

it would compile of course but if you checked the hex you will notice it is not just a single byte but multiple bytes. Looking at an ascii table we already know that a single character is a byte. So how can we have strings? I tried to apply this with numbers. We know the range for the BYTE directive is from -128 to 255. I tried this code:

number BYTE 9987

when I tried to compile and run, I got an error because of course 9987 was bigger than a byte. But why are strings immune to this? Why can the BYTE directive hold more than a byte in the contexts of strings but not do the same with numbers?

I would imagine when I compiled and ran the first code statement I listed to have the same error as the second one. Why is the "rule" broke here?

-Dan

Was it helpful?

Solution

Note that there is no single assembler syntax, different programs may use different syntax. You should consult your assembler's manual.

That said, the string constant is usually just a shorthand notation for each of the characters separately, and the BYTE directive allows multiple items to be defined at once.

So, for example, BYTE 1, 2, 3, 4 is valid and defines 4 bytes, and your string BYTE "HelloWorld" is equivalent to string BYTE 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' and defines 10 bytes. The reason you don't get an error is because each character on its own fits into a byte.

Some assemblers don't even allow string literals in BYTE or equivalent directives, they have specialized directives for strings, for example .string or .ascii.

OTHER TIPS

The BYTE directive tells the assembler to reserve a block of storage and remember its length in bytes. The length is determined by the number of bytes in the initializer, which could be numbers between 0-255 decimal, or characters in single quotes. Strings are common in programming languages, and are usually considered to be arrays of characters in consecutive bytes in memory. As a convenience to the programmer the assembler allows you to reserve a sequence of bytes initialized to a character string by specifying the string in single or double quotes. These are equivalent

string1 BYTE 'H', 'e', 'l', 'l', 'o'

or

string1 BYTE 'Hello'

Some assemblers have different directives for this, for example the Oracle Solaris x86 Assembly Language has a

  • .byte directive that just takes bytes
  • .ascii directive that takes a string which will not be NUL terminated
  • .string directive that takes a string which will be NUL terminated
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top