gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:17:53 26 4
gpt4 key购买 nike

目录

  • 简介
  • 代码
  • 组装运行
  • 杂项
  • 问题

  • 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步黑框)
  • 选择黑框并按回车键,文本“Hello World!”应该出现。

  • 4. 杂项

    为什么我必须单击控制台组装和运行,而不仅仅是在项目菜单中的组装和运行?

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

    this link 中备注下的第三段以获得更详细的解释。

    5.提问

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

    最佳答案

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

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

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

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

    关于winapi - 使用 WIN32 函数在 MASM 中输出 Hello World,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4568306/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com