News:

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

Main Menu

Macro help needed!

Started by NoCforMe, July 22, 2022, 02:15:57 PM

Previous topic - Next topic

NoCforMe

OK, I'm really not a macro guy. I started fooling around trying to write one, and all those %, &, ! symbols, substitution, things magically turning into other things, etc., just about made my head explode.

So I'm seeking your help. Here's what I want to do. Given a list of identifiers and associated numbers like so:

<IMAGE_REL_ARM_MOV32, 2>
<IMAGE_REL_ARM_SECREL, 3>
<IMAGE_REL_ARM_SECTION, 4>
<IMAGE_REL_ARM_BRANCH11,5>
*

I'd like to see the following as output:

IMAGE_REL_ARM_MOV32 equ 2
IMAGE_REL_ARM_SECREL equ 3
IMAGE_REL_ARM_SECTION equ 4
IMAGE_REL_ARM_BRANCH11 equ 5


(Actually the output would ideally be in the form of ECHO statements, so the output could be redirected to a text fiile.)

I also need to be able to separate the identifiers (like IMAGE_REL_ARM_MOV32) and be able to use them in a conditional statement to see if they're already defined:


IFNDEF (identifier)
... do something
... do something else
ENDIF


* The angle brackets around these are just a guess as to how to format these. May not be needed at all. I'm assuming these would be fed into a FOR loop.
Assembly language programming should be fun. That's why I do it.

jj2007

Quote from: NoCforMe on July 22, 2022, 02:15:57 PMGiven a list of identifiers and associated numbers like so:

<IMAGE_REL_ARM_MOV32, 2>
<IMAGE_REL_ARM_SECREL, 3>
<IMAGE_REL_ARM_SECTION, 4>
<IMAGE_REL_ARM_BRANCH11,5>

How is that list "given"? By including a text file, as in Windows.inc?

IMAGE_REL_ARM_BLX11              equ 0009h
IMAGE_REL_ARM_SECTION            equ 000Eh
IMAGE_REL_ARM_SECREL             equ 000Fh


That would be easy:

include \masm32\include\masm32rt.inc ; pure Masm32 SDK
; include MyNewEquates.inc

echoMe MACRO arg
Local tmp$
  tmp$ CATSTR <&arg equ >, %arg
  % echo tmp$
ENDM

.code
start:
  echoMe IMAGE_REL_ARM_BLX11
  echoMe IMAGE_REL_ARM_SECTION
  echoMe IMAGE_REL_ARM_SECREL
  inkey "Look at the echos in your output window"
  exit
end start


Attached EchoMe.asc opens in WordPad, MS Word, and RichMasm.

P.S., same but output goes to DOS prompt:
include \masm32\include\masm32rt.inc ; pure Masm32 SDK
; include MyNewEquates.inc

echoMe MACRO arg
Local tmp$
  tmp$ CATSTR <chr$("&arg equ >, %arg, <")>
  % echo tmp$
  print tmp$, 13, 10
ENDM

.code
start:
  echoMe IMAGE_REL_ARM_BLX11
  echoMe IMAGE_REL_ARM_SECTION
  echoMe IMAGE_REL_ARM_SECREL
  exit
end start


Note that tmp$ CATSTR <chr$("&arg equ >, %arg, <")> expands to e.g. chr$("IMAGE_REL_ARM_BLX11 equ 9"); text arguments are in <brackets>, while numbers are converted to text using %mynumber. Inside macros, the arguments can be embedded, as in text &arg& text

The line echoMe IMAGE_REL_ARM_BLX11 passes to the macro both the text IMAGE_... and the number 9; inside the macro, arg is thus, as you wish, a text or a number.

NoCforMe

Um, close, but not exactly. What I want is to create a list of equates and see which ones are already defined in an include file, say windows.inc. Those that aren't included will be echoed to the output stream. So instead of having to write an entry for each one as in your example, this should all happen within a FOR loop. Somewhere inside that loop, if the equate from the list passes the IFNDEF test, it will be echoed. Does that make sense?

Trying to save myself a lot of typing here.
Assembly language programming should be fun. That's why I do it.

jj2007

#3
That's not possible with macros, I'm afraid. But it's relatively easy with Recall and Instr. Can you post the include file that you want to test?

include \masm32\MasmBasic\MasmBasic.inc         ; download
  SetGlobals v$
  Init
  Let esi=FileRead$("\Masm32\include\Windows.inc")      ; load wininc into a fat string
  Recall CL$(), L$()                                    ; put the command line file into an array of strings
  For_ each edi in L$()                                 ; parse the array
        .if Instr_(edi, ",")                            ; this line has a comma
                lea ecx, [eax+1]                        ; put ecx after the comma
                Let v$=Trim$(Left$(edi, edx-1))         ; extract the name of the variable
                .if Val?(ecx)>0 && eax<9                ; check if it's a number
                        .if Instr_(esi, v$, 4)
                                PrintLine "*** [", v$, "] found in Windows.inc" ; IMAGE_REL_ARM_MOV32 equ 2
                        .else
                                Print v$, Str$("\tequ %i\n",  Val(ecx))         ; not found
                        .endif
                .endif
        .endif
  Next
EndOfCode


The line .if Instr_(esi, v$, 4) checks if the name of the variable is found in Windows.inc; mode 4 means case-sensitive, full word.

HSE

Hi NoC!

To make a list is more complex than to test each item:



checkforNoC macro arg1, arg2
  ifndef &arg1
    &arg1 equ &arg2
    echo &arg1 was undefined
  endif
endm

checkforNoC IMAGE_REL_ARM_MOV32, 2
checkforNoC IMAGE_REL_ARM_SECREL, 3
checkforNoC IMAGE_REL_ARM_SECTION, 4
checkforNoC IMAGE_REL_ARM_BRANCH11, 5



HSE
Equations in Assembly: SmplMath

NoCforMe

HSE: Close, but keep in mind that I want the output to look like this:

IMAGE_REL_ARM_MOV32 EQU 2

That should be possible, right?

I'm trying to generate a list of EQUates that are missing from another include file.

OK, I tried this based on your example:

checkEQU macro arg1, arg2
ifndef &arg1
echo &arg1 EQU &arg2
endif
endm

checkEQU IMAGE_REL_ARM_MOV32, 2
checkEQU IMAGE_REL_ARM_SECREL, 3
checkEQU IMAGE_REL_ARM_SECTION, 4
checkEQU IMAGE_REL_ARM_BRANCH11, 5


and it worked, but it only fired once: the output I got was

IMAGE_REL_ARM_MOV32 EQU 2


Why was that? It should have gone through all 4 macro invocations, right?

Duh! I had included windows.inc and those other things were in there. So it works! Thanks.
Assembly language programming should be fun. That's why I do it.

HSE

Equations in Assembly: SmplMath

jj2007

Quote from: NoCforMe on July 23, 2022, 03:08:58 AMit worked, but it only fired once: the output I got was

IMAGE_REL_ARM_MOV32 EQU 2


Why was that? It should have gone through all 4 macro invocations, right?

Well, no (output of my code attached above):
IMAGE_REL_ARM_MOV32     equ 2
*** [IMAGE_REL_ARM_SECREL] found in Windows.inc
*** [IMAGE_REL_ARM_SECTION] found in Windows.inc
*** [IMAGE_REL_ARM_BRANCH11] found in Windows.inc

NoCforMe

So this new topic in the windows.inc forum here is the reason for doing all this.
Assembly language programming should be fun. That's why I do it.

hutch--

If all you want to do is put extra data not included in other INC files,

IFNDEF YourIncFile.inc
YourIncFile.inc equ <1>

  ; Your include file data

ELSE
  echo ----------------------------------------------
  echo WARNING duplicate include file YourIncFile.inc
  echo ----------------------------------------------
ENDIF

NoCforMe

Yes, I know. But wouldn't it be better if it were there in the main file that all of us here depend on?

And yes, I know it must be a hell of a lot of work maintaining those files. That's why I asked how that actually works, who updates them. And how often are they updated?

I'd be willing to help, if that means anything.
Assembly language programming should be fun. That's why I do it.

hutch--

The problem is the single file update is never ending, the windows.inc and extra files has truly massive amounts of work done on them and few will do the work, most have swiped the contents of those 2 files for their own.

If you have extra equates, put them in a conditional block and you solve the problem. If you think the win32 versions were complex, the slightest error in the win64 version will bring it crashing down on top of you.

jj2007

Problem solved, but of course, you will have to add NoCforMe.inc to every piece of code posted here...

Quote from: jj2007 on July 23, 2022, 07:32:16 PM
include \masm32\include\NoCforMe.inc

.code
start:
  MsgBox 0, str$(IMAGE_DLLCHARACTERISTICS_APPCONTAINER), "It's easy:", MB_OK
  exit

end start

Another option:

include \masm32\include\Masm32rt.inc

IMAGE_DLLCHARACTERISTICS_APPCONTAINER=123  ; a comment gives you the chance to enhance readability

.code
start:
  MsgBox 0, str$(IMAGE_DLLCHARACTERISTICS_APPCONTAINER), "It's easy:", MB_OK
  exit

end start


I can understand Hutch. There are a few gaps, which is normal after so many years. Fill them for yourself if you really need them. Equates like IMAGE_DLLCHARACTERISTICS_APPCONTAINER are used almost never, so why bother?