The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: fonolite on April 22, 2010, 06:35:58 AM

Title: How to write numeric labels?
Post by: fonolite on April 22, 2010, 06:35:58 AM
I want to write labels like this.
entry0:
entry1:
entry2:
...

I defined a macro.
X = 0
REPEAT 100
entry%X:
X = X + 1
ENDM

But this macro shows error A2070. (Operand is expected)
(entry<%X>: has same error.)

What is the problem?
Title: Re: How to write numeric labels?
Post by: jj2007 on April 22, 2010, 06:42:05 AM
Try this:
X = 0
REPEAT 100
@CatStr(<entry>, %X, <:>)
X = X + 1
ENDM
Title: Re: How to write numeric labels?
Post by: fonolite on April 22, 2010, 06:46:59 AM
Oh, Great! Thanks so much.

Your code works fine.

But what's wrong with my macro?

Text macro isn't sufficient?
Title: Re: How to write numeric labels?
Post by: clive on April 22, 2010, 12:31:18 PM
I think it's a matter of when MASM thinks it's doing string or numeric substitution. There are probably several ways of doing it if you want to wrestle with MASM syntax.

        .386
        .MODEL FLAT,C
.CODE

start:

MakeLabel MACRO   Head, Tail
&Head&&Tail&:
ENDM

X = 0
REPEAT 100
MakeLabel entry, %X
X = X + 1
ENDM

END start
Title: Re: How to write numeric labels?
Post by: jj2007 on April 22, 2010, 02:56:02 PM
Quote from: fonolite on April 22, 2010, 06:46:59 AM
Oh, Great! Thanks so much.

Your code works fine.

But what's wrong with my macro?

Text macro isn't sufficient?

The % operator has two functions:
- it forces expansion
- it translates numbers into strings
For the latter, you need string concatenation with CATSTR or @CatStr:

Quoteinclude \masm32\include\masm32rt.inc

.code
AppName   db "Masm32:", 0

start:

   X = 0
   REPEAT 2
     tmp$ CATSTR <entry>, %X, <:>      ; the % translates numeric X into a string
     % echo tmp$         ; optional: shows the label in the batch file's output window
     tmp$
     X = X + 1
   ENDM

   REPEAT 2
     tmp$ CATSTR <entry>, %X, <:>
     echo tmp$      ; no %: shows 'tmp$' instead of the label
     tmp$
     X = X + 1
   ENDM

   REPEAT 2
     @CatStr(<entry>, %X, <:>)      ; shorter but no tmp to show
     X = X + 1
   ENDM
   echo --- we are done ---

   MsgBox 0, "Hello World", addr AppName, MB_OK
   exit

end start

You should see something like this in your IDE's output window:
Assembling: tmp_file.asm
entry0:
entry1:
tmp$
tmp$
--- we are done ---
Title: Re: How to write numeric labels?
Post by: fonolite on April 22, 2010, 11:37:25 PM
Thanks a lot again. You guys or girls?  :toothy
Title: Re: How to write numeric labels?
Post by: dedndave on April 23, 2010, 01:41:46 AM
we get gals in here from time to time, but these are guys
Clive (aka "Neo") is from England and JJ (aka Jochen) is from Italy
I am from Arizona and the pic is my wifee (who is from England)
most gals that come in here are overwhelmed by all the hunky geeks that hang out in here   :P
Title: Re: How to write numeric labels?
Post by: clive on April 24, 2010, 12:50:24 PM
I'm from England, but live outside Chicago, USA.

MASM needs to be in MACRO expansion mode, so you either need to use your own macro, or use a system one. As JJ suggests @CatStr works quite well (MASM 6+), though I'd put the colon outside.

The most compact form appears to be

@CatStr(<entry>, %X):

Which you could call with

   call @CatStr(<entry>, 55)

or

   call @CatStr(<entry>, %Y)