News:

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

need help with macros!

Started by rafaly, November 02, 2006, 05:03:03 PM

Previous topic - Next topic

rafaly

 Hi friends

I am new here and would like to ask if there is a tutorial somewhere about writing macros because I have
no idea on how to make them.

basically my problem is I have a program where the same thing appears a lot

Invoke wsprintf, addr wdldest, addr fmt, addr name, addr variable   
Invoke lstrlen, addr wdldest                                         
dec eax
Invoke send, sock, addr wdldest, eax, 0

where variable is the only thing that changes.. so I have to put these 4 lines of code always there and only change variable which is really bad in my opinion.

How should I proceed with this? Thanks a lot for the help.

rafaly

zooba

This is exactly what macros were designed for. At their simplest, they do an automatic copy-paste-edit. This macro will do what you want:

SendVariable MACRO variable
    Invoke wsprintf, addr wdldest, addr fmt, addr name, addr variable   
    Invoke lstrlen, addr wdldest                                         
    dec eax
    Invoke send, sock, addr wdldest, eax, 0
ENDM


To use it:

SendVariable myVariable

And anywhere you put that, it's as if you'd copy-paste-edited the code yourself. A huge advantage is that you only have to edit it in one place and not hundreds. A disadvantage is that your executables can get really big quite quickly as the code is being put everywhere. For something like this, you may be better off using a procedure.

There are heaps of examples of advanced macros on the forum, a search for 'MACRO' should find them. It's possible to create some really intricate code with them - for example, I once created a (bad) arithmetic code generator, which would take a standard style expression and generate all the 'add', 'sub', etc. commands. I say bad because it was so horribly inefficient (used the stack too much) but it really is the extreme. MACROS.ASM has a lot of simpler macros in it which are helpful for picking up the basics.

Cheers,

Zooba :U

rafaly


Thank you a lot! It has worked really well.