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

时间:2010-12-31 05:33:58

标签: winapi assembly masm masm32

目录

  1. 简介
  2. 装配和运行
  3. 杂项
  4. 问题
  5. 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 
    

    第3。装配和运行

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

    • 如果您没有安装MASM 请去 http://masm32.com/install.htm 并按照说明进行操作。

    • 如果MASM32安装在另外一个 目录请改变 相应的说明。

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

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

      3. 保存代码后,单击“项目”菜单,然后选择“控制台组合和链接”( 组装和链接(请参阅其他))

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

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

      6. 选择黑匣子并点击ENTER,文字“Hello World!”应该出现。

    4。其它

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

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

    有关更详细的说明,请参阅this link中备注下的第三段。

    5。问题

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

3 个答案:

答案 0 :(得分:3)

程序没问题。它确实是Win32的“Hello World”版本。但是,请记住它的控制台程序。在Win32中,你将主要处理Windows,对话框,而不是使用Console(Incase,你想专门处理控制台,这是另一个故事)。

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

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

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

答案 1 :(得分:1)

此示例代码更简单易懂

.386
.model flat, stdcall
option casemap: none

include windows.inc
include user32.inc
include kernel32.inc
includelib user32.lib
includelib kernel32.lib

.data
    szCaption   db  'Hello', 0
    szText      db  'Hello, World!', 0

.code
    start:
            invoke MessageBox, NULL, offset szText, offset szCaption, MB_OK
            invoke ExitProcess, NULL        
    end start

答案 2 :(得分:0)

StdOut是一个控制台功能

您可以使用MessageBox功能...

.model small,pascal,nearstack
.386
?WINPROLOGUE=1
include win.inc
includelib libw.lib
extern __astart:proc

.data
text sbyte "Hello f*** World!",0
title sbyte "Win",0

.code
WinMain    PROC, hInstance:HANDLE, hPrevInstance:HANDLE, lpszCmdLine:LPSTR, nCmdShow,WORD
  LOCAL msg:MSG

 invoke MessageBox, NULL, addr text, addr title, 0
 invoke PostQuitMessage,0

 .while TRUE
     invoke GetMessage,addr msg,NULL,0,0
     .break .if (ax == 0)
     invoke TranslateMessage,addr msg
     invoke DispatchMessage,addr msg
 .endw
WinMain    ENDP
END        __astart