现在的位置: 首页 > 综合 > 正文

(译)win32asm教程-11

2013年07月22日 ⁄ 综合 ⁄ 共 3422字 ⁄ 字号 评论关闭
 

12.0 第一个程序

是创建你的第一个程序的时候了。本章中的指导将这样组织:

12.1第一步

如果万事具备,你应该在你的masm同一个区上有一个win32(或win32asm)目录。为每个工程,你应该创建一个子目录。

win32目录中创建一个名为“Firstprogram“的子目录。创建一个新的文本文件并重命名为“first.asm”。

12.2第二步

first.asm中输入一下代码:

.486
.model flat, stdcall

option casemap:none

includelib /masm32/lib/kernel32.lib
includelib /masm32/lib/user32.lib

include /masm32/include/kernel32.inc
include /masm32/include/user32.inc
include /masm32/include/windows.inc

因为现在,我们仅需要kernel32和user32两个dll。

12.3第三步

我们将要创建著名的“Hello World”程序。要显示“hello World”字符串,我们要用消息对话框。消息对话框由MessageBox函数创建。你可以在《win32 程序员参考》(看第二章)中查找这个函数。这是书上说的:

MessageBox函数创建,显示并操作消息对话框。消息对话框包含应用程序定义的消息和标题,加上任何预定义的图标与按钮的组合。

int MessageBox(
HWND hWnd, // handle of owner window
LPCTSTR lpText, // address of text in message box
LPCTSTR lpCaption, // address of title of message box
UINT uType // style of message box
);

Parameters

hWnd

Identifies the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window.

lpText

Points to a null-terminated string containing the message to be displayed.

lpCaption

Points to a null-terminated string used for the dialog box title. If this parameter is NULL, the default title Error is used.

uType

Specifies a set of bit flags that determine the contents and behavior of the dialog box. This parameter can be a combination of flags from the following groups of flags.

[--SNIP--]

在这段文字后有所有常数和标志的列表(他们定义在windows.inc中)。因为它太长了,我没有在这里列出来。通过查看参考,你就知道MessageBox函数要4个参数:父窗口(owner),指向消息串的指针,指向标题串的指针和消息框的类型。

HWnd可以是Null。因为我们的程序没有窗口。

LpText必须是指向我们文本的指针。这仅仅意为参数是文本所在内存地址的offset

LpCaption 是标题串的offset

UType 是参考中解释的像MB_OKMB_OKCANCELMB_ICONERROR等值的组合。

让我们先定义两个用于MessageBox的字符串:

first.asm中加入:

.data

MsgText db "Hello world!",0
MsgTitle db "This is a messagebox",0

.data 指示data部分的开始。用db,字节直接被插入,而且字符串又只是字节的集合,data部分会在包含上面的字符串,附加上结尾的0。MsgText装有第一个字符串的offset。MsgTitle有第二个字符串的offset。现在我们可以使用函数:

invoke MessageBox, NULL, offset MsgText, offset MsgTitle, Null

但因为用的是invoke,你可以使用(更安全)ADDR代替offset:

invoke MessageBox, Null, ADDR MsgText, ADDR MsgTitle, Null

我们还没有看最后一个参数,但这不会有什么问题。因为MB_OK(有一个ok按钮的消息对话框的样式)等于0(NULL)。但你也可以使用其他的任何样式。Utype(第4个参数)的定义是:

指定一系列决定对话框内容与行为的位标志。这个参数可以是下面标志组中标志的组合。

现在以我们要一个有OK按钮与“information”图标的简单消息对话框为例。MB_OK是OK按钮的样式,MB_ICONINFORMATION是information图标的样式。样式是用“or”操作符联合的。这不是or伪代码。Masm会在汇编前处理or操作。不用or,你可以用+号(加号)代替,但有时对层叠样式有问题(一个样式包含其他一些样式)。但在本例中你也可以用+号。

.code

start:

invoke MessageBox, NULL, ADDR MsgText, ADDR MsgTitle, MB_OK + MB_ICONINFORMATION

end start

把以上的代码加入到你的first.asm文件中。

我们还加入了一个start标签。如果你现在汇编你的程序并运行它,它将显示一个消息对话框但很有可能在你点OK之后就崩溃了。这是因为程序没有结束,而处理器开始执行MessageBox代码后的任何东西。Windows中程序是用ExitProcess函数结束的:

VOID ExitProcess(

UINT uExitCode //对于所有线程的退出代码
);

我们可以把0用作退出码。

把你的代码改成这样:

.code

start:

invoke MessageBox, NULL, ADDR MsgText, ADDR MsgTitle, MB_OK + MB_ICONINFORMATION
invoke ExitProcess, NULL

end start

12.4第4步

因此我们最终的程序是:

.486
.model flat, stdcall

option casemap:none

includelib /masm32/lib/kernel32.lib
includelib /masm32/lib/user32.lib

include /masm32/include/kernel32.inc
include /masm32/include/user32.inc
include /masm32/include/windows.inc

.data
MsgText db "Hello world!",0
MsgTitle db "This is a messagebox",0

.code

start:
invoke MessageBox, NULL, ADDR MsgText, ADDR MsgTitle, MB_OK or MB_ICONINFORMATION
invoke ExitProcess, NULL

end start

12.5第5步

现在我们将从源代码产生可执行文件。

用一下内容新建一个文本文件并命名为make.bat:

@echo off
ml /c /coff first.asm
link /subsystem:windows first.obj
pause>nul

解释:

ml /c /coff first.asm

Ml是宏汇编器(masm)。Masm将从程序创建原始代码。参数的意思是:
/c =汇编不链接(因为我们用link.exe来做这项工作)
/coff = 产生COFF格式的object(对象)文件,这是Windows可执行文件的标准格式。
first.asm = a汇编first.asm文件

link /subsystem:windows first.obj

链接器把object文件和所有导入的dll与库链接起来:
/subsystem:windows =
创建Windows的可执行文件。
first.obj =
链接 first.obj

如果你把所有的事情都正确的完成了,并运行批处理文件。将产生first.exe。运行它,看看有什么结果。

抱歉!评论已关闭.