使用 WIN32 函数在 MASM 中输出 Hello World
内容
- 介绍
- 代码
- 汇编和运行
- 杂项
- 问题
1。简介
这本身不是一个问题(尽管底部有一个问题),而是一个供 StackOverflow 上的人们进行实验的 HelloWorld 应用程序。
当我第一次尝试在 MASM 中编程时,我试图找到一个使用 WIN32 API 调用(因此不链接到 C 库)的工作 HelloWorld 应用程序,但找不到(在 MASM 语法中)。所以现在我有了一些经验,我为其他想要学习汇编的人写了一篇。
2.代码
.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.组装和运行
我假设您已将 MASM32 安装在 C:\MASM32 目录中。
如果您没有安装 MASM 请去 http://masm32.com/install.htm 并按照说明进行操作。
如果 MASM32 安装在不同的 目录请更改 相应的指示。
通过单击桌面快捷方式打开 MASM32 编辑器 (QEditor),或者如果没有快捷方式,请转至 C:\MASM32\ 并双击 qeditor.exe
复制代码部分中的代码(仅具有灰色背景的文本)并将其粘贴到 MASM32 编辑器(QEditor)中并保存。
保存代码后,单击“项目”菜单,然后选择“控制台组装和链接”(不是组装和链接(请参阅其他))
转到“开始”并单击“运行”,然后键入 cmd 并按 Enter 键,此时会出现带有灰色文本的黑框
使用资源管理器导航到您在步骤 3 中保存代码的位置。现在应该有一个与源文件(步骤 3)同名的文件,但是一个 exe。将exe文件从资源管理器窗口拖放到cmd框(第4步黑框)
选择黑框并按 Enter 键,显示文本“Hello World!”应该会出现。
4.其他
为什么我必须单击 Console Assemble and Run 而不仅仅是项目菜单中的 Assemble and Run?
必须单击 Console Assemble and Run 的原因是因为有两种类型的应用程序,有 GUI,还有基于文本的控制台 (DOS) 应用程序。 Hello Would 应用程序是一种基于文本的应用程序,因此在组装时必须具有基于控制台的应用程序而不是 GUI 的设置。
5.问题
现在的问题是,这里有人看到此代码的任何问题、错误或一般问题或有任何建议吗?
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 \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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
程序没问题。它确实是 Win32 的“Hello World”版本。但是,请记住它是一个控制台程序。在 Win32 中,您将主要处理 Windows、对话框,很少处理控制台(如果您想专门处理控制台,那就是另一个故事了)。
如果您想学习 Win32 程序集,我强烈建议您查看 Iczelion 教程。
以下是他的教程的“Hello World”:
http://win32 assembly.online.fr/tut2.html
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
这个示例代码比较简单易懂
This sample code is simpler and easy to understand
StdOut 是一个控制台函数
您可以使用 MessageBox 函数...
StdOut is a console function
You can use MessageBox function...