【发布时间】:2011-06-01 21:12:48
【问题描述】:
内容
- 简介
- 代码
- 组装和运行
- 杂项
- 问题
1.简介
这本身不是一个问题(尽管底部有一个),而是一个供 StackOverflow 上的人们试验的 HelloWorld 应用程序。
当我第一次尝试在 MASM 中编程时,我试图找到一个使用 WIN32 API 调用(因此不链接到 C 库)但找不到一个(在 MASM 语法中)的工作 HelloWorld 应用程序。所以现在我有了一些经验,我写了一篇给其他想学习汇编的人来摆弄。
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.组装和运行
我假设您在 C:\MASM32 目录中安装了 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 的原因是因为有两种类型的应用程序,一种是 GUI,另一种是文本基本控制台 (DOS) 应用程序。 Hello Will 应用程序是基于文本的应用程序,因此在组装时必须具有基于控制台的应用程序而不是 GUI 的设置。
更详细的解释见this link备注下的第三段。
5.问题
好的,现在的问题是,这里是否有人看到此代码的任何问题、错误或一般问题或有任何建议
【问题讨论】:
标签: winapi assembly masm masm32