Contents
- Intro
- Code
- Assembling and Running
- Miscellaneous
- Question
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 masm32includewindows.inc
; Functions that we use (GetStdHandle, WriteConsole, and ExitProcess)
; Listing of all available functions in kernel32.lib
include masm32includekernel32.inc
; Actuall byte code available of the functions
includelib masm32libkernel32.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
(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.
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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…