Home > Net >  Assemble-time read the value of a data variable
Assemble-time read the value of a data variable

Time:12-13

I am making a sort of test operating system, as a proof of concept. I am using NASM as my assembler, and am wondering if I can multiply a constant (ie. 512) by a variable (ie. SECTOR_COUNT).

In case it helps, the actual command that I use to compile the boot loader is as follows:

nasm -f bin boot.asm -o os.bin

an example of what I want to do is as follows:

begin_main_program:

times (512*[SECTOR_COUNT]-511)-($-begin_main_program)

when I try to compile that, NASM says that I need to pass in a constant value. So, I'm wondering if there is a way to do the above, and not have to pass in a constant.

EDIT: fixed times (512*SECTOR_COUNT-511)... to be times (512*[SECTOR_COUNT]-511)... (a de referenced pointer i guess)

CodePudding user response:

That's not how assembly language works. It assembles bytes into the output file, and there's no way to read back those bytes at assemble time. "Variable" is a high-level concept which you can implement in asm, but isn't natively supported1.

If you want to have multiple things depend on the same value,
use foo equ 123 to define an assemble-time constant you can use later in multiple places.

To assemble some bytes in data memory with that value, use bar: dw foo.

It's an assemble-time constant so you can do things like resb foo*512 or
times (512*foo-511)-($-begin_main_program)

If you only did bar: dw 123, there'd be no way to get at the 123 while assembling other lines. (The bar: label is separate from the dw 123 or db 123, 0 or whatever you choose to put before or after it. It gives you a way to refer to that address from elsewhere, e.g. dw bar to assemble a pointer, or mov ax, [bar] to assemble an instruction that at run-time will load from that absolute address.)


Footnote 1: Except in NASM's macro language, with stuff like %assign i i 1 - useful inside a %rep 10 / %endrep block.
https://nasm.us/doc/nasmdoc4.html#section-4.1.8

  • Related