NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: ben321 on February 12, 2023, 01:37:08 PM

Title: I can't do this simple expression with TIMES directive.
Post by: ben321 on February 12, 2023, 01:37:08 PM
When I use the code
Code: [Select]
times 1024-$ db 0it's supposed to fill the remainder of the output file in question with the value 0x00. For example, if there were 1000 bytes of code before it, then it only needs to write 24 bytes of 0x00, to make the total file size 1024 bytes. But this isn't doing that. It's saying there's an error because it's a non-constant expression. But it IS constant. $ is not something that will change at runtime. It will change as code is written, but it becomes fixed (constant) at the moment of compiling. I can even use this alternative that DOES work
Code: [Select]
times 1024-$+start db 0where "start" is the label at the start of my program, and thus has an offset of 0 (making the expression mathematically equivalent to doing 1024-$+0, but guess what literally using 1024-$+0 doesn't work again). I had assumed that numbers and labels were BOTH treated as numeric constants internally by the assembler (and thus any expression consisting of arithmetic involving only constants would itself be a constant), but it seems they aren't. This seems like a bug.
Title: Re: I can't do this simple expression with TIMES directive.
Post by: fredericopissarra on February 12, 2023, 02:30:23 PM
Read NASM documentation about $$ token (3.5, expressions).
Title: Re: I can't do this simple expression with TIMES directive.
Post by: ben321 on February 12, 2023, 09:06:25 PM
Read NASM documentation about $$ token (3.5, expressions).

I was using the $ token, not the $$ token.
Title: Re: I can't do this simple expression with TIMES directive.
Post by: debs3759 on February 12, 2023, 10:11:11 PM
What you want is

Code: [Select]
times 1024+$$-$ db 0

where $$ is the address of the start of your code. Without it, nasm doesn't assume where your code starts. That's why Fred pointed you to the relevant nasm docs section relating to $$. You want the current offset relative to the start of your code.