The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: UtillMasm on January 24, 2010, 06:07:54 AM

Title: Just another "Hello World!"
Post by: UtillMasm on January 24, 2010, 06:07:54 AM
 :bg
Test.asm.386
includelib \masm32\lib\kernel32.lib
extrn _ExitProcess@4:near,_WriteConsoleA@20:near,_GetStdHandle@4:near
_DATA segment USE32 'DATA'
  Num dd 0
  Msg db 'Hello,World!',13,10,0
  MsgBytes=$-Msg-1
_DATA ends
_TEXT segment dword public USE32 'CODE'
  assume cs:_TEXT,ds:_DATA
  _main:push dword ptr -11
  call _GetStdHandle@4
  push dword ptr 0
  push dword ptr offset Num
  push dword ptr msgbytes
  push dword ptr offset Msg
  push eax
  call _WriteConsoleA@20
  push dword ptr 0
  call _ExitProcess@4
_TEXT ends
end _main

Test.cmd@echo off
\masm32\bin\ml.exe /c /coff /FoTest.obj /nologo Test.asm
\masm32\bin\link.exe /subsystem:console /out:Test.exe Test.obj /nologo
pause
Title: Re: Just another "Hello World!"
Post by: sinsi on January 24, 2010, 06:12:12 AM

push dword ptr 0
push dword ptr msgbytes
push dword ptr offset Msg

No need for 'dword ptr'  :bg
Title: Re: Just another "Hello World!"
Post by: UtillMasm on January 24, 2010, 06:13:35 AM
 :U
but, i just collect "Hello World!".
Title: Re: Just another "Hello World!"
Post by: Slugsnack on January 25, 2010, 12:17:19 PM
Should the handle returned from GetStdHandle() be closed with CloseHandle() ?
Title: Re: Just another "Hello World!"
Post by: donkey on January 25, 2010, 02:37:50 PM
Quote from: Slugsnack on January 25, 2010, 12:17:19 PM
Should the handle returned from GetStdHandle() be closed with CloseHandle() ?

Its really unclear and only sparsely documented as to whether STD handles should be closed or not, I err on the side of caution and do not close them since during the run of a process the call will not spawn new resources anyway.
Title: Re: Just another "Hello World!"
Post by: dedndave on January 25, 2010, 03:15:28 PM
an old DOS throwback is if you closed a STDIO handle, that function ceased to operate until you re-boot - lol
i could not find a specific mention of it in MS docs
i doubt that it matters, as they are aliased handles, anyways
supposedly, the ExitProcess call will close any handles left open that should be closed
Title: Re: Just another "Hello World!"
Post by: Slugsnack on January 25, 2010, 07:07:49 PM
Quote from: donkey on January 25, 2010, 02:37:50 PM
Quote from: Slugsnack on January 25, 2010, 12:17:19 PM
Should the handle returned from GetStdHandle() be closed with CloseHandle() ?

Its really unclear and only sparsely documented as to whether STD handles should be closed or not, I err on the side of caution and do not close them since during the run of a process the call will not spawn new resources anyway.
I've read that the handle should be closed but apparently, unlike many other functions, that handle is not a duplicate so should only be closed after you do not intend that object anymore. However.. there is something that confuses me. StdOut in MASM32 calls GetStdHandle() without ever closing the result.. It would seem to me that this would result in some sort of memory leak ?

@dedndave : in this case, the handle returned is not an alias, according to the guy on the bottom of this page at least : http://msdn.microsoft.com/en-us/library/ms683231(VS.85).aspx
And yes, the handles SHOULD be closed after ExitProcess() but we could not fool ourselves that that is an excuse not to free memory and not to close handles, right ? :]
Title: Re: Just another "Hello World!"
Post by: GregL on January 25, 2010, 11:10:33 PM
I agree with Donkey, you don't need to close them. StdOut, StdIn and StdErr are special handles that are created when the console is created, so it's not your responsibility to close them. You can close them with CloseHandle if you really need to, but it's not neccessary.

  Console Handles (Windows) (http://msdn.microsoft.com/en-us/library/ms682075%28VS.85%29.aspx)