what does the assembly instruction 'db' actually do?

Assembly

Assembly Problem Overview


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?

Assembly Solutions


Solution 1 - Assembly

It literally places that byte right there in the executable.

Solution 2 - Assembly

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionr0nkView Question on Stackoverflow
Solution 1 - Assemblyuser541686View Answer on Stackoverflow
Solution 2 - AssemblyK-LaboratoryView Answer on Stackoverflow