News:

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

local proc

Started by korte, August 30, 2009, 02:03:25 PM

Previous topic - Next topic

korte

I would like to define a local procedure




proc1 proc xx:dword,yy:dword
  local anythiinh1:dword
.
.
.
   call  _subproc1
.
.
.
   ret   ; return of proc1

_subproc1:
  ; want use proc1 local's

  ;        MASM insert LEAVE instruction, program halt
   ret

proc1 endp


dedndave

you can turn epilogue off and on...

        OPTION  EPILOGUE:NONE
_subproc1:

        ret
        OPTION  EPILOGUE:EpilogueDef

EpilogueDef is the default epilogue (leave, possibly pop's, and ret n)
the assembler generates an epilogue whenever it sees the RET instruction

Ghandi

Is this what you mean? Looking at the assembled binary in OllyDbg, examples one, two and four assemble to identical code. (You wrote about it as i was slapping this together, dedndave ;) )

HR,
Ghandi


.386
.model flat, stdcall
option casemap:none

include windows.inc

include kernel32.inc
includelib kernel32.lib


;EQUATES
xx EQU dword ptr [ebp+08h]
yy EQU dword ptr [ebp+0Ch]
anything EQU dword ptr [ebp-04h]

;PROTOTYPES
SomeProc2 PROTO :DWORD,:DWORD
SomeProc3 PROTO :DWORD,:DWORD
SomeOtherProc PROTO :DWORD
SomeProc4 PROTO :DWORD,:DWORD

;CODE
.code
start:

;Calling example one, no PROC statement or such
push 00001234h
push 0000FFFFh
call SomeProc


;Calling example two, PROC statement and embedded C3h
;(RET) byte to prevent LEAVE or such being inserted
invoke SomeProc2,0000FFFFh,00001234h

;Calling example three, PROC statement used, secondary function instead of internal.
invoke SomeProc3,0000FFFFh,00001234h

;Calling example four, epilogue off
invoke SomeProc4,0000FFFFh,00001234h

invoke ExitProcess,NULL




;Example One: No PROC statement, no INVOKE parameter checking though.
SomeProc:
push ebp
mov ebp,esp
add esp,-4
mov eax,xx
mov ecx,yy
shl eax,16
and ecx,0000FFFFh
or eax,ecx
mov anything,eax
call SomeInternalProc
mov eax,anything
add esp,4
pop ebp
Ret 8

SomeInternalProc:
mov eax,anything
not eax
mov anything,eax
ret



;Example Two: PROC statement used, embedded C3h byte to prevent LEAVE instruction being generated
SomeProc2 PROC _xx:DWORD,_yy:DWORD
LOCAL _anything:DWORD

mov eax,_xx
mov ecx,_yy
shl eax,16
and ecx,0FFFFh
or eax,ecx
mov _anything,eax
call SomeInternalProc2
mov eax,_anything
Ret

SomeInternalProc2:
mov eax,_anything
not eax
mov _anything,eax
db 0C3h
SomeProc2 EndP



;Example Three: PROC statement used, external function called. Address of variable passed.
SomeProc3 PROC _xx:DWORD,_yy:DWORD
LOCAL _anything:DWORD

mov eax,_xx
mov ecx,_yy
shl eax,16
and ecx,0FFFFh
or eax,ecx
mov _anything,eax
invoke SomeOtherProc,ADDR _anything
mov eax,_anything
Ret
SomeProc3 EndP

SomeOtherProc PROC lpAnything:DWORD
mov eax,lpAnything
mov ecx,[eax]
not ecx
mov [eax],ecx
Ret
SomeOtherProc EndP



;Example Four: EPILOGUE OFF and PROLOGUE OFF
SomeProc4 PROC _xx:DWORD,_yy:DWORD
LOCAL _anything:DWORD

mov eax,_xx
mov ecx,_yy
shl eax,16
and ecx,0FFFFh
or eax,ecx
mov _anything,eax
call SomeInternalProc3
mov eax,_anything
Ret

OPTION EPILOGUE:NONE
OPTION PROLOGUE:NONE
SomeInternalProc3:
mov eax,_anything
not eax
mov _anything,eax
ret
OPTION  EPILOGUE:EpilogueDef
OPTION PROLOGUE:PrologueDef


SomeProc4 EndP

end start

dedndave

mine took less time Ghandi - lol
EDIT - altering the prologue is not required in this case
the assembler only generates a prologue for PROC directives

Ghandi

lol, so true dedndave. :) Thanks for the tip with prologue, i only usually use it to wrap PROC statements, which is why i use it in a macro to set both prologue and epilogue off and on again:


NAKED_PROC_START MACRO
  OPTION EPILOGUE:NONE
  OPTION PROLOGUE:NONE
ENDM

NAKED_PROC_END MACRO
  OPTION EPILOGUE:EpilogueDef
  OPTION PROLOGUE:PrologueDef
ENDM

;Example
NAKED_PROC_START
SomeProcGoesHere PROC
  ;Code goes here
  RET
SomeProcGoesHere ENDP
NAKED_PROC_END



HR,
Ghandi

korte


Ghandi:
The second example is ingeniously simple. I  defined a "locRet" macro.

Thank you everyone

hutch--

 :bg

There is an even simpler way.


MainProc proc args:etc ...

  ; normal code

  ret

  subproc:

  retn

MainProc endp


Manually specifying RETN (near return) solves the problem of confusing MASM's PROC statement with an extraneous RET.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Ghandi

Well... That sort of made my post rather redundant... lol, thanks for the simpler solution Hutch.  :clap: I honestly never thought of it because i (lazily) use RET for everything.

HR,
Ghandi

dedndave

i like my soultion better, Hutch   :lol
it gets the attention of someone reading the code and tells them we specifically turned epilogues off and back on   :P