@ArgRev MACRO args:VARARG

LOCAL arg,y
y TEXTEQU <>
FOR arg,<&args>
y CATSTR <arg>,<!,>,y
ENDM
y SUBSTR y,1,@SizeStr(%y) - 1
EXITM y
ENDM



@SaveRegs MACRO regs:VARARG
LOCAL myEnd, num, flag, reg

num=0
flag=0
WHILE flag EQ 0
% IFDEF @CatStr(<RealEnd>,<%num>)
num = num + 1
ELSE
flag = 1
ENDIF
ENDM

% FOR reg, <regs>
push reg
ENDM

@RestoreRegs MACRO
@CatStr(<RealEnd>,<%num>) %num
ENDM

@CatStr(<RealEnd>,<%num>) MACRO depth:REQ
LOCAL reg
IF (OPATTR (myEnd)) AND 0100000y
RealEnd&depth %(depth-1)
ELSE
myEnd LABEL BYTE ;; dummy
% FOR reg, < @ArgRev( [u]&[/u]regs ) > ;<<-- the board had a problem with this line
pop reg
ENDM
ENDIF
ENDM
ENDM


;Example usage:
@SaveRegs eax,ebx,ecx,edx
@SaveRegs esi,edi,eax,ebx,ecx,edx
@SaveRegs esi,edi
@RestoreRegs
@RestoreRegs

@SaveRegs esi,edi,eax,ebx,ecx,edx
@RestoreRegs
@RestoreRegs
Just have equal pairs of Saves/Restores and the stack will be the same as you left it. I just wrote this to fix the versions in the MASM manual. That version had a single global varible to hold which registers were pushed - no flexiblity what-so-ever. :) I hope someone can use it?
Posted on 2001-10-29 01:49:18 by bitRAKE
Great macros bitRAKE.

Prevents me from doing stupid mistakes in reverse order poping.
I alredy have them added to my sniplets.

KetilO
Posted on 2001-10-29 02:54:52 by KetilO
Should have just posted the file - board doesn't catch '&regs' in a code block. Should read &regs between the quotes. :) Here is the file form.

Please re-download: It's fixed.
Posted on 2001-10-29 04:24:06 by bitRAKE
bitRAKE,

seems that last @RestoreRegs produces no code?! :)

japheth
Posted on 2001-10-29 05:29:57 by japheth
I thought I was being smart, and broke it. :)
Should be fixed.

japheth, you were using the downloaded code, not the post code, right?

Oh, you can certainly pass values other than registers! Anything that can be pushed/poped. f0dder, was the inspiration for this one - this is just a modified version of the advanced BEGIN/END byte offset macro I did. Let me go find it so everyone knows what I'm talking about - it's a very handy LIFO stack using local symbols. I'm wondering how far I can take this stuff before I run up against MASM's internal limits?
Posted on 2001-10-29 05:44:50 by bitRAKE
Well, your "title" say it all, bitrake... "Use the Macro, Luke."
Posted on 2001-10-29 06:12:10 by f0dder
Hehe...yeah, cool. :alright:
Posted on 2001-10-29 06:14:52 by bitRAKE
bitRAKE,

no I used the downloaded version (the first gives assembly errors) :) .

I just included some echos in your @CatStr macro :



@CatStr(<RealEnd>,<%num>) MACRO depth:REQ
LOCAL reg
IF (OPATTR (myEnd)) AND 0100000y
@CatStr(<RealEnd>,%(depth-1)) %(depth-1)
PURGE RealEnd&depth
echo after purge
ELSE
myEnd LABEL BYTE ;; dummy
% FOR reg, < @ArgRev( &regs ) >
pop reg
ENDM
echo after pops
ENDIF


and when assembling the following code:



@SaveRegs eax,ebx,ecx,edx
@SaveRegs eax,ebx,ecx
@SaveRegs esi,edi
@RestoreRegs
@RestoreRegs

@SaveRegs esi,edi,eax,ebx,ecx,edx
@RestoreRegs
@RestoreRegs


I should get 4 times "after pops", but what I get is:



Microsoft (R) Macro Assembler Version 6.15.8803
Copyright (C) Microsoft Corp 1981-2000. All rights reserved.
Assembling: GetIconn.asm
after pops
after pops
after purge
after pops
after purge
Linking...

GetIconn.exe - 0 error(s), 0 warning(s)



so the error seems to be "real" (Could you please explain why I get 5 displays!? :) ).

japheth
Posted on 2001-10-29 06:52:39 by japheth
Do not use PURGE version, that is the error.
Re-Download, or just comment out the PURGE line. :)
Have you looked at the code produced?
I'll explain later - have to go to work, been up all night.

I think it's still got errors. :) The @RestoreRegs macro get redefined everytime the @SaveRegs macro is called. Need to PURGE and have @RestoreRegs selective execute the other macro based on the highest number not PURGE'd. Will fix later...(this means the other(BEGIN/END) one is broken, too.)
Posted on 2001-10-29 07:56:14 by bitRAKE
I've given up on the fancy approach! :)
PURGE doesn't work like I thought it does.
This one really works.
@ArgRev MACRO args:VARARG

LOCAL arg,y
y TEXTEQU <>
FOR arg,<&args>
y CATSTR <arg>,<!,>,y
ENDM
y SUBSTR y,1,@SizeStr(%y) - 1
EXITM @CatStr(<!<>,y,<!>>)
ENDM


StackGlobalNumber = 0

@StackSave MACRO vars:VARARG
LOCAL var

StackGlobalNumber = StackGlobalNumber + 1
@CatStr(<StackGlobalContent>,%StackGlobalNumber) TEXTEQU @ArgRev(&vars)
FOR var,<&vars>
push var ;; Push each value
ENDM
ENDM

@StackRestore MACRO
LOCAL var
% FOR var,@CatStr(<StackGlobalContent>,%StackGlobalNumber)
pop var
ENDM
StackGlobalNumber = StackGlobalNumber - 1
ENDM
Posted on 2001-11-04 10:11:10 by bitRAKE
Better attach it, bitRAKE. Remind the "&-problem".:)

EDIT: &-Problem seems to be solved!
EDIT: And it really works and is much easier to understand, bitRAKE :alright: .

But thread name "nested macro magic..." is now wrong ;)
Posted on 2001-11-04 10:42:48 by japheth
The problem isn't fixed, I just didn't include any HTML codes in the source. :)
&reg
Yeah, but I didn't want to start a new thread for this.

The simple solution is usually the best.
I was trying to show-off and I messed it all up. :grin:
Posted on 2001-11-04 12:46:52 by bitRAKE