0

Although I have more "advanced" questions on this site about Assembler, I still can't understand the variables, everywhere is the same information, where this table is included in all:

Directive Purpose Storage Space
DB Define Byte allocates 1 byte
DW Define Word allocates 2 bytes
DD Define Doubleword allocates 4 bytes
DQ Define Quadword allocates 8 bytes
DT Define Ten Bytes allocates 10 bytes

What is the difference between doing this:

message: db "Hello"

or this:

message: dw "Hello"

What does 1 byte assign to or 2 byte in the case of dw? Layers is something very obvious but I am having a harder time understanding this than opening a socket. What I "understood" at the beginning is that for example, if you created a variable with the db directive you could only store 1 byte, but it doesn't make sense when in the tutorials I see that they store a "Hello world". I know it must be stupid, but if someone has the patience to explain me without scolding me, I would appreciate it. I can't keep doing things without knowing something basic like creating a variable.

  • 1
    You can pass multiple items to the directives, and string constants are a special case to make it easier for you so that you don't need to type out the characters separately. See the [nasm manual](https://www.nasm.us/xdoc/2.11.08/html/nasmdoc3.html#section-3.4.4). The particular directive selects the packing, so your `dw "Hello"` will be 3 words, 6 bytes while it would be 5 bytes with `db`. – Jester Apr 14 '21 at 18:32
  • @Jester I understood, if it were dd it would be in 2 words and it would be 8 bytes, right? Assuming that this is correct, in which area should I put one or the other? If the word contains 4 characters, it would be 4 bytes with db and 4 with dd – Franco Milich Apr 14 '21 at 18:51
  • 1
    what did you see when you disassembled/analyzed the output of nasm for the db vs dw lines? – old_timer Apr 14 '21 at 19:05
  • assembly langauge in general does not have variables, these dw/db things are just directives to tell the assembler to reserve some space, possibly with an alignment, and such that you can ask it to initialize that space with specific values. They are not variables. you have labels, data and instructions and instructions are a subset of data. and the only thing that distinguishes data from instructions is instructions are data that the processor executes when it follows the execution order of the data it is fed. – old_timer Apr 14 '21 at 19:07
  • `dd` allocates dword-wise. A dword (doubleword) is 2 words, or 4 bytes. Not 8 bytes. Anyway, I've never had a use for defining strings spanning multiple items with `dw` or larger directives. (Single-item strings, like a `dw` with a two-byte string, do occur sometimes, [here's an example](https://hg.ulukai.org/ecm/ldebug/file/69e00a339763/source/msg.asm#l1681).) – ecm Apr 14 '21 at 19:08
  • @ecm: Indeed, there's not much use-case for this, but there is a canonical answer with examples showing how NASM pads the last partial word / dword / qword to make it a whole number of the element size: [How are dw and dd different from db directives for strings?](https://stackoverflow.com/q/38860174) – Peter Cordes Apr 15 '21 at 00:39
  • Thank you very much everyone, I love this site / community, that people take the time to teach others is priceless, @peter you are my idol already, I see you in every post about Assembler, not all heroes wear a cape! – Franco Milich Apr 15 '21 at 04:14
  • That reminds me, I've been looking for a good cape :P – Peter Cordes Apr 15 '21 at 04:21

0 Answers0