Skip to content

Outputting data blocks β€” #d

Lorenzi edited this page Jan 16, 2022 · 3 revisions

Sized Data Directive

This directive copies a sequence of values verbatim to the output. In the sized version, its name controls the size of each element in the sequence. Elements with a smaller size will be sign-extended. Elements with a larger size are prohibited, and you should slice them first if needed.

For example:

#d4  0x1, 0x2, 0x3, 0x4
#d8  0x12, 0x34, 0x56, 0x78
#d16 0x1234, 0x5678
#d32 0x1234, 0x5678

...would be assembled into the following bytes:

12 34
12 34 56 78
12 34 56 78
00 00 12 34 00 00 56 78

Note that the #d32 directive's arguments, 0x1234, 0x5678, were sign-extended to match the specified size.

Unsized Data Directive

You can also use a plain #d directive, and it will rely on the intrinsic size of each element, which can vary between elements.

For example:

#d 0x12, 0x345678
#d 0xabcd`8, 0xef`32

...would be assembled into the following bytes:

12 34 56 78
cd 00 00 00 ef

Strings

Strings are treated as sized values like number literals, so they can also be output with data directives. The unsized #d version is especially useful here.

Rust-like escape sequences and Unicode characters are available. The string is encoded as UTF-8 by default, but you can use the string encoding functions described here to change the encoding.

For example:

#d "abcd"
#d "\n\r\0"
#d "\x12\x34"
#d "木"

...would be assembled into the following bytes:

61 62 63 64
0a 0d 00
12 34
e6 9c a8

Note that you can also mix strings and number literals:

#d "hello!", 0x00
#d "score: ", 0x35, 0x31, 0x32, " coins: ", 0x39, 0x37

If the string's length is needed, we can use a bit of arithmetic to derive it:

helloworld:
    #d "Hello, world!\0"

helloworldLen = $ - helloworld