I always see the code
times 512-($-$$)-2 db 0
dw 0xaa55
what's the '$','$$' and 2 mean ?
the meaning of times in nasm
- Pype.Clicker
- 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
from the NASM manual (info nasm, section 3.5)
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.
the reason to use ($-$$) rather than $ is that you usually have an 'ORG' directive that makes your section start at a non-zero address.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 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.