In NASM assembly, there are db and dw pseudo instructions to declare data. NASM Manual provides a couple of examples but doesn't say directly what's the difference between them. I've tried the following "hello world" code with both of them, and it turned out that no difference is observable. I suspect the distinct has something to do with internal data format, but I don't know how to inspect that.
section .data
msg db "hello world",10,13,0
msg2 dw "hello world",10,13,0
section .text
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, msg ; or use msg2
mov rdx, 14
syscall
jmp .exit
.exit:
mov rax, 60
mov rdi, 0
syscall
NASM produces WORDs anyhow ;-)
dw 'a' is equivalent to dw 0x61 and stores the WORD 0x0061 (big-endian) as 61 00 (little-endian).
dw 'ab' (little-endian) is equivalent to dw 0x6261 (big-endian) and stores 61 62 (little-endian).
dw 'abc' (one word, one byte) is equivalent to dw 0x6261, 0x63 and stores two WORDS (little-endian): 61 62 63 00.
dw 'abcd' (two words) stores two WORDs: 61 62 63 64.
msg2 dw "hello world",10,13,0 transfers the string into 6 words and the numbers to 3 words and stores it: 68 65 6C 6C 6F 20 77 6F 72 6C 64 00 0A 00 0D 00. In your example, msg won't be printed until its end.
The NASM manual sections 3.2.1 DB and Friends: Declaring Initialized Data and 3.4.2 Character Strings indicate that there is a difference when the individual strings are shorter than the element size. Each element is padded with zero bytes to its native size.
To ensure that you do not have unintended characters in the data, always use DB for 8-bit strings. DW may or may not work for UTF-16 depending on the machine byte order and any assumptions in the code.
Using DW pseudo instructions will definitely result in unexpected values for the numeric values as these will be interpreted as 16-bit words introducing unexpected null characters into the string.
Use 2.1.3 The -l Option: Generating a Listing File to see the actual memory image being output to see the content you are generating.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With