News:

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

How to write numeric labels?

Started by fonolite, April 22, 2010, 06:35:58 AM

Previous topic - Next topic

fonolite

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?

jj2007

Try this:
X = 0
REPEAT 100
@CatStr(<entry>, %X, <:>)
X = X + 1
ENDM

fonolite

Oh, Great! Thanks so much.

Your code works fine.

But what's wrong with my macro?

Text macro isn't sufficient?

clive

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
It could be a random act of randomness. Those happen a lot as well.

jj2007

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 ---

fonolite

Thanks a lot again. You guys or girls?  :toothy

dedndave

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

clive

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)
It could be a random act of randomness. Those happen a lot as well.