The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: jj2007 on March 06, 2011, 01:12:47 AM

Title: Preloaded LOCAL variables
Post by: jj2007 on March 06, 2011, 01:12:47 AM
Looks weird but it works perfectly...

include \masm32\include\masm32rt.inc

MyTest PROTO: DWORD, :DWORD

.data
stdata SYSTEMTIME <2011,3,7,3,20,59,59,12345> ; wYear, wMonth, ... wMilliseconds
dataArg2 dd ?
dataArg1 dd ?
dataRetAdd dd ?
dataSaveEbp dd ?
dummy LABEL dword

.code
AppName db "Masm32 is great!", 13, 10, 0
Hello db "A message: ", 0

start:
mov ebx, esp
print str$(ebx), 9, "Stack", 13, 10 ; check for stack and ebp on entry
print str$(ebp), 9, "ebp", 13, 10, 10

invoke MyTest, addr Hello, offset AppName ; that looks really straightforward...

mov ebx, esp
print str$(ebx), 9, "Stack", 13, 10 ; check for stack and ebp on entry
print str$(ebp), 9, "ebp", 13, 10, 10
inkey "OK"
exit

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE
MyTest proc arg1x:DWORD, arg2x:DWORD
LOCAL SaveEbp, RetAdd, arg1, arg2, stime:SYSTEMTIME
  push ebp
  mov ebp, offset dummy
  pop SaveEbp
  pop RetAdd
  pop arg1
  pop arg2
  print arg1
  print arg2
  movzx eax, stime.wYear ; yep, this is a "preloaded LOCAL"!
  print str$(eax), 9, "Year", 13, 10
  movzx eax, stime.wMonth
  print str$(eax), 9, "Month", 13, 10
  movzx eax, stime.wMilliseconds
  print str$(eax), 9, "millisecs", 13, 10, 10
  mov edx, RetAdd
  mov ebp, SaveEbp
  jmp edx
MyTest endp
OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

end start
Title: Re: Preloaded LOCAL variables
Post by: Magnum on March 06, 2011, 02:01:27 AM
So, is there an advantage than can be had with your code ?


Andy

Title: Re: Preloaded LOCAL variables
Post by: jj2007 on March 06, 2011, 04:18:31 AM
Quote from: Magnum on March 06, 2011, 02:01:27 AM
So, is there an advantage than can be had with your code ?

You can declare a global structure "local". Code might be a bit more compact...
Title: Re: Preloaded LOCAL variables
Post by: qWord on March 06, 2011, 02:33:14 PM
might be more compacter, but it also destroy the concept of local variables (e.g. thread save variables)
Title: Re: Preloaded LOCAL variables
Post by: jj2007 on March 06, 2011, 06:47:10 PM
That is true, although imho not each and every routine has to be thread-safe.
Actually, it is just a little exercise that helped me understand what is behind LOCAL - a macro that assigns equates to memory relatetive to ebp.
Title: Re: Preloaded LOCAL variables
Post by: dedndave on March 07, 2011, 09:22:03 PM
 :8)

        INCLUDE \masm32\include\masm32rt.inc

        .DATA

             dd 256 dup(?)
EspInit      dd STD_OUTPUT_HANDLE,TxtString,sizeof TxtString,BytesWritten,0
BytesWritten dd ?
TxtString    db 'Hello Jochen!',13,10

        .CODE

_main   PROC

        mov     esp,offset EspInit
        CALL    GetStdHandle
        push    eax
        CALL    WriteFile
        CALL    ExitProcess

_main   ENDP

        END     _main
Title: Re: Preloaded LOCAL variables
Post by: dedndave on March 07, 2011, 10:03:17 PM
version 2   :P

        INCLUDE \masm32\include\masm32rt.inc

        .DATA

             dd 256 dup(?)
EspInit      dd GetStdHandle,Brnch,STD_OUTPUT_HANDLE,WriteFile,ExitProcess
hStdOut      dd ?,offset TxtString,sizeof TxtString,offset BytesWritten,0
BytesWritten dd ?
TxtString    db 'Hello Jochen!',13,10

        .CODE

_main   PROC

        mov     esp,offset EspInit
        ret
Brnch:: mov     hStdOut,eax
        ret

_main   ENDP

        END     _main
Title: Re: Preloaded LOCAL variables
Post by: jj2007 on March 07, 2011, 11:18:08 PM
Madman :cheekygreen:
Title: Re: Preloaded LOCAL variables
Post by: Antariy on March 07, 2011, 11:20:28 PM
Quote from: dedndave on March 07, 2011, 10:03:17 PM
version 2   :P

:U

Under Win9x you even may self-delete EXE after all :bg
Only one thing is - allocate more space for the stack :P
Title: Re: Preloaded LOCAL variables
Post by: dedndave on March 07, 2011, 11:52:02 PM
it shouldn't use too much stack space   :P
i could probably reduce it
Title: Re: Preloaded LOCAL variables
Post by: Antariy on March 08, 2011, 12:36:08 AM
Quote from: dedndave on March 07, 2011, 11:52:02 PM
it shouldn't use too much stack space   :P
i could probably reduce it

It is just suit to thing stated there: http://www.masm32.com/board/index.php?topic=12460.msg128614#msg128614. APIs you using very fastly come to the kernel, with less stack usage, this is reason :P
Title: Re: Preloaded LOCAL variables
Post by: hutch-- on March 08, 2011, 12:56:23 AM
I think its cute but I would be inclined to test it under DEP before you distributed any code that works like this.
Title: Re: Preloaded LOCAL variables
Post by: dedndave on March 08, 2011, 02:27:54 AM
i have no intention of publishing any code like that
it was just for fun   :bg
Title: Re: Preloaded LOCAL variables
Post by: askm on March 08, 2011, 05:09:57 PM
These are preloaded, with tilted stack.

#1
no auto exit

#2
Exit code: -1073741819

#3
Exit code: -1073741819

Perhaps a preloaded smiley face will make them work.