Let's say I want to define a initialized variable string before running my assembly program (in section .data
). The variable I chose to create is called Digits
and it is a string that contains all the hexadecimal symbols.
Digits: db "0123456789ABCDEF"
I defined the variable with db
, that means define byte. Does this mean that the Digits
variable is of 8-bits long? This doesn't seem to have sense for me because:
Each character in the string is an ASCII character, therefore I will need 2 bytes for each character. In total, I would need 32 bytes for the whole string!
So what does it mean when I define the variable as byte? Word? Double word? I don't see the difference. Because of my misunderstanding, it seems to be redundant to tell the type of data you need for the string.
PD: This question didn't help me to understand.
One of the answers on the linked question has a quote from the NASM manual's examples which does answer your question. As requested, I'll expand on it for all three cases (and correct the lower-case vs. upper-case ASCII encoding error!):
So the difference is that it pads out to a multiple of the element size with zeros when you use
dd
ordw
instead ofdb
.According to @Jose's comment, some assemblers may use a different byte order for
dd
ordw
string constants. In NASM syntax, the string is always stored in memory in the same order it appears in the quoted constant.You can assemble this with NASM (e.g. into the default flat binary output) and use
hexdump -C
or something to confirm the byte ordering and amount of padding.Note that this padding to the element size applies to each comma-separated element. So the seemingly-innocent
dd '%lf', 10, 0
actually assembles like this:Note the
0
before the newline; if you pass a pointer to this toprintf
, the C string is just"%lf"
, terminated by the first0
byte.(
write
system call orfwrite
function with an explicit length would print the whole thing, including the0
bytes, because those functions work on binary data, not C implicit-length strings.)For each character in the string "0123456789ABCDEF" you need just one byte. So, the string will occupy 16 bytes in the memory.
In case of this declaration:
vark db 1
you can make this:
mov [vark],128
and cannot:
mov [vark],1024
but in this case:
vark dw 1
you can.