4

I am trying to understand the difference between the MASM EQU and TEXTEQU directives. So far all I have been able to gather is that the syntax is slightly different and that EQU macros cannot be redefined while TEXTEQU macros can. They still seem very similar in my mind, so are there other differences? In what situations should I use EQU rather than TEXTEQU and vice versa?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
canac
  • 15,440
  • 2
  • 16
  • 11
  • 2
    See section 8.5 of [MASM: DIRECTIVES & PSEUDO-OPCODES (Part 1)](http://www.oopweb.com/Assembly/Documents/ArtOfAssembly/Volume/Chapter_8/CH08-1.html). – lurker Mar 30 '15 at 20:26
  • No promises, as this isn't exactly your question, but it's not totally off-base with what you're asking: http://stackoverflow.com/questions/28948274/x86-assembly-equ-vs/28952568#28952568 If that doesn't help ask some more. – User.1 Mar 30 '15 at 20:43
  • 1
    Live link: [8.5 MASM: Directives & Pseudo-Opcodes](https://www.plantation-productions.com/Webster/www.artofasm.com/DOS/ch08/CH08-1.html#HEADING1-206) – Miscreant Oct 23 '20 at 06:23

1 Answers1

3

EQU is more general in that it allows numeric constants as well as text constants. EQU also explicitly states that a text value can be changed after declaration. While the documentation is confusing, the statement "The name cannot be redefined later" only applies to the first form of EQU "name EQU expression", whereas the second form of EQU "name EQU <text>" is annotated with "The name can be assigned a different text later."

TEXTEQU, on the other hand, only deals with text literals. The use of the normal double quoted text, literals proceeded by % (which I've never seen), and the values of macros. The latter two types do not seem to be supported by EQU, but I have not tested that.

For the most part, if you're only creating text constants of the double quoted variety, they seem interchangeable.

Michael Karcher
  • 3,803
  • 1
  • 14
  • 25
Sparafusile
  • 4,696
  • 7
  • 34
  • 57
  • Does MASM `equ` evaluate expressions like `msglen equ $ - msg` on the spot, at the location of the `equ`, or does it substitute that `$ - msg` into somewhere that uses it? I think FASM is the latter (resulting in a much-too-large size), while NASM is the former. In FASM the fix is to use `msglen = $ - msg`, and I think MASM also supports `=` for numeric expressions. – Peter Cordes Apr 19 '19 at 13:38
  • It's my understanding that EQU and TEXTEQU are directives that get expanded during the assembly process. That is to say that their references are replaced with their actual value when the code is turned into machine code. The value does not get stored in memory and referenced later. I'm not sure how that would even work without allocating memory for it explicitly. – Sparafusile Apr 22 '19 at 16:14
  • Yes of course, but the question is whether it's evaluated to a number once, at the place where you write `equ`, or whether it's re-evaluated at every place you use the symbol. e.g. whether writing `mov ecx, msglen` gives you the right number, or whether it's exactly equivalent to writing `mov ecx, $-msg` somewhere other than right after `msg`. – Peter Cordes Apr 22 '19 at 17:52
  • 1
    The value of the directive is only evaluated once and then stored in a key/value store during assembly. Every usage of the directive name gets replaced with the original, constant value. This is the only way it would work as expected. – Sparafusile Apr 22 '19 at 21:28
  • That's how it works in NASM, but *not* how it works in FASM. I was asking because I didn't know what the expected behaviour was in MASM, not having actually used it. Thanks for clearing that up; maybe worth adding that to your answer. – Peter Cordes Apr 22 '19 at 23:03
  • 2
    @PeterCordes EQU directive in MASM supports all variants of expression expansion and behavior is controlled by syntax of expression in EQU directive. If you have varaibles `Var1=5` and `Var2=1` and then you define expression `expr1 EQU Var1 * Var2` then `expr1` will evaluate to value 5 anywhere you refer to `expr1` in the code even if you later change value or `Var2=2`. If you define as `expr2 EQU ` though then MASM stores this expression itself in its symbol table and delay its evaluation in places where `expr2` is referenced. It will evaluate to 10 if you change `Var2=2` later – Petr Lazecky Apr 02 '23 at 15:11