Author Topic: I can't do this simple expression with TIMES directive.  (Read 2723 times)

Offline ben321

  • Full Member
  • **
  • Posts: 182
I can't do this simple expression with TIMES directive.
« 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.
« Last Edit: February 12, 2023, 01:39:46 PM by ben321 »

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 371
  • Country: br
Re: I can't do this simple expression with TIMES directive.
« Reply #1 on: February 12, 2023, 02:30:23 PM »
Read NASM documentation about $$ token (3.5, expressions).
« Last Edit: February 12, 2023, 02:33:35 PM by fredericopissarra »

Offline ben321

  • Full Member
  • **
  • Posts: 182
Re: I can't do this simple expression with TIMES directive.
« Reply #2 on: February 12, 2023, 09:06:25 PM »
Read NASM documentation about $$ token (3.5, expressions).

I was using the $ token, not the $$ token.

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 223
  • Country: gb
    • GPUZoo
Re: I can't do this simple expression with TIMES directive.
« Reply #3 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.
My graphics card database: www.gpuzoo.com