Later, I was reading a pdf titled Writing a Simple Operating System from Scratch by Nick Blundell. The book basically describes how to write your own os/kernel from scratch. In that pdf, I encountered the below-mentioned line of assembly code which I am struggling to understand.
times 510 -( $ - $$ ) db 0
I am not familiar with assembly language. But so far I know that $ mean address of currently executed instruction and $$ means the first address of the section. Nothing else!! Any thoughts ??
You are correct. $ means current address and $$ means the first address of the current section. You have to understand that the times directive only operates on numbers and the difference of address ( $-$$ ) yields a number (Offset). So $-$$ gives you the offset from start to address of the currently executed instruction. If subtract that value from 510 you will get the offset from the address of the currently executed instruction to the 510th byte. So we now know how many bytes are there from the address of the currently executed instruction to the 510th byte. The times directive will now pad that number of bytes up to 510th byte with zeros.
For Ex:
Assume your codes current address $ is at 0x000f and the first address of section $$ is 0x0000. Then $-$$ yield to 15, which implies your current instruction is at the 15th byte. Now 510 - 15 will give you 495 i.e 495 bytes from current instruction will be padded with zeros by times directive.
if you dissect the binary for times 510 -( 0x000f - 0x0000 ) db 0 alone, it will exactly have 495 zeros!!
If you read the pdf very thoroughly you will understand that all operation of TIMES takes place in the bootable drive.TIMES is an assembler specific commands, and as such aren’t part of x86 assembly language. Same goes for $ and $$
I tried to find the full version of the same pdf online but all the pdf I found is the same one. The link for the above pdf file points to the University of Birmingham official website. So, whatever in that pdf is the full version. Its seems like the author had abruptly finished his book.
Thank you very much for welcoming. This indeed was a good starter. Hope we had a fuller version. If in case you have any other such material please. Much appreciated!!
If you are developing a new OS, then do it on any virtual machine. You can’t run this instruction on a live PC with an operating system already installed. So the MBR is safe. In virtual machine, the control is in your hands. You may create your OS according to your design.