Oh ok, so when we assemble the code above:
I'm guessing this is what happens when I have the binary at the first sector of a hard disk ofcourse or bootable medium( at least at a basic level ), the BIOS looks for a boot sector, it finds one as it comes across the magic number 0xaa55 on our bin file, which is just raw binary, the CPU then executes this boot sector and it hangs in an infinite loop,
I don't know what you mean by "what are we writing to". "dw" means "data word". In other words, it tells the assembler "at this point in the binary I want these particular arbitrary values". By the way, "db" means "data byte", which is used to zero-fill the majority of the file. |
that makes sense, I'm confusing higher level constructs with assembly, we are actually writing
0xaa55 to the bin file itself with dw, so 0xaa55 will just be placed after 510 zeros right?
__________________________________________________________________________________
Another few things that kind of confuse me, I assembled the file using a nasm assembler and the file is 512 bytes (which it should be) but I fail to see how this file would only be 512 bytes, doesn't the jump instruction take a couple of bytes too?
so the jump instruction should be maybe 2 bytes, and then we write 510 zeroes followed by another 2 bytes 0xaa55 , so in total shouldn't this be 510 + 2 + 2(for jump instruction) = 514?
after I assembled the program with -
nasm boot.asm -f bin -o boot.bin
I checked the size and strangely enough it's 512 bytes, so are the jump instructions not included?
*edit
After spending a couple of hours of researching I think I understand, times 510-($-$$) db 0
so ($-$$) will give us the current position AKA how many bytes we have written which in our case is 2, so we subtract 510 - 2 = 508, so we write 508 bytes followed by 0xaa55
I think that's correct?
and lastly I again created a disk image and made a new vm and set the vdi to this new disk image ( from the bin file created with nasm ) and again it worked ( boot screen stuck in text mode ),
but I examined both bin files, the one I created manually the first time and the one I created second time around with nasm, they both do the same thing but for some reason the first couple of bytes differ.
in the first one the first three bytes are E9 FD FF followed by zeroes until the magic number.
and in the second bin file created from nasm only the first two bytes are set - EB FE followed by zeroes until the magic number,
so why does nasm produce different results and how come it essentially does the same thing?