Search code examples
assemblycompiler-constructionnasmdirective

Are there any other ways to write these "data" sections in Assembly without the magic helpers?


In NASM you see stuff like this:

msg: db "Hello world!", 0 ; we need to explicitely put the zero byte here

times 510-($-$$) db 0           ; fill the output file with zeroes until 510 bytes are full
dw 0xaa55                       ; magic number that tells the BIOS this is bootable

From my understanding, these are compiler-specific "helpers" (NASM being the compiler here).

It looks like the as compiler/program has their own helpers like .asciz for a similar thing:

msg: .asciz "Hello world!"

.fill 510-(.-init), 1, 0 # add zeroes to make it 510 bytes long
.word 0xaa55 # magic bytes that tell BIOS that this is bootable

Is there any "lower-level" way to write this in a compiler-agnostic way sort of thing? Somehow perhaps writing them with instructions rather than these helpers? Basically, what are these abstracting? How do I write them using lower-level abstractions?

Pretty much, I would like to write it in a more lower level way to not be bound into the specific compiler, and also know what is actually happening at a deeper level.

Same with stuff like this:

[bits 16]    ; use 16 bits
[org 0x7c00] ; sets the start address

Where do I find docs or something on how I can write this at a lower level?


Solution

  • I'm not sure what you think a compiler is, but the examples you gave are of various directives (helpers, as you call them) of two different assemblers. Assemblers are distinguished from compilers because they produce machine code from source that more or less directly represents what that machine code should be. Compilers produce machine code from a language that doesn't try to represent the machine code that the compiler produces. Also, traditionally many compilers have worked by creating assembly code and assembling that to produce machine code, rather than by producing machine code directly.

    There's no common subset of assembly language shared between all assemblers, so there's no assembler-agnostic language, lower-level or otherwise, that would let you do whatever it is you want.

    However, most assembly languages can be thought of as a language that when assembled produces an output consisting of a sequence of arbitrary bytes, just specialized towards producing machine code. This means that there is, in a way, a lowest-level almost-entirely-agnostic non-language you could use instead, and that's a binary file, which is just a sequence of arbitrary bytes. Unfortunately, there's no way you, as a human, can physically input a binary file directly into a computer without going through some other representation. The most direct but still practical way would be to use a hex editor. A more direct, but much less practical method would be to use cat > file.bin on Unix or copy con file.bin on Windows.