x86 ASM: DD Being Used as an "Instruction"?
Asked Answered
C

1

7

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?

Cymoid answered 1/10, 2017 at 14:33 Comment(3)
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:gnu.org/software/grub/manual/multiboot/…Lodestone
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)Lodestone
It's what is often called a "compile-time calculation." The assembler is evaluating the expression in brackets and storing it there before your program is converted to machine code. In many assemblers you can type something like MOV EAX,4+3 and it will get automatically changed to MOV EAX,7 without you actually having to do so. This is handy for communicating the intent behind numeric constants (for example, in 68000 assembly where loops terminate at $ffff instead of zero, it's common for the programmer to write MOVE.W #32-1,D7 for a loop that is intended to execute 32 times.)Catamite
C
4

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)

Cortese answered 1/10, 2017 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.