1

I've searched the web but haven't found anything that answers my question so hopefully someone on here will be able to answer it.

Using GNU AS with Intel syntax (.intel_syntax noprefix) gives mixed results. Using:

mov ax, variable

variable = 5

Compiles as mov ax, [5], whereas

variable = 5

mov ax, variable

Compiles as mov ax, 5.

Does anyone know why defining the variable after its referenced makes it a memory offset rather than a constant value?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Just shows why you should always use `[]` if you want a memory reference and `offset` if you want the address. I guess the behavior was chosen so that you can use the second form if you wanted to define immediate constants (assuming you do that before referencing), while the first one works for labels. – Jester Dec 16 '21 at 00:25
  • You may also look at the AS Manual [Setting Symbols](https://sourceware.org/binutils/docs/as/Setting-Symbols.html) and the further explanation under [.set](https://sourceware.org/binutils/docs/as/Set.html). The `=`, equivalent to `.set`, allows you to change the value and type. – David C. Rankin Dec 16 '21 at 00:36
  • I've edited the question to be more specific as to what I'm querying. – robertapengelly Dec 16 '21 at 00:42
  • I'm by no means a `nasm` expert but in your first example the assembler doesn't know what `variable` is on the first pass of assembly. Perhaps in that case, for whatever reason, it assumes the yet-to-be rest lved symbol is a memory reference. – lurker Dec 16 '21 at 04:02
  • 2
    @lurker: NASM doesn't have this ambiguity; no `[]` means you always get the "value" of the symbol. But unfortunately this is GAS `.intel_syntax noprefix` where it assumes a load if it hasn't seen the symbol yet in its one pass. [Distinguishing memory from constant in GNU as .intel\_syntax](https://stackoverflow.com/q/39355188) covers how to write unambiguous code, and my guess at the internals that lead to this poor behaviour; specifically that GAS describes itself as a one-pass assembler. – Peter Cordes Dec 16 '21 at 04:08
  • @PeterCordes thanks for the link to the other question it makes a bit more sense now. – robertapengelly Dec 16 '21 at 13:02

0 Answers0