使用WIN32函数在MASM中输出Hello World

Zim*_*m3r 5 assembly winapi masm masm32

内容

  1. 介绍
  2. 装配和运行

1.简介

这本身不是一个问题(虽然底部有一个)但是一个HelloWorld应用程序供StackOverflow上的人们试验.

当我第一次尝试在MASM中编程时,我试图找到一个使用WIN32 API调用的工作HelloWorld应用程序(因此不链接到C库)但找不到(在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 
Run Code Online (Sandbox Code Playgroud)

3.组装和运行

我假设您在C:\ MASM32目录中安装了MASM32.

  • 如果您没有安装MASM,请访问 http://masm32.com/install.htm 并按照说明操作.

  • 如果MASM32安装在不同的目录中,请相应地更改说明.

    1. 通过单击桌面快捷方式打开MASM32编辑器(QEditor),如果没有快捷方式,请转到C:\ MASM32 \并双击qeditor.exe

    2. 复制代码部分中的代码(仅具有灰色背景的文本)并将其粘贴到MASM32编辑器(QEditor)并保存.

    3. 保存代码后,单击Project菜单并选择Console Assemble and Link(NOT Assemble and Link(参见杂项))

    4. 转到START并单击"运行",然后键入cmd并按Enter键,将出现一个带有灰色文本的黑框

    5. 使用资源管理器导航到步骤3中保存代码的位置.现在应该有一个与源文件同名的文件(步骤3),但是应该是exe.将exe文件从资源管理器窗口拖放到cmd框中(黑匣子步骤4)

    6. 选择黑框,然后按ENTER键,文本"Hello World!" 应该出现.

4.杂项

为什么我必须单击Console Assemble and Run而不仅仅是在Project菜单中组装和运行?

您必须单击控制台组装和运行的原因是因为有两种类型的应用程序,有GUI,然后有文本基本控制台(DOS)应用程序.Hello Will应用程序是基于文本的应用程序,因此在组装时必须具有基于控制台的应用程序而不是GUI的设置.

有关更详细的说明,请参阅此链接中"备注"下的第三段.

5.问题

好了,现在问题是,这里有没有人看到任何问题,错误或此代码的一般问题或有任何建议

Mad*_*uja 5

程序没问题。它确实是 Win32 的“Hello World”版本。但是,请记住它是一个控制台程序。在 Win32 中,您将主要处理 Windows、对话框,很少处理控制台(Incase,您想专门处理控制台,那是另一回事)。

如果您想精益 Win32 程序集,我强烈建议您查看 Iczelion 教程。

这是“Hello World”开始他的教程:

http://win32assembly.online.fr/tut2.html