Contents
1. Intro
This isn't a question per se (though there is one at the bottom) but a HelloWorld app for people on StackOverflow to experiment with.
When I was first trying programing in MASM I tried to find a working HelloWorld application that used the WIN32 API calls (so not linking to C libraries) but couldn't find one (in MASM Syntax). So now that I have some experience I have written one for others wanting to learn assembly to fiddle with.
2. Code
.386 ; 386 Processor Instruction Set
.model flat,stdcall ; Flat memory model and stdcall method
option casemap:none ; Case Sensitive
;Libaries and Include files used in this project
; Windows.inc defines alias (such as NULL and STD_OUTPUT_HANDLE in this code
include \masm32\include\windows.inc
; Functions that we use (GetStdHandle, WriteConsole, and ExitProcess)
; Listing of all available functions in kernel32.lib
include \masm32\include\kernel32.inc
; Actuall byte code available of the functions
includelib \masm32\lib\kernel32.lib
.data
; Labels that with the allocated data (in this case Hello World!...) that are aliases to memory.
output db "Hello World!", 0ah, 0h; This String Hello World! and then a the newline character \n (0ah) and then the null character 0h
.code
start:
; --------------------------------------------------------------------------------------------------------------------------------------
; Retrieves that handle to the output console
;
; ====Arguments===
;
; STD_OUTPUT_HANDLE - alias for -11 and indicates that we want the handle to
; write to console output
;
invoke GetStdHandle, STD_OUTPUT_HANDLE
; --------------------------------------------------------------------------------------------------------------------------------------
; --------------------------------------------------------------------------------------------------------------------------------------
; Writes the text in output (.data section) to the console
;
; ====Arguments===
;
; eax - the handle to the console buffer
;
; addr output - pass by reference the text of output (Hello World!)
;
; sizeof output - the size of the string so that the WriteConsole knows when to
; stop (doesn't support NULL terminated strings I guess);
;
; ebx - secondary "return" value that contains the number of bytes written (eax
; is used for an error code)
;
; NULL - this is reserved and MSDN says just to pass NULL
;
; MSDN Link: http://msdn.microsoft.com/en-us/library/ms687401(v=VS.85).aspx
;
invoke WriteConsole, eax, addr output, sizeof output, ebx, NULL
; --------------------------------------------------------------------------------------------------------------------------------------
; --------------------------------------------------------------------------------------------------------------------------------------
; Exits the program with return code 0 (default one that usually is used to
; indicate that the program did not error
;
; ====Arguments===
;
; 0 - the exit code
;
; MSDN Link: http://msdn.microsoft.com/en-us/library/ms682658(VS.85).aspx
;
invoke ExitProcess, 0
; --------------------------------------------------------------------------------------------------------------------------------------
end start
3. Assembling and Running
I assume you have MASM32 installed in your C:\MASM32 directory.
If you do not have MASM installed please go to http://masm32.com/install.htm and follow the instructions.
If MASM32 is installed in a different directory please change the instructions accordingly.
Open up the MASM32 Editor (QEditor) by either clicking on the Desktop Shortcut or if there is no shortcut go to C:\MASM32\ and double click qeditor.exe
Copy the code in the code section (only the text that has a gray background) and paste it into the MASM32 Editor (QEditor) and save it.
After saving the code click the Project menu and select Console Assemble and Link (NOT Assemble and Link (see Miscellaneous))
Go to START and click Run, then type cmd and hit ENTER a black box with gray text should appear
Navigate, using Explorer, to where you saved the code in step 3. There should now be a file with the same name as your source file (step 3) but be an exe. Drag and drop the exe file from the Explorer window to the cmd box (step 4 the black box)
Select the black box and hit ENTER, the text "Hello World!" should appear.
4. Miscellaneous
Why do I have to click Console Assemble and Run and not just Assemble and Run in the Project Menu?
The reason you have to click Console Assemble and Run is because there are two types of applications, there are GUIs and then there are text base console (DOS) applications. The Hello Would Application is a text based one and so when assembled must be have the settings a console based app would and not a GUI.
See the third paragraph under Remarks in this link for a more detailed explanation.
5. Question
Ok now the question, does anyone here see any problems, errors, or general issues with this code or have any suggestions
Go to Start, Programs, MS-DOS Prompt (or Command Window). This will open a second DOS window. Change to your . ASM directory (for example, C:\COP3402) and issue the commands to run MASM.
The program is fine. It is indeed "Hello World" version of Win32. However, remember its a console program. In Win32, you will be mostly dealing with Windows, Dialog Boxes and very less with Console (Incase, you want to deal specifically with console, thats another story).
If you want to lean Win32 Assembly, I strongly suggest you to look at Iczelion Tutorials.
Here is the "Hello World" to start with his tutorials:
http://win32assembly.online.fr/tut2.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With