The MASM Forum

Miscellaneous => The Orphanage => Topic started by: Magnum on November 30, 2012, 03:49:44 AM

Title: Goasm -> Masm
Post by: Magnum on November 30, 2012, 03:49:44 AM
This is what I have so far on converting this Goasm code to masm syntax.

I don't need a complete conversion as I want to learn a lot of things.

I am looking at whether this will need a  (1) _main   PROC, or (2) a start: , end start
I will also convert many of the calls to invoke.

Title: Re: Goasm -> Masm
Post by: dedndave on November 30, 2012, 04:31:30 AM
MASM will accept Start...
Start:
        mov     eax,0
        INVOKE  ExitProcess,eax

        END     Start


or a PROC...
_main   PROC

        mov     eax,0
        INVOKE  ExitProcess,eax

_main   ENDP

        END     _main


i prefer the later, as it allows for the use of LOCAL vars
it also looks neater and, i think, is more readable   :P
when you use LOCAL, the assembler generates a prologue
it also generates an epilogue anywhere RET is used in that PROC
seeing as the _main PROC rarely has a RET, no epilogue is generated
as you know, when the assembler generates one of the prologues, it uses stack space
normally, the stack space is released in the epilogue
seeing as there is no RET and no epilogue, there is no balancing of the stack
however, ExitProcess releases any stack space used by the process thread

this may not apply for PROC's used in threads
i.e., the operating system may not free the stack for ExitThread the same way as for ExitProcess
this can cause a memory leak
when i write a thread PROC, i am careful not to use any parameters or USED on the PROC line
i am also careful not to use LOCAL in thread PROCs
when you start a thread, there may be one caller parameter available in [ESP+4], however
when i terminate a thread, i make sure the stack is balanced
Title: Re: Goasm -> Masm
Post by: MichaelW on November 30, 2012, 10:56:28 PM
Why not just return from the thread procedure?
Title: Re: Goasm -> Masm
Post by: dedndave on December 01, 2012, 06:35:48 AM
well - according to the documentation, it is prefered to use ExitThread

i also see the docs state that it releases the stack space
i don't think that was in there in older versions - it may be new with vista/win 7 or something
Title: Re: Goasm -> Masm
Post by: MichaelW on December 01, 2012, 07:10:57 AM
Returning from the thread procedure triggers an implicit call to ExitThread, so everything happens as it should and it's easier to code.