what does the assembly instruction 'db' actually do?

I'm a bit confused as to what the asm instruction db actually does. I've read that it defines a byte, but what does that actually mean? Does it move the value into a certain place in memory, or does it predefine a value before the code is assembled?


Solution 1:

It literally places that byte right there in the executable.

Solution 2:

From ARM manual:

〚label:〛 DB expression 〚, expression ...〛

The DB statement initializes memory with one or more byte values. label is a symbol that is assigned the current memory address. expression is a byte value that is stored in memory. Each expression may be a symbol, a string, or an expression.

so, this statement do following:

  • set current memory address value for the "label" variable
  • set "byte" values from expressions to current and next memory addresses ("expressions" can be one byte or many bytes - a long string)

After the operation, if we compare the new "current" memory address vs the memory address before the operation (hold by label), we can calculate the length of the "expression" list (because each character is one byte and that is same as increase 1 in memory address).

Getting the assembler to calculate the distance between here ($ in some assemblers) and a label works around any instruction or pseudo-instruction, not just db, and looks like this in some assembly languages. Exact directive syntax varies by assembler (ARMASM, NASM, MASM, FASM, etc. all use db to emit literal bytes into the output, but differ on the details of equ or something else.)

msg: db "Hello, World!", 0x10, 0
msglen equ $ - msg                @ includes the 0 terminator