The MASM macro functionality if very powerful. You can validate the types of the parameters and even do conditional assembly. Combined with the Conditional Moves CMOVcc available since the Intel P6 family processors you could optimize your procedure to
min MACRO arg1:REQ, arg2:REQ, arg3:REQ
; Returns result in EAX register
IF ((OPATTR (arg2)) AND 00000100b) or ((OPATTR (arg3)) AND 00000100b)
ECHO ### Function min: No constants allowed as arguments
ELSEIF ((OPATTR (arg1)) AND 00000100b)
; Move constant to EAX if it's not EAX
mov eax, arg1
ELSEIF (SIZE TYPE arg1 NE 4)
ECHO ### Function min: First parameter must be of DWORD type
ELSEIF SIZE TYPE arg2 NE 4
ECHO ### Function min: Second parameter must be of DWORD type
ELSEIF SIZE TYPE arg3 NE 4
ECHO ### Function min: Third parameter must be of DWORD type
ELSEIFDIF <arg1>, <eax>
; Move first value to EAX if it's not EAX
mov eax, arg1
ENDIF
cmp eax, arg2 ; compare arg1 to arg2
cmovg eax, arg2 ; move arg2 to EAX if EAX is greater than arg2
cmp eax, arg3 ; compare arg1 to arg3
cmovg eax, arg3 ; move arg3 to EAX if EAX is greater than arg3
ENDM
In the above MACRO the OPATTR (arg1)) AND 00000100b
checks if arg1
is a constant value. This is important, because the CMOVcc
instructions do not take constants as arguments. You could extend the MACRO to move constants to registers or temporary variables with conditional assembly, but this is not realized in the above code.
The SIZE TYPE arg1 NE 4
directives check if the size of the argument if 4, register or memory - preventing a possible error with the MOV
. You could extend this to use MOVZX
/MOVSX
with further conditional assembly.
The IFDIF <arg1>, <eax>
checks if the first argument is equal to the string "EAX". This is done to avoid setting the EAX
register if the value is already in it.
Errors occurring are ECHOed to the console starting with a ###
providing additional information at assembly time.
This MACRO is far from being complete. It only takes care of some possible errors and could be extended to realize even more automatisation. But IMHO it shows an approach worth of being comtemplated.