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


#1

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:


#2

Hi, sorry for the late response.

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 address of the currently executed instruction to 510th byte. So we now know how many bytes are there from address of the currently executed instruction to 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 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

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


#4

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


#5

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


#6

Thank you so much. you explained it very well.