News:

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

question about macro

Started by minor28, November 08, 2011, 08:58:19 PM

Previous topic - Next topic

minor28


literal MACRO quoted_text:VARARG
LOCAL local_text
.data
local_text db quoted_text,0
align 4
.code
EXITM <local_text>
ENDM

CTXT MACRO quoted_text:VARARG
EXITM <offset literal(quoted_text)>
ENDM


With these two macros from the file macros.asm I can type
text directly into my code and text are declared in the .data
segment. However, if I then type the same text in a different
place in my code, the text will get it's own location in
.data segm. i.e. two offsets for the same text.

My wonder is if it is possible make the macro to search for the
text and give "local_text" the same symbol. For example if
"local_text" is the symbol "??0010" and finds the text, for example
in "??0005" change it to found location. It seems that C++
macro TEXT can do this.

I have tried but not succeeded. If possible can someone give a hint
how it can be done?

Best regards

jj2007

- get MasmBasic here
- unzip to the root of your Masm32 drive using the folder names option
- open \masm32\MasmBasic\MasmBasic.inc
- search inside the file for raFound

qWord

you must record all new declared strings in a list of text macros:
lit MACRO quoted_text:VARARG
LOCAL local_text

IFNDEF lit_cntr
lit_cntr = 0
ENDIF

; search for literal
lit_found = 0
cntr = 0
WHILE cntr LT lit_cntr
% IFIDN <&quoted_text>,<@CatStr(<lit_list_txt>,%cntr)>
lit_match TEXTEQU @CatStr(<lit_list_lbl>,%cntr)
lit_found = 1
EXITM
ENDIF
cntr = cntr + 1
ENDM
IF lit_found
EXITM lit_match
ENDIF
.data
local_text db quoted_text,0
align 4
.code
; record new string
@CatStr(<lit_list_txt>,%lit_cntr) TEXTEQU <&quoted_text>
@CatStr(<lit_list_lbl>,%lit_cntr) TEXTEQU <OFFSET local_text>
lit_cntr = lit_cntr + 1
EXITM <OFFSET local_text>
ENDM

(you may add a FOR-loop to trim the parameter quoted_text)
FPU in a trice: SmplMath
It's that simple!

jj2007

Quote from: qWord on November 08, 2011, 09:24:46 PM
you must record all new declared strings in a list of text macros:
Just for fun, here a little test - check the data section in Olly:

include \masm32\MasmBasic\MasmBasic.inc   ; download
include qWordLitMacro.asm

   Init

   PrintLine lit("This is a qWord test")
   PrintLine lit("This is a qWord test")
   PrintLine lit("This is a qWord test")

   PrintLine "This is a MasmBasic test"
   PrintLine "This is a MasmBasic test"
   PrintLine "This is a MasmBasic test"

   Inkey "ok"
   Exit
end start


minor28

Quote from: qWord on November 08, 2011, 09:24:46 PM
you must record all new declared strings in a list of text macros:

Is strings not automatically redorded with symbols ??0001 and upwards?

I'll look into your suggestions.

thank you

hutch--

I would be surprised if you can do it in any practical sense, some compilers of old had a technique for comparing string data before they wrote the string data into the object module but it would be very difficult to do in modern MASM code at a general purpose level.

The other factor is you don't need to do this if string size is a problem and you don't want duplicate strings, you simply assign the string to a variable of whatever scope you need once then use the variable for each occurrence where you need it.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

qWord

Quote from: hutch-- on November 08, 2011, 11:10:25 PM
I would be surprised if you can do it in any practical sense, some compilers of old had a technique for comparing string data before they wrote the string data into the object module but it would be very difficult to do in modern MASM code at a general purpose level.
well, at least inside a module you can avoid duplicates. The above macro simply record the macros parameter (quoted literals) in a list of text macros. Before declaring a new string, this list searched and, if found, the corresponding label is returned.
FPU in a trice: SmplMath
It's that simple!

jj2007

Quote from: hutch-- on November 08, 2011, 11:10:25 PM
I would be surprised .... it would be very difficult to do in modern MASM code at a general purpose level.

So you mean that both qWord and myself are geniuses? Thank you :bg

ToutEnMasm

To avoid the replicate of the same chain with differents label just use this macro:
The LabelName is optionnal and if exist ,just do an error at compile time
The first call of the macro give the value of the label
Quote
   CREDATA MACRO quoted_text:REQ,LabelName
      LOCAL Local_Label,verif
   IFNB <LabelName>         ;
      verif SUBSTR <LabelName>,1,1
         IFIDN verif,<">            ; "
         .ERR  <delet the " >         
      ENDIF
   Local_Label TEXTEQU <LabelName>      ;
   ENDIF
   verif SUBSTR <quoted_text>,1,1            
   IFDIF verif,<">
      .ERR  <CHAIN add the " >
   ENDIF   
      .data
      Local_Label db quoted_text,0
      .code
      EXITM <Local_Label>
   ENDM
Or the same without verif:
Quote
   CREDATA MACRO quoted_text:REQ,LabelName
      LOCAL Local_Label,verif
   IFNB <LabelName>         
      Local_Label TEXTEQU <LabelName>      
   ENDIF
      .data
      Local_Label db quoted_text,0
      .code
      EXITM <Local_Label>
   ENDM