News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to make a non-magic number

Started by thomas_remkus, January 20, 2007, 11:40:32 PM

Previous topic - Next topic

thomas_remkus

When making a non-magic number is MASM I see I can use a few methods. Using EQU or =, using .CONST, or using TEXTEQU ... are all sort of available. I understand that EQU is for integers, = is for integers that I can change the value as needed, .CONST and TEXTEQU are I guess the most confusing. One puts the data in the data segment I think and the other is just plastered all over the code as a substitute. So .CONST might lead to smaller code while TEXTEQU might lead to larger binary but faster code.

Can someone shed some more light/shadow on this for me? Is there a particular non-magic technique that will lead to the faster performance regardless over the rest?

Ratch

thomas_remkus,

QuoteWhen making a non-magic number is MASM ...

     What is all this "magic" hocus pocus you are talking about?  MASM is a completely deterministic processor, and uses no magic at all.  EQU defines its label as text.  Equals (=) defines its label as a number.  They are usually interchangable, but not always.  The MASM manual is your friend.  Ratch

thomas_remkus

A "magic number" is a number that is used in the source code that should be defined before use so it's self documenting code and so you can change the value in one location and have all references updated.

TNick

Hello!
This is how things are :)
When you want to use a variable (that is, a location in memory to store some value), you can use this ways:
- define a section in your exe to store that value; that section may be
       - readonly (you put a value in it when you build your app, and that value can't be changed during runtime); in masm, you define such a section like this: .CONST
       - not readonly
             - with an initial defined value; you define such a section like this: .DATA
             - without a initial defined value at runtime; you define such a section like this: .DATA?
Here is an example:
.DATA
  Counter   DWORD   0  ;you need this to be 0 so that,
                                  ;when you start your loop, you don't have to put one instruction
                                  ; mov Counter, 0
.DATA
  pMemory  DWORD   ? ;you don't care about thisone, because it's value will came from HeapAlloc
.CONST
  AppName   BYTE    "My great program",0 ;this will never change, but you need it somewhere in memory

Everything till here was pieces of memory. But, let's say that you want to add, somewhere in your code,
an offset.
mov  eax,   dword ptr [edx+20]
this would be no problem, but you need to do the same thing somewhere else. If, after a while, you came to realize that the offset that should be added is 32, you have to find every piece of code to see where you used that offset and to change it to 32.
Here comes the EQU. All you have to do is place a
MyOffset  EQU  20
somewhere at the beginning of your file, and then, in your code
mov  eax,   dword ptr [edx+MyOffset].
At assamble time, that string (MyOffset) will be replaced with it's value. When you see that you have to change the offset, there's only one place to modify
MyOffset  EQU  32

Now, TEXTEQU is simple:
MyOffset  EQU  32
MyNewOffset  TEXTEQU MyOffset

mov  eax,   dword ptr [edx+MyOffset]
mov  eax,   dword ptr [edx+MyNewOffset]
both lines will do the same thing, because MyNewOffset is just another name for 32.

Regards,
Nick
PS I know it would be hard to find some worse examples, but this is Sunday ... :)





Ratch

thomas_remkus,

QuoteA "magic number" is a number that is used in the source code that should be defined before use....

     First time I heard that phrase/word used for a defined variable or constant.  Anyway, it is easy to do that provided the definitions are coded before they are used.  Ratch

Tedd

BLAH EQU 3 will cause every usage of "BLAH" to be replaced with the value '3' before assembly - it makes using constants easier to read, and change. (This also works for text, if you enclose it with angle brackets "<...>") Attempting to redefine 'BLAH' will throw up an error.

"=" is similar but won't give an error if you redefine its value.

TEXTEQU is pretty much the same, but doesn't require angle brackets, and can do macro substitutions.

.CONST is a section, similar ".DATA", but is labelled read-only; direct modifications are usually picked up by the assembler. However, it's down to the OS to enforce this by placing the data on a read-only memory page - attempting to change data in this section should generate an exception.
No snowflake in an avalanche feels responsible.

thomas_remkus

The documentation that I am reading, "Microsoft® Macro Assembler Programmer's Guide", I read:

symbol EQU expression
The symbol is a unique name of your choice, except for words reserved by
MASM. The expression can be an integer, a constant expression, a one- or
two-character string constant (four-character on the 80386/486), or an
expression that evaluates to an address.


From your description I can do this:

MyName EQU <"thomas remkus">

Is that right? Is this something that's actually in the description above but I'm just not getting? Either way, thanks. I'm trying to go through the docs carefully and hope I'm not being too picky.

Tedd

Some stolen examples..

MASM    EQU      5.1 + 0.9         ;Evaluates to 6.0
Msft    EQU      <Microsoft>       ;String equate
libpath TEXTEQU  @Environ(<LIB>)   ;Text Macro Equate
endstr  TEXTEQU  %(endadr-staradr) ;String equate to numeric value


(don't ask precisely what they do :bdg)
No snowflake in an avalanche feels responsible.

thomas_remkus

Yeah, some of that was so over my head ... I'll just say ... thanks for the "Microsoft" (<Microsoft>) string example and the extra float example.

ChrisLeslie

thomas

QuoteA "magic number" is a number that is used in the source code that should be defined before use so it's self documenting code and so you can change the value in one location and have all references updated.
You are defining a variable or an equate depending on how you interpret your sentence.

A "magic number" is one that defines a partilcular entity. For example, .exe files may physically begin with "ML" or "PE", either of which define the executable in some manner.

Also, please keep in mind that equates do not attach a value to a type in some immutable fashion. Rather, equates mearly define a symbol used for substitution in your code during preprocessing. Hence they cannot be altered later in your code. Doing so will create an undefined symbol.

Chris

zooba

'Magic number' is a term used in universities. It refers to a numeric constant used without knowing what it is, for example, the maximum value of a loop or a state value. To 'remove' 'magic numbers', you define a constant. The most likely equivalent is "static final int MY_CONSTANT = 100;" (Java) though "static const int MY_CONSTANT = 100;" (C++) is probably a close second ("#define MY_CONSTANT 100" (C) is generally frowned upon, but it should still be taught).

The most direct equivalent in MASM is the EQU statement. "MY_CONSTANT EQU 100" It behaves similarly to all of them, but IMO is better than any of them. Without compiler optimisations, the Java(?) and C++ ones will allocate run-time memory for the constant, while the C one will simply replace text at compile time (identical to TEXTEQU, incidentally). Hence, any calculations done in a constant will be evaluated once in Java/C++ and stored (probably at compile time) while the C one will calculate it every time you use it.

EQU will evaluate it once and store it in a symbol table while it's compiling. Anywhere you use that symbol, the value is replaced.

Also, before the topic goes off topic, Chris's "magic number" has the emphasis on "magic", ie. it is a number representing the magic. Thomas's "magic number" has the emphasis on "number", ie. it is a number which is magical (ie. you don't know how it works or where it came from). Both should use equates, otherwise the "magic number" is as much a "magic number" as anything else :bg

Cheers,

Zooba :U