News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

how to do unique labels in PB or MASM

Started by bobl, March 10, 2016, 06:35:42 AM

Previous topic - Next topic

bobl


#COMPILE EXE
#DIM ALL

MACRO FUNCTION add_3_ones (num)
  MACROTEMP i, result
  'function add_3_ones (num as long) as long
  DIM i AS LONG, result AS LONG
  result = num
  !mov eax,4
again:
  !dec eax
  !jz finish
  !add result,1
  !jmp again
finish:
  'function=result
  'end function
END MACRO = result

FUNCTION PBMAIN () AS LONG
    ? add_3_ones(2)
    ? add_3_ones(3)  'error AGAIN: Duplicates the built-in compiler definition
    WAITKEY$
END FUNCTION


I'm prototyping a part of my app in a scripting language at the moment but when I come to the next bit I'd like to do it in my own little asm-based language. I was planning on doing it in PB but hit an obstacle a while back re not being able to use macros more than once that contained labels 'cos I struggled to make the labels unique
Unless I'm mistaken I don't think you can macrotemp labels like you can variables.
I have a feeling masm's preprocessor might provide a solution by allowing me to token paste unique labels.
Any thoughts re how this could be achieved would be much appreciated.
As stated I'm not ready to go yet but I've wondered about this for a while and it's a nice break from scripting languages :)




jj2007

Here is a MASM example:
include \masm32\include\masm32rt.inc

method1 MACRO arg
Local lbl
  jmp lbl
  print "wow"
lbl:
ENDM

meth2ct=0
method2 MACRO arg
Local tmp$
  tmp$ CATSTR <lbl>, %meth2ct
  % echo tmp$
  meth2ct=meth2ct+1
  jmp tmp$
  print "wow"
tmp$:
ENDM

method3 MACRO arg
Local tmp$
  tmp$ CATSTR <lbl>, %@Line
  % echo tmp$
  jmp tmp$
  print "wow"
tmp$:
ENDM

.code
start:
method1
method2
method3
method1
method2
method3
  inkey "ok"

  exit

end start

bobl

jj That's great!
I'm having a few problems running it in the MASM32 editor but I'm sure it's me or my anti-virus software
i.e. I get the whirly thing despite masm creating an exe.
It's been a while since I last assembled anything so I'll have a play.
Thank you very much.

bobl

I just deleted .obj and .exe and did a console buildall in the masm32 editor and then ran the exe.
It runs now but I just see "ok" i.e. I'm not seeing the print "wow" statements that I think I should.
Sorry for being a plonker...but what am I not doing right.

bobl

My mistake...as part of the build process I'm getting the following and lbl0 - lbl37 look very much like unique labels at the top of the "build" which is I suppose when these should be injected.
I'm just not used to seeing stuff like that there which seems to signal masm's flexibility :).
Thank you once again.

Assembling: C:\masm32\unique_labels.asm

***********
ASCII build
***********

lbl0
lbl34
lbl1
lbl37
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

Volume in drive C has no label.
Volume Serial Number is C43C-B7A4

Directory of C:\masm32

10/03/2016  07:41               492 unique_labels.asm
10/03/2016  09:05             2,560 unique_labels.exe
10/03/2016  09:05             1,263 unique_labels.obj
               3 File(s)          4,315 bytes
               0 Dir(s)  15,986,855,936 bytes free
Press any key to continue . . .



jj2007

Quote from: bobl on March 10, 2016, 08:05:54 PMI'm not seeing the print "wow" statements that I think I should.

Check \Masm32\help\opcodes.chm for jmp  ;)

bobl

See what I mean about a plonker :)
Thanks for the hint