In nasm (Netwide Assembler), it is possible to find the address of the current instruction with $, and the address of the current section with $$. By using ($-$$) we therefore get the length of the section so far, and with the full instruction:
times 512-($-$$) db 0
we pad the remainder of a sector with zeros. This is typically used in small hello-world programs for OS development.
I have seen that GNU assembler supports constructs like rept to make repetions, and . (dot) can be used to get the current address. But I have not yet figured out exactly how to do the whole thing in gas.
So how can it be done?