簡體   English   中英

使用WIN32函數在MASM中輸出Hello World

[英]Outputting Hello World in MASM using WIN32 Functions

內容

  1. 介紹
  2. 組裝和運行

1.簡介

這本身不是問題(盡管底部是一個問題),而是一個HelloWorld應用程序,供StackOverflow上的人們嘗試。

當我第一次嘗試在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安裝在其他目錄中,請相應地更改說明。

    1. 通過單擊桌面快捷方式打開MASM32編輯器(QEditor),或者如果沒有快捷方式,請轉到C:\\ MASM32 \\,然后雙擊qeditor.exe

    2. 復制代碼部分中的代碼(僅復制具有灰色背景的文本),然后將其粘貼到MASM32編輯器(QEditor)中並保存。

    3. 保存代碼后,單擊“項目”菜單,然后選擇“控制台組裝和鏈接”( 不是“組裝和鏈接”(請參閱​​其他))

    4. 轉到“開始”並單擊“運行”,然后鍵入cmd並按Enter,將出現一個帶有灰色文本的黑框。

    5. 使用資源管理器導航到第3步中保存代碼的位置。現在應該有一個與源文件同名的文件(第3步),但應該是exe。 將exe文件從“資源管理器”窗口拖放到cmd框中(步驟4,黑框)

    6. 選擇黑框,然后按Enter,輸入文本“ Hello World!”。 應該出現。

4.雜項

為什么我必須單擊“控制台組裝並運行”,而不僅僅是在“項目”菜單中單擊“組裝並運行”?

您必須單擊“控制台組裝並運行”的原因是因為有兩種類型的應用程序,有GUI,然后是文本控制台(DOS)應用程序。 Hello Would Application是基於文本的應用程序,因此在組裝時必須具有基於控制台的應用程序而非GUI的設置。

有關更多詳細說明,請參見此鏈接中“備注”下的第三段。

5.問題

現在的問題是,這里的任何人是否看到此代碼有任何問題,錯誤或一般性問題,或有任何建議

該程序很好。 它確實是Win32的“ Hello World”版本。 但是,請記住其控制台程序。 在Win32中,您將主要使用Windows,對話框和控制台進行處理(以防萬一,您想專門處理控制台,那就是另一回事了)。

如果您想精簡Win32 Assembly,我強烈建議您查看Iczelion教程。

這是從“ Hello World”開始的教程:

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

此示例代碼更簡單易懂

.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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM