This file can be included to 32bit programs written in EuroAssembler.
The library contains OS-independent macroinstructions which extend generic pseudoinstruction
PROC, ENDPROC, CALL
.
Macroinstructions Procedure and EndProcedure hide the prologue and epilogue of standard call calling convention , where the arguments are pushed backwards and they are removed by the called procedure.
Macroinstructions in this library comunicate with one another
at assembly time using the preprocessing %variables
%LocalFrameSize
and %NrOfArg_ProcedureName
.
stdcall HEAD
Implementation of standard call convention in €ASM uses formal %names for accessing Procedure parameters, local stack-memory variables and all eight general-purpose registers saved on stack.
Following example shows the stack frame created by invoking Procedure with the name
Function which has three parameters and uses two local stack variables
with sizes 4 and 8. Prologue of Function Procedure Param1, Param2, Param3
will assign "global" %variables
%NrOfArg_Function %SETA 3
and %LocalFrameSize %SETA 4+8
, they will be used by epilogue in EndProcedure Function
during
Invoke Function, Param1, Param2, Param3
.
; Formal %names assignment: %Param8 %SET EBP+64 %Param7 %SET EBP+60 %Param6 %SET EBP+56 %Param5 %SET EBP+52 %Param4 %SET EBP+48 %Param3 %SET EBP+44 %Param2 %SET EBP+40 %Param1 %SET EBP+36 %ReturnEAX %SET EBP+28 %ReturnECX %SET EBP+24 %ReturnEDX %SET EBP+20 %ReturnEBX %SET EBP+16 %ReturnEBP %SET EBP+08 %ReturnESI %SET EBP+04 %ReturnEDI %SET EBP+00
PUSHAD
and
MOV EBP,ESP
as the procedure prolog.EndProcedure Move
.
%LocalFrameSize %SETA 0 ; This %variable propagates to macro LocalVar, if used in Move Procedure
.
%Source %SET EBP+36 ; These are formal %variables of all operands.
%Destination %SET EBP+40
%Size %SET EBP+44Procedure %MACRO Operands ; Prologue of standard calling convention procedure. LblCheck %IF "%:" == "" %ERROR ID=5921, 'Macro "Procedure" requires a label.' %EXITMACRO Procedure %ENDIF LblCheck %%NrOfArg_%: %SETX %# %LocalFrameSize %SETA 0 ArgNr %FOR 1..%#, STEP=+1 %%%*{%ArgNr} %SETX EBP+(32+%ArgNr<<2) ; >> %ENDFOR ArgNr %::: PROC %=*, NESTINGCHECK=OFF ; Open the namespace. PUSHAD MOV EBP,ESP %ENDMACRO Procedure
EBP-4
Block LocalVar Size=1K ; %Block is now EBP-1028
.
ClearLocalVar ; Fill Block and BlockSize with 0.
MOV [%BlockSize],1K, DATA=DWORD
LEA EDI,[%Block]
...
EndProcedure ProcNameSUB ESP,%Size
to reserve room on the machine stack.
%LocalFrameSize
which was initialized in macro Procedure
.LocalVar %MACRO Size=4 LblCheck %IF "%:"=="" %ERROR ID=5922, 'Macro "LocalVar" requires a label.' %EXITMACRO LocalVar %ENDIF LblCheck SizeCheck %IF %# %ERROR ID=5923, 'Macro "LocalVar" does not expect ordinal parameters.' %EXITMACRO LocalVar %ENDIF SizeCheck %: %COMMENT ; This makes the label of macro invokation void %ENDCOMMENT %: ; so it does not declare a symbol. %LocalVarSize %SETA (%Size + 3) & ~3 ; Round up to DWORD. %LocalFrameSize %SETA %LocalFrameSize + %LocalVarSize SUB ESP, %LocalVarSize %%%: %SETX EBP-%LocalFrameSize ; Assign formal %name to the id specified with LocalVar label. %ENDMACRO LocalVar
We could as well decide to initialize each local variable individually, in this case the macro ClearLocalVar will not be used in the Procedure body at all.
ESP
, cleared size is specified with "global" variable
%LocalFrameSize
.ClearLocalVar %MACRO %IF %LocalFrameSize MOV EDI,ESP MOV ECX,%LocalFrameSize/4 XOR EAX,EAX REP STOSD %ENDIF %ENDMACRO ClearLocalVar
Macro EndProcedure terminates context of the previously opened
Procedure
. This epilogue of StdCall convention will discard local variables defined with
LocalVar using machine instruction
MOV ESP,EBP
, restore all GP registers using
POPAD
and then return to the parent code
which the Procedure was Invoked from.
Operands pushed on stack in the Invoke statement
will be discarded here by this EndProcedure macro, using
RET 4 * NrOfArg
.
All registers are preserved throughout the procedure invokation
unless the procedure changed their stored value
on the stack frame (writing to [%ReturnEAX]
for instance).
CPU flags are not preserved, EndProcedure returns
with the same flag values which were set at the
EndProcedure entry.
Programmer should never use explicit machine instructionRET
to return from the block defined withProcedure .. EndProcedure
. If premature return is required, jump to the label ofEndProcedure
statement instead.
EndProcedure %MACRO ProcName OpCheck %IF "%ProcName" == "" %ERROR ID=5924, 'Macro "EndProcedure" requires one operand.' %EXITMACRO EndProcedure %ENDIF OpCheck %ProcNameStrip %SET %ProcName %WHILE "%ProcNameStrip[%&]" == ":" ; Get rid of trailing colons. %ProcNameStrip %SET %ProcNameStrip[1..%&-1] %ENDWHILE %NrOfArg %SET2 %%NrOfArg_%ProcNameStrip NestChck %IF "%NrOfArg" == "" %ERROR ID=5925, '"%ProcName Procedure" statement missing.' %EXITMACRO EndProcedure %ENDIF NestChck MOV ESP,EBP POPAD RET 4 * %NrOfArg ENDP %ProcName, NESTINGCHECK=OFF ; Terminate the namespace. %ENDMACRO EndProcedure
Invoke %MACRO StdProcedure, Arguments ArgCheck %IF "%StdProcedure" == "" %ERROR ID=5926, 'Macro "Invoke" requires the name of called Procedure.' %EXITMACRO Invoke %ENDIF ArgCheck ArgNr %FOR %#..2, STEP= -1 PUSHD %*{%ArgNr} %ENDFOR ArgNr CALL %StdProcedure %ENDMACRO Invoke
ENDHEAD stdcall