9

コンテンツ

  1. はじめに
  2. コード
  3. 組み立てと実行
  4. その他
  5. 質問

1. はじめに

これはそれ自体は質問ではありません (一番下に質問があります) が、StackOverflow で実験するための HelloWorld アプリです。

最初に MASM でプログラミングを試みたとき、WIN32 API 呼び出しを使用する (C ライブラリにリンクしていない) 動作する HelloWorld アプリケーションを見つけようとしましたが、(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. [スタート] に移動して [実行] をクリックし、cmd と入力して Enter キーを押すと、灰色のテキストが表示された黒いボックスが表示されます。

    5. エクスプローラーを使用して、手順 3 でコードを保存した場所に移動します。ソース ファイル (手順 3) と同じ名前のファイルが存在するはずですが、exe ファイルです。エクスプローラ ウィンドウからコマンド ボックスに exe ファイルをドラッグ アンド ドロップします (手順 4 のブラック ボックス)。

    6. 黒いボックスを選択して Enter キーを押すと、「Hello World!」というテキストが表示されます。表示されるはずです。

4. その他

[プロジェクト] メニューの [アセンブルして実行] だけでなく、[コンソールのアセンブルして実行] をクリックする必要があるのはなぜですか?

[Console Assemble and Run] をクリックする必要があるのは、GUI とテキスト ベース コンソール (DOS) アプリケーションの 2 種類のアプリケーションがあるためです。Hello would アプリケーションはテキスト ベースのアプリケーションであるため、組み立てる際には、GUI ではなくコンソール ベースのアプリケーションと同じ設定にする必要があります。

詳細な説明については、このリンクの備考の 3 番目の段落を参照してください。

5. 質問

さて、質問ですが、このコードの問題、エラー、または一般的な問題を見た人はいますか、何か提案がありますか

4

3 に答える 3

5

プログラムは問題ありません。これはまさに、Win32 の「Hello World」バージョンです。ただし、これはコンソール プログラムであることを忘れないでください。Win32 では、主に Windows とダイアログ ボックスを扱い、コンソールを扱うことはほとんどありません (場合によっては、特にコンソールを扱いたい場合、それは別の話です)。

Win32 アセンブリについて学習したい場合は、Iczelion チュートリアルを参照することを強くお勧めします。

彼のチュートリアルを開始する「Hello World」は次のとおりです。

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

于 2010-12-31T05:57:08.563 に答える
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
于 2011-01-02T18:10:33.473 に答える