コンテンツ
- はじめに
- コード
- 組み立てと実行
- その他
- 質問
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 が別のディレクトリにインストールされている場合は、それに応じて手順を変更してください。
デスクトップ ショートカットをクリックして MASM32 エディター (QEditor) を開くか、ショートカットがない場合は C:\MASM32\ に移動し、qeditor.exe をダブルクリックします。
コード セクションのコード (背景が灰色のテキストのみ) をコピーし、MASM32 エディター (QEditor) に貼り付けて保存します。
コードを保存したら、[プロジェクト] メニューをクリックし、[コンソールのアセンブルとリンク] を選択します ([アセンブルとリンク] ではありません(「その他」を参照)) 。
[スタート] に移動して [実行] をクリックし、cmd と入力して Enter キーを押すと、灰色のテキストが表示された黒いボックスが表示されます。
エクスプローラーを使用して、手順 3 でコードを保存した場所に移動します。ソース ファイル (手順 3) と同じ名前のファイルが存在するはずですが、exe ファイルです。エクスプローラ ウィンドウからコマンド ボックスに exe ファイルをドラッグ アンド ドロップします (手順 4 のブラック ボックス)。
黒いボックスを選択して Enter キーを押すと、「Hello World!」というテキストが表示されます。表示されるはずです。
4. その他
[プロジェクト] メニューの [アセンブルして実行] だけでなく、[コンソールのアセンブルして実行] をクリックする必要があるのはなぜですか?
[Console Assemble and Run] をクリックする必要があるのは、GUI とテキスト ベース コンソール (DOS) アプリケーションの 2 種類のアプリケーションがあるためです。Hello would アプリケーションはテキスト ベースのアプリケーションであるため、組み立てる際には、GUI ではなくコンソール ベースのアプリケーションと同じ設定にする必要があります。
詳細な説明については、このリンクの備考の 3 番目の段落を参照してください。
5. 質問
さて、質問ですが、このコードの問題、エラー、または一般的な問題を見た人はいますか、何か提案がありますか