In a bootloader, the second last line is :
TIMES 510-($-$$) db 0
Now, will this command also do the same :
db 510-($-$$) DUP (0)
If not why?
I know what TIMES
does, but its not mentioned in my x86 book by Mazidi (Pearson Publication). Any idea why?
And what is the meaning of the $
sign exactly? Different sites have different information about $
.
And is the line TIMES 510-($-$$) db 0
absolutely necessary even if my bootloader code is of 512 bytes in size?
So can anyone help me with these questions?
TIMES
and DUP
are assembler specific commands, and as such aren’t part of x86 assembly language. Same goes for $
and $$
.
The $
symbol denotes the current address of the statement, and the $$
denotes the address of the beginning of current section. So the lines with DUP
and TIMES
calculate the current address in the section and subtracts it from 510. This effectively just zeroes out the section from the beginning to the current address.
2
DUP
is a specific operand specifier to the DB
/DW
/etc psuedo-instructions, telling them to repeat a specific value. It can only be used in these data instructions.
TIMES
is a generic instruction prefix, telling the assembler to produce multiple copies of the instruction (or psuedo-instruction), whatever it may be.
AFAIK, TIMES
is specific to NASM, whereas DUP
is widely supported. The reason why TIMES
is used in NASM is that one of the primary design goals of NASM was to simplify the use of directives and provide more logical, rational ways of doing things. TIMES
can be used in the way you show to fill a space with zeroes, but there are other applications (e.g. aligning loops by generating a sequence of NOP
or similar instructions), and this way a single directive can handle multiple jobs. I actually wasn’t aware that DUP
was supported in NASM at all. I guess DUP
was added in a later version for compatibility with MASM-style syntax; I’m not quite sure when; certainly it was quite late, with the result that the documentation doesn’t report that it’s available), but TIMES
is preferred (at least by the original authors of NASM) as being a more generally useful directive.
As to the meaning of $
, the reason for different sites having different descriptions is probably again due to the NASM/MASM distinction. In NASM, $
is a variable that contains the address at which the current instruction will be assembled. Note that as it is an address, it may be subject to relocation or otherwise changed during linking. It therefore cannot be used in the TIMES
instruction, whose value must be determinable at the point of assembly (it is a critical expression). $$
is the address of the start of the section, so $ - $$
is an offset, which is not subject to relocation and can therefore always be turned into a number. In MASM, $
has the same meaning, but there is no equivalent $$
variable (I believe you would achieve the same things using the WRT
operator, but it has been a while since I used MASM so I’m not sure about that).
This should allow you to work out the answer to your third question: TIMES 510-($-$$) db 0
does nothing if $-$$
is equal to 510
because the number of repetitions becomes 0, so if you know that your code is already 512 bytes long you do not need it.
0