Hey everyone,
Please pardon me, I'm VERY new at this. I'm trying to learn masm... I wish to write my own code, and not include libraries, etc. I've got some code I've made for a simple hello world application that compiles, links, etc., but doesn't seem to do anything, and I can't figure it out.
I was hoping a guru here could point me in the right direction.
;╒══════════════════════════════════════════════════════════════════════════╕
;│ HW.ASM Console Hello World created: 05/30/09 │
;│ Generic Win32 Application Template │
;╘══════════════════════════════════════════════════════════════════════════╛
TITLE Console Hello World
include windows.inc
.data
;╒══════════════════════════════════════════════════════════════════════════╕
;│ D A T A │
;╘══════════════════════════════════════════════════════════════════════════╛
szMessage SBYTE "Hello World!",13,10,0
.data?
;╒══════════════════════════════════════════════════════════════════════════╕
;│ U N I N I T I A L I Z E D D A T A │
;╘══════════════════════════════════════════════════════════════════════════╛
dwWritten DWORD ?
.code
;╒══════════════════════════════════════════════════════════════════════════╕
;│ EXECUTION BEGINS HERE ... │
;╘══════════════════════════════════════════════════════════════════════════╛
Start: invoke GetStdHandle, STD_OUTPUT_HANDLE ; standard output handle
mov edx, eax
invoke WriteConsole, edx, ADDR szMessage, 14, ADDR dwWritten, 0
invoke ExitProcess, eax ; eax == Exit Code
;╒══════════════════════════════════════════════════════════════════════════╕
;│ EXECUTION TERMIATES HERE ... │
;╘══════════════════════════════════════════════════════════════════════════╛
END Start
And my inc...
.386 ; 32-Bit when .386 appears before .MODEL
.MODEL FLAT, STDCALL
;╒══════════════════════════════════════════════════════════════════════════╕
;│ ************************************************************************ │
;│ │
;│ MICROSOFT WINDOWS 32-BIT API ASSEMBLY LANGUAGE HEADER FILE │
;│ by Jacob Janzen │
;│ * minimal includes * │
;│ ************************************************************************ │
;╘══════════════════════════════════════════════════════════════════════════╛
option casemap:none
;╒══════════════════════════════════════════════════════════════════════════╕
;│ From KERNEL32.LIB and WINBASE.H │
;└──────────────────────────────────────────────────────────────────────────┘
STD_OUTPUT_HANDLE equ -11d
GetStdHandle PROTO :DWORD
WriteConsoleA PROTO :DWORD, :DWORD, :DWORD, :DWORD, :DWORD
WriteConsole equ <WriteConsoleA>
ExitProcess PROTO WINAPI :DWORD
;┌──────────────────────────────────────────────────────────────────────────┐
;│ END OF THE WINDOWS.INC FILE │
;╘══════════════════════════════════════════════════════════════════════════╛
Thanks in advance.
assuming your asm file is named demo.asm you can build it to a console application using the code below
;build.bat
\MASM32\BIN\ML /c /coff /Cp demo.asm
\MASM32\BIN\link /SUBSYSTEM:console /LIBPATH:c:\masm32\lib demo.obj
pause
Quote from: E^cube on July 14, 2009, 01:45:09 AM
assuming your asm file is named demo.asm you can build it to a console application using the code below
;build.bat
\MASM32\BIN\ML /c /coff /Cp demo.asm
\MASM32\BIN\link /SUBSYSTEM:console /LIBPATH:c:\masm32\lib demo.obj
pause
Thanks, but my problem is with the code itself; it doesn't do anything when run. I can assemble and link it just fine (And I DO have the /SUBSYSTEM:CONSOLE argument)... The thing is, when I run it, nothing happens... No text in command prompt, no errors, nothing.
My MAKEFILE:
PROJECT = hw
OBJ_CORE = hw.obj
ALL: $(PROJECT).exe
#╒══════════════════════════════════════════════════════════════════════════╕
#│ Assembler and Linker Options │
#└──────────────────────────────────────────────────────────────────────────┘
AssemblerOptions = /nologo /c /coff
LinkerOptions = /SUBSYSTEM:CONSOLE
#╒══════════════════════════════════════════════════════════════════════════╕
#│ Inference Rule for Updating Object Files │
#└──────────────────────────────────────────────────────────────────────────┘
.asm.obj:
J:\DEV\BIN\ML $(AssemblerOptions) $<
#╒══════════════════════════════════════════════════════════════════════════╕
#│ Build Rule for Executable │
#└──────────────────────────────────────────────────────────────────────────┘
$(PROJECT).exe: $(OBJ_CORE) $(OBJ_MORE)
J:\DEV\BIN\LINK $(LinkerOptions) @<<LinkFile
/MACHINE:i386
/SUBSYSTEM:WINDOWS,4.0
/ENTRY:Start
/MAP:$(PROJECT).map
/OUT:$(PROJECT).exe
$(OBJ_CORE)
$(OBJ_MORE)
J:\DEV\LIB\KERNEL32.LIB
<<NOKEEP
Thanks,
Hi jake072. The problem is with your windows.inc, you do not have your functions properly resolved.
QuoteMicrosoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
test.obj : error LNK2001: unresolved external symbol _GetStdHandle@4
test.obj : error LNK2001: unresolved external symbol _WriteConsoleA@20
test.obj : error LNK2001: unresolved external symbol _ExitProcess@4
test.exe : fatal error LNK1120: 3 unresolved externals
_
Link error
I used the defaults that come with Masm32 (\masm32\include\masm32rt.inc includes anything you would need) and compiled it just fine.
hiya Jake - welcome to the forum
when i want to write to console like this, i use WriteFile, but WriteConsole should work also
QuoteszMessage SBYTE "Hello World!",13,10,0
use db instead of SBYTE
QuoteszMessage db "Hello World!",13,10
also, the string "szMessage" does not need to be 0 terminated with these functions
Quote invoke WriteConsole, edx, ADDR szMessage, 14, ADDR dwWritten, 0
the "14" may be easier replaced by "sizeof szMessage" - if you change the message or are too lazy to count bytes, it will be good
Quote invoke WriteConsole, edx, ADDR szMessage, sizeof szMessage, ADDR dwWritten, 0
btw - don't let it frustrate you - we have all been there - lol
treat it as a simple learning experience and stick with it - you will get better every day
Jake,
When you run a console app, start the console first because unless the code is written to not terminate, it flashes past that quick that you often don't see it.
Quote from: disintx on July 14, 2009, 01:54:15 AM
Hi jake072. The problem is with your windows.inc, you do not have your functions properly resolved.
QuoteMicrosoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
test.obj : error LNK2001: unresolved external symbol _GetStdHandle@4
test.obj : error LNK2001: unresolved external symbol _WriteConsoleA@20
test.obj : error LNK2001: unresolved external symbol _ExitProcess@4
test.exe : fatal error LNK1120: 3 unresolved externals
_
Link error
I used the defaults that come with Masm32 (\masm32\include\masm32rt.inc includes anything you would need) and compiled it just fine.
Weird, it compiles ok for me.
Quote from: dedndave on July 14, 2009, 02:23:59 AM
hiya Jake - welcome to the forum
when i want to write to console like this, i use WriteFile, but WriteConsole should work also
QuoteszMessage SBYTE "Hello World!",13,10,0
use db instead of SBYTE
QuoteszMessage db "Hello World!",13,10
also, the string "szMessage" does not need to be 0 terminated with these functions
Quote invoke WriteConsole, edx, ADDR szMessage, 14, ADDR dwWritten, 0
the "14" may be easier replaced by "sizeof szMessage" - if you change the message or are too lazy to count bytes, it will be good
Quote invoke WriteConsole, edx, ADDR szMessage, sizeof szMessage, ADDR dwWritten, 0
btw - don't let it frustrate you - we have all been there - lol
treat it as a simple learning experience and stick with it - you will get better every day
Thanks!
I removed the trailing null, and used sizeof instead, thanks!
I'm starting to wonder if Windows 7 console does not have this, as you all seem to say that it works, but I get no text... Perhaps it works all along, but not for Windows 7? I do run it from the console, after nmake... Grr =) !!
I'm going to try the WriteFile instead, to see if that works, thanks so much for the help.
P.S. db = a double byte right? Can db be substituted for DBYTE? I'm trying to use 'extended' names so I don't constantly forget what I'm looking at =)
Hi,
Quote from: jake072 on July 14, 2009, 01:02:29 PM
Weird, it compiles ok for me.
Read E'cube's post carefully! It's the one which contains the crucial hint. The other posts are just smalltalk.
"db" means "define byte"
db 0 ;byte value
db 'a' ;ascii byte
dw 0 ;word value
dd 0 ;dword value
dd 0.0 ;real4 - single float
dq 0 ;qword value
dq 0.0 ;real8 - double float
dt 0 ;tbyte value
dt 0.0 ;real10 - extended float
@Japheth
QuoteThe other posts are just smalltalk.
thanks alot ! - lol
Quote from: japheth on July 14, 2009, 01:11:17 PM
Hi,
Quote from: jake072 on July 14, 2009, 01:02:29 PM
Weird, it compiles ok for me.
Read E'cube's post carefully! It's the one which contains the crucial hint. The other posts are just smalltalk.
Thanks for the clarification.
Now I'm even more lost... So if I use /LIBPATH, then I get unresolved symbols. So then I modify my WINDOWS.INC, and still get the errors...
The thing I REALLY don't understand is why in the world is nmake via MAKEFILE is NOT giving me errors. So I tried to modify my MAKEFILE, and I can now get it to error as does the make.bat.
So now I've just got to figure out why I get the:
hw.obj : error LNK2001: unresolved external symbol _GetStdHandle@4
hw.obj : error LNK2001: unresolved external symbol _WriteFile@20
hw.obj : error LNK2001: unresolved external symbol _ExitProcess@4
hw.exe : fatal error LNK1120: 3 unresolved externals
I'm REALLY LOST.
So I finally bite the bullet and include \masm32\include\masm32rt.inc... Voila, "Hello World!".
Thanks for the help, but I'm so confused about the unresolved external symbols.
I would rather make my own .INCs... Could someone help me with that? I copied the definitions FROM the relevant files (kernel32.inc)...
Thanks so much everyone, much appreciated!
:bg
Jake,
At your leisure have a look at masm32rt.inc and you will see why it works. The problem is there is smalltalk and there is smalltalk, the technique that works is the one that does the job.
Quote from: hutch-- on July 14, 2009, 02:13:00 PM
:bg
Jake,
At your leisure have a look at masm32rt.inc and you will see why it works. The problem is there is smalltalk and there is smalltalk, the technique that works is the one that does the job.
hutch,
After a little patience, I knew it must be something easy, right? Hehe, all I needed was includelib after my include windows.inc... Or I guess it would be better to includelib in my include, but either way, it now compiles just fine the way I want it to.
Quote from: hutch-- on July 14, 2009, 02:13:00 PM
The problem is there is smalltalk and there is smalltalk, the technique that works is the one that does the job.
LOL!
Thanks everyone again! You have saved me endless hours of frustration. I've literally spent 40+ hours on this Hello World, mind you I've learned a HECK of a lot in the process, I am so relieved that it's working the way I want.
One more really quick, probably stupid question... Is there a syntax guide for masm somewhere? Googling and Binging always want me to go look at the HLA stuff which I'm not really interested in, or with links to out of print books.
just "small-talk" here, Jake...
Quotebtw - don't let it frustrate you - we have all been there - lol
treat it as a simple learning experience and stick with it - you will get better every day
Quote from: jake072 on July 14, 2009, 02:23:37 PM
One more really quick, probably stupid question... Is there a syntax guide for masm somewhere? Googling and Binging always want me to go look at the HLA stuff which I'm not really interested in, or with links to out of print books.
You will find a good masm32 guide in the subdirectory 'help' in wherever you installed masm (for example, c:\masm32\help).
It goes over the syntax thats specific to masm, and also macros and various utilities provided in the package.
This site has an online MASM manual:-
massmind.org/techref/language/masm/toc.htm (http://massmind.org/techref/language/masm/toc.htm)
Thank you all so much for all the help.
I'm trying to learn assembly and create a beginners guide so that I can help teach others, and (most importantly =P) learn how to program in assembler myself.
I've posted my source code, and will have supporting documentation to follow...
It's available here (with special thanks to those who helped me =)) nymtec.com/assembly
P.S. If it is not proper to post links such as this, please let me know, and I will remove it immediately.