the meaning of times in nasm

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
lokii

the meaning of times in nasm

Post by lokii »

I always see the code
times 512-($-$$)-2 db 0
dw 0xaa55
what's the '$','$$' and 2 mean ?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:the meaning of times in nasm

Post by Pype.Clicker »

from the NASM manual (info nasm, section 3.5)
NASM supports two special tokens in expressions, allowing
calculations to involve the current assembly position: the `$' and `$$'
tokens. `$' evaluates to the assembly position at the beginning of the
line containing the expression; so you can code an infinite loop using
`JMP $'. `$$' evaluates to the beginning of the current section; so you
can tell how far into the section you are by using `($-$$)'.
the reason to use ($-$$) rather than $ is that you usually have an 'ORG' directive that makes your section start at a non-zero address.

The code you show is a padding code that will ensure the last 2 bytes of the boot sector will be '55' and 'AA', and that all other bytes between the end of the code and the signature are zero.
lokii

Re:the meaning of times in nasm

Post by lokii »

Thanks Pype.Clicker . :)
Post Reply