Why can't I declare a variable that takes the value of a dereferenced variable in Assembly?

Viewed 75

In assembly(x86 NASM), it is perfectly okay to do this

a dd 17h
a2 dd a

with these 2 lines of code in the data segment I declare a doubleword a, to which I assign the value 17h. Then I declare another doubleword and I give this doubleword the value of the address of a. This works and I understand what's going on.

But when I try to do this:

a dd 17h
a2 dd [a]

I get a syntax error! And I don't really know why. I expected this piece of code to define a doubleword a with the value 17h (this is the same as in the previous example) and then define a doubleword a2 that takes the value of a, so 17h. But as I said, this doesn't seem to work. I am confused why this wouldn't work. From what I know, we can address memory as long as we follow the address offset formula which is in the form of [base + index * scale + constant]. In my case, the address of a a is constant so that will be the constant from the formula, and the base and scale will be zero. At least this is how I thought about it. But clearly, it doesn't work.

1 Answers

But as I said, this doesn't seem to work. I am confused why this wouldn't work.

Simply because the assembler program doesn`t support it, although they could.

From what I know, we can address memory as long as we follow the address offset formula which is in the form of [base + index * scale + constant]

Yes, that`s Intel syntax to express the addressing possibilities of x86 CPUs. However, that only applies at runtime, when the chip is doing the actual addressing.

What you are asking for is that the assembler program supports a similar syntax for defining data in your source file that takes the value of another definition. They could definitely do something like that (or not), but that's up to the designer of the assembler program. It's nothing to do with x86 addressing possibilities.

Related