Times 510 -( $ - $$ ) db 0 means?

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 can see the whole pdf here:

1 Like

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!!

3 Likes

Good Job @Mr.Tesla. You have put so much effort to explain the concept. Brief and well explained.

Thanks for the responds @Mr.Tesla. Where are all these action going to take place ? RAM or Drive ?

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 $$

1 Like

Thank you so much. you explained it very well.

Hello Prototype,

It seems like few of the chapters at end are missing in the pdf did you manage to get them.

Hi, Welcome to the community!!

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!!

Unfortunately we don’t have full version of the same book. I hope the below link will help you to get started.

  1. JamesM's kernel development tutorials
  2. Department of Computer Science and Technology – Raspberry Pi: Baking Pi – Operating Systems Development
  3. GitHub - cfenollosa/os-tutorial: How to create an OS from scratch

Let me see if I can find any books for you. If I get any, I will share it here…!

Thank you very much!!

i HAVE a problem.
When the rest of the space is filled with 0, isn’t the MBR partition destroyed?

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.