-2

Book on Assembly - The constants are substituted for their defined values during the assembly process. As such, a constant is not assigned a memory location. So how and where constants are stored in memory?

<name>    equ    <value>
  • 1
    Often as immediates in the machine code when used like `add eax, `. (e.g. [Hello, world in assembly language with Linux system calls?](https://stackoverflow.com/q/61519222) shows a use like that with a string length the assembler calculated.) Or not at all, when used like `times db 0xff`. It's a similar idea to the C preprocessor `#define `, except in most assemblers `equ` is evaluated as a number, not a text substitution. – Peter Cordes Oct 03 '20 at 17:49
  • 1
    They aren't stored anywere in particular. The assembler and linker resolve constants during assembly and linking. – fuz Oct 03 '20 at 18:05
  • Think of this as a #define in C its just a search and replace by the tool before assembling. – old_timer Oct 03 '20 at 23:25

1 Answers1

3

In code compiled into machine code, many constants are replicated as needed — if one constant is used in two different lines of code (whether in different functions or not), it is likely replicated.  Often, this is visible in the assembly language, if that is the source or an intermediate for the machine code.

Constants are often found within immediate fields of machine code instructions.

So how and where constants are stored in memory?

As programs (modulo the cache which caches memory) are too large to fit in the processor, a program's machine code program instructions are stored in memory, so in theory, we can identify where in memory (what address or addresses) copies of these constants occur encoded in machine code instructions.

The construct name equ value does not consume any locations for that alone, it is the use of name in other assembly instructions that causes the value (or some adjustment of that) to be encoded in their machine code translations.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • 1
    Note that an `equ` constant might only be used as the sizes of a couple arrays, never as an immediate. Or only used as `1< – Peter Cordes Oct 04 '20 at 02:27