News:

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

on $Const

Started by savage, May 26, 2006, 04:01:21 PM

Previous topic - Next topic

savage

I'm trying to make a macro called $Const which I could use in places where immediate values aren't allowed, such as floating point operations, and thatwill reuse a const if it has already been created.  If it's possible then I'm sure it's already been created, but I can't seem to find any examples. So I decided it should be easy enough to make.

Here is an example of how I'd use it:


fldpi
fimul $Const(2)
fiadd $Const(4+1)




I've gotten it partially to work by simply having it create a variable for each constant.
for example c_2 for $Const(2) and c_5 for $Const(4+1).

Here is my original working macro:



; For example, $Const(4) will generate "c_4 dd 4"
$Const MACRO Num
   cName textequ @CatStr(<c_>,<%Num>)
       .const
           cName dd Num
       .code
   EXITM cName
ENDM





I would like for it to use the constant if it is already defined, which is why I didn't make it local to the macro.
The question is, how do I test if it is already defined?


fiasub $Const(5+5)
fiasub $Const(10)

The above will trigger a redefinition error. 

Here is my attempt at solving this redefinition error, (note that my purpose here is to avoid creating multiple constants with the same value, but instead, reuse those values):


$Const MACRO Num
   cName textequ @CatStr(<c_>,<%Num>)
   IFNDEF cName
       .const
           cName dd Num
       .code
   ENDIF
   EXITM cName
ENDM



I have tried using IFNDEF, but it doesn't seem to test text-to-variable-name expressions, but rather it simply tests if exactly what i typed is defined. In other words, this macro will ALWAYS think cName is defined, since it is defined as a "textequ". That's just fine and dandy that cName itself is defined, but this is not what I need to know; I want to know if the variable which is named --expand text from cName here-- is defined

Ossa

Try this,

$Const MACRO Num
cName textequ @CatStr(<c_>,<%Num>)

% IFNDEF cName
.const
cName dd Num
.code
ENDIF
EXITM cName
ENDM


(its the other use of %)

[edit] to explain a bit more fully - % at the start of a line will expand all macro expressions on that line (in the middle of a line it behaves differently) [/edit]

Ossa

[edit] i had added a "LOCAL cName" line... but just realised that its not needed... doesn't have to be removed though [/edit]
Website (very old): ossa.the-wot.co.uk

Ossa

Sorry to harp on a bit, but here's the relevent section from the MASM Programmer's Guide:

QuoteExpansion Operator as First Character on a Line

The expansion operator has a different meaning when used as the first character on a line. In this
case, it instructs the assembler to expand any text macros and macro functions it finds on the rest of
the line.

This feature makes it possible to use text macros with directives such as ECHO, TITLE, and
SUBTITLE, which take an argument consisting of a single text value. For instance, ECHO displays its
argument to the standard output device during assembly. Such expansion can be useful for debugging
macros and expressions, but the requirement that its argument be a single text value may have
unexpected results. Consider this example:
 
ECHO    Bytes per element: %(SIZEOF array / LENGTHOF array)
 
Instead of evaluating the expression, this line echoes it:
 
Bytes per element: %(SIZEOF array / LENGTHOF array)
 
However, you can achieve the desired result by assigning the text of the expression to a text macro
and then using the expansion operator at the beginning of the line to force expansion of the text
macro.

temp    TEXTEQU %(SIZEOF array / LENGTHOF array)
%       ECHO    Bytes per element: temp

 
Note that you cannot get the same results simply by putting the % at the beginning of the first echo
line, because % expands only text macros, not numeric equates or constant expressions.

Here are more examples of the expansion operator at the start of a line:
 
; Assume memmod, lang, and os specified with /D option
%   SUBTITLE  Model: memmod  Language: lang  Operating System: os
; Assume num defined earlier
tnum    TEXTEQU %num
%       .ERRE   num LE 255, <Failed because tnum !> 255>

 

Ossa
Website (very old): ossa.the-wot.co.uk

savage

Thanks Ossa, this works like a charm.  And it's so obvious! Doh.  But thanks for the clarification, I understand it MUCH better now =)

Sorry I didn't reply yesterday... I couldn't login, I sure hope masmforum finds a good home soon.

Ossa

No problem, glad it helped.

I have to say that the MASM macro syntax (when using the more advanced features - % and &) is one of the things that I understand least well (about anything)... it's just so darn complex... I always find myself twisting lines of code into knots with extra & and % symbols and dummy variables and... well, I guess thats why so few people use or understand them fully.

Ossa
Website (very old): ossa.the-wot.co.uk

stanhebben

Same. Are there actually any good guides for them?

Ossa

The only reference that I've found is the MASM Programmer's Guide, which you can get from Randall Hyde's website: http://webster.cs.ucr.edu/AsmTools/MASM/MASMDoc/MASM-programmersguide611.zip

There are very few examples, that I've ever seen, on the internet about using these more advanced features.

Ossa
Website (very old): ossa.the-wot.co.uk