News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Option prologue/epilogue macro

Started by Adamanteus, December 09, 2016, 01:07:10 AM

Previous topic - Next topic

Adamanteus

 Due to this thread - for work with probing stack algo, use enter and leave instructions, is need to have in assembler sets own macro names, as NONE, PrologueDef and EpilogueDef - with some sequence of parameters, that to implement and automatically insert this macros to each procedure seen this options  :eusa_boohoo:

Vortex

Hi Adamanteus,

Reading the section User-defined prologue and epilogue macro (IA32) [8.00] in Pelle's manual, you can construct your macros. Attached is a 32-bit HJWasm example.

include testfunc.inc

CustomPrologue MACRO procname, flags, parambytes, localbytes, reglist, userparms:VARARG

    echo Procedure name      = procname

    echo Flag                = flags

    echo Byte count          = parambytes

    echo Number of locals    = localbytes

    echo Registers           = reglist

    echo Macro arguments     = userparms

    IF localbytes GT 0
   
        push    ebp
        mov     ebp,esp
        sub     esp,localbytes
       
    ELSEIF parambytes GT 0
   
        push    ebp
        mov     ebp,esp
       
    ELSEIFNB <userparms>
   
        IF @InStr(1,<userparms>,<FORCEFRAME>)

            push    ebp
            mov     ebp, esp
           
        ENDIF
       
    ENDIF
   
    IFNB <reglist>
   
        FOR     reg,reglist
       
            push    reg
           
        ENDM

    ENDIF
   
EXITM <localbytes>

ENDM


CustomEpilogue MACRO procname, flags, parambytes, localbytes, reglist, userparms:VARARG

    echo
    echo

    echo Procedure name      = procname

    echo Flag                = flags

    echo Byte count          = parambytes

    echo Number of locals    = localbytes

    echo Registers           = reglist

    echo Macro arguments     = userparms

    IFNB <reglist>
   
        FOR reg,reglist
       
            pop     reg
           
        ENDM

    ENDIF
   
    IF (parambytes GT 0) OR (localbytes GT 0)
   
        mov     esp,ebp
        pop     ebp
       
    ELSEIFNB <userparms>
   
        IF @InStr(1,<userparms>,<FORCEFRAME>)
       
            mov     esp,ebp
            pop     ebp
           
        ENDIF
       
    ENDIF
   
    IF (flags AND 10h) OR (parambytes EQ 0)
   
        ret             ; retn - Poasm
       
    ELSE
   
        ret parambytes  ; retn - Poasm

    ENDIF

ENDM

.data

msg         db 'This is a test',0
capt        db 'Hello',0

.code

start:

    invoke  testfunc,ADDR msg,ADDR capt
    invoke  ExitProcess,0

OPTION PROLOGUE:CustomPrologue
OPTION EPILOGUE:CustomEpilogue

testfunc PROC <abc> USES esi edi ebx message:DWORD,caption:DWORD

LOCAL a:DWORD
LOCAL x:DWORD
LOCAL y:DWORD
LOCAL z:DWORD

    lea     eax,a
    lea     eax,x
    lea     eax,y
    lea     eax,z

    mov     y,100
    invoke  MessageBox,0,message,caption,MB_OK
    mov     eax,y
    ret

testfunc ENDP

OPTION PROLOGUE:PROLOGUEDEF
OPTION EPILOGUE:EPILOGUEDEF

END start

Adamanteus

#2
  I hear about such type of macros, but example not found - thanks, Vortex !
And my variant, that looks for 32-bit HJWasm most universal, with few conditions :
IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    __UNICODE__ equ 1
   
    EL_NESTED equ 0
    EL_PROC equ 1
    IF EL_PROC
EL_PROLOGUE equ 1
EL_EPILOGUE equ 1
ELSE
EL_CHKSTK equ 1
ENDIF
   
    include masm32rt.inc

CustomPrologue MACRO procname, flags, parambytes, localbytes, reglist, userparms : VARARG
if 0
echo Procedure name      = procname
echo Flag                = flags
echo Byte count          = parambytes
echo Number of locals    = localbytes
echo Registers           = reglist
echo Macro arguments     = userparms
endif

IF localbytes GT 0
IF EL_PROLOGUE
ENTER localbytes, EL_NESTED
ELSE
PUSH EBP
MOV EBP, ESP
IF EL_CHKSTK
  IF @InStr(1, userparms, <CLEARFRAME>) eq 0
MOV EAX, localbytes + TYPE EBP + TYPE EAX
CALL __chkstk
  ENDIF
ELSE
SUB ESP, localbytes
ENDIF
ENDIF
ELSEIF parambytes GT 0
IF EL_PROLOGUE
ENTER 0, EL_NESTED
ELSE
PUSH EBP
MOV EBP, ESP
ENDIF   
ELSEIFNB <userparms>

IF @InStr(1, userparms, <FORCEFRAME>)
IF EL_PROLOGUE
ENTER 0, EL_NESTED
ELSE
PUSH EBP
MOV EBP, ESP
ENDIF     
ENDIF

ENDIF

IFNB <reglist>
FOR reg, reglist
PUSH reg
ENDM
ENDIF

EXITM <localbytes>

ENDM


CustomEpilogue MACRO procname, flags, parambytes, localbytes, reglist, userparms : VARARG

IFNB <reglist>   
FOR reg,reglist 
POP reg
ENDM
ENDIF

IF (parambytes GT 0) OR (localbytes GT 0)
IF EL_EPILOGUE
LEAVE
ELSE    
MOV ESP, EBP
POP EBP
ENDIF
ELSEIFNB <userparms>   
IF @InStr(1, userparms, <FORCEFRAME>)
IF EL_EPILOGUE
LEAVE
ELSE            
MOV ESP, EBP
POP EBP
ENDIF   
ENDIF   
ENDIF

IF (flags AND 10h) OR (parambytes EQ 0)
RET
ELSE
RET parambytes
ENDIF

ENDM

.data

STRING msg, "that is test"
STRING capt, "Hello,"

.code

__chkstk PROTO SYSCALL

OPTION PROLOGUE:CustomPrologue
OPTION EPILOGUE:CustomEpilogue

testfunc PROC <abc> USES esi edi ebx message : DWORD, caption : DWORD

LOCAL a:DWORD
LOCAL x:DWORD
LOCAL y:DWORD
LOCAL z:DWORD

    lea     eax,a
    lea     eax,x
    lea     eax,y
    lea     eax,z

    mov     y,100
    print caption
    print " "
    print message
    print " ", 21h, 13, 10
    mov     eax,y
    ret

testfunc ENDP

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    cls
   
    invoke testfunc, addr msg, addr capt

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start