63

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?

6
  • 5
    It literally places a byte right there in the executable.
    – user541686
    Jun 30, 2013 at 5:36
  • 3
    @Mehrdad, why didn't you make that an answer?
    – Carl Norum
    Jun 30, 2013 at 5:38
  • Neither. It defines a byte. "Put this byte here."
    – Ken White
    Jun 30, 2013 at 5:39
  • 3
    @CarlNorum: To be honest, for fear of people who like downvoting succinct answers... but here you go.
    – user541686
    Jun 30, 2013 at 5:59
  • It might be better to change "instruction" to "directive" in your question(?).
    – RastaJedi
    Aug 4, 2016 at 3:25

2 Answers 2

86

It literally places that byte right there in the executable.

9
  • 11
    I really have no idea why this exact sentence isn't placed in manuals. Thanks, it makes sense now.
    – r0nk
    Jun 30, 2013 at 6:08
  • 4
    @David天宇Wong: I believe it stands for "define byte", but I'm not sure.
    – user541686
    Nov 19, 2014 at 11:43
  • 5
    What do you mean "it places that byte in the executable" ? Jan 8, 2015 at 9:13
  • 1
    I too would like to know exactly what this sentence means... does this mean a random byte of memory is allocated for whatever it is you need a byte for at runtime?
    – Totem
    Mar 3, 2015 at 1:36
  • 14
    @Totem: You're thinking too hard. Binary code is just a bunch of bytes; when your assembly code is assembled, it just turns into a bunch of bytes. db just puts whatever extra byte you desire right in the middle of all the other bytes.
    – user541686
    Mar 3, 2015 at 1:43
3

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
1
  • 2
    I added an example. Does the ARM assembler you had in mind (ARMASM?) use $ for current position, and use equ that way? This example is valid NASM syntax (except for using @ as the ARM comment character), so it might be a franken-example that doesn't actually work anywhere. Jan 3, 2021 at 13:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.