I am assembling a program with nasm to fit in the boot sector (512 bytes max).
e.g. nasm -f bin boot.asm -o boot.bin
The last two lines of the program pad the remaining space with 0 and add the magic bytes:
times 510 - ($-$$) db 0 ; Pad the remaining of the first 510 bytes with 0
dw 0xaa55 ; Magic bytes required at end of boot sector
The outputted boot.bin
file is always 512 bytes (expected), so I cannot trivially look at the size of boot.bin
to get the size of the meaningful (non-padding and non-magic bytes) instructions.
I imagine it would work to print ($-$$)
at assembly time (similar to #warning
or #pragma message
for gcc), but I cannot find any way to print at nasm assembly time.
Is there a clean or straightforward way to know the size of the instructions before padding?
It would be nice to avoid hacky methods like printing at runtime or searching backwards through boot.bin
looking for a non-zero value.