4

In the following x86 assembly code:

dd 0x1BADB002
dd 0x00
dd - (0x1BADB002+0x00)

The values don't seem to be assigned to any variables. So what does this snippet of code do? I've heard something about it being stored in memory, but where exactly?

Michael Petch
  • 43,801
  • 8
  • 98
  • 174
  • 2
    This is a mulitboot header. If you are writing code to be launched through something like GRUB this signature needs to be found in the first 8k of the ELF file it appears in for GRUB (or any mulitboot compliant loader) to identify the code as mulitboot compliant. Without it a multiboot loader will error out. You cand find documentation about this in the GRUB documentation:https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#OS-image-format – Michael Petch Oct 01 '17 at 15:24
  • DD isn't being used as an instruction. It is a NASM directive that allows you to define a 32-bit value. This data making up the multiboot header is usually placed in a data section that a linker script places before everything else in the generated ELF executable (this si done to get the header in the first 8k of the file so the multibootloader/GRUB can find it) – Michael Petch Oct 01 '17 at 18:17

1 Answers1

3

dd is a "pseudo-instruction" that assembles 4-byte constants into the output, the same way that add eax,eax assembles 0x01 0xc0 into the output.

The NASM manual section 3.2 Pseudo-Instructions describes db/dw/dd and so on.

In this case, as @MichaelPetch points out, those specific constants are used to assemble a multiboot header into the output file. https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#OS-image-format

How does this assembly bootloader code work?


Related:

How are dw and dd different from db directives for strings?

What is the use of .byte assembler directive in gnu assembly?

x86 assembly - Which variable size to use (db, dw, dd)

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731