Drawing inspiration from cfenollosa, I was writing my boot sector in ASM. Having just learnt how to use includes in ASM, I was writing code to print a given string using a loop.
My boot sector consisted of two files:
This is the main file which instantiates the boot sector and prints the string HELLO.
Now we can compile the code and run it using qemu as shown:
nasm -f bin boot_main.asm -o boot.bin; and qemu-system-x86_64 boot.bin
Which gives the much expected result:
NASM Success Hi
But what if we mess around with the code a little bit? coming from C, I’m used to having my includes at the beginning. So similarly I tried moving the includes to the beginning:
But running this, we are greeted with gibberish:
NASM Failure Hi
Interesting. But why is this happening? Lets look at the hexdump to see if we can find any difference
Hexdump of modified main.asm
Hexdump of original main.asm
Why? The answer is simple. If we include the boot_print.asm file in the starting, NASM will execute that before the other code, this would mean it will print random gibberish onto the screen, as per whatever is the value stored in the location stored in the bx register.
Labels in NASM are executed sequentially even if not called.