win32汇编程序运行方法 --MASM32、makefile、makeit


近刚学习win32汇编程序,用的工具是MASM32,MASM32安装包不会对注册表进行写操作,安装后,它会在桌面创建qeditor.exe的快捷方式,qeditor是MASM32的IDE环境,我们要做的事就是对这个IDE进行设置。如何设置咱们之后再讲(见后文附录),假设环境设置好,下面选择以下方式之一来执行程序。


先把程序涉及文件和图片贴出来

a.)  Test.ASM  源文件

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Test.asm
; 编程环境测试代码
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 使用 nmake 或下列命令进行编译和链接:
; ml /c /coff Test.asm
; rc rsrc.rc
; Link /subsystem:windows Test.obj rsrc.res
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.486
.model flat,stdcall
option casemap:none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include		F:\masm32\bin\hunter.inc  ;hunter.inc文件是设置后的文件,详见下面的附录
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 .data     
        DatePicker db "The date you picked is:",0
        DateString db "aaa",0

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
start:
		invoke MessageBox,NULL,ADDR DateString,ADDR DatePicker,MB_OK
		invoke	ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end	start

b) rsrc.rc  -资源文件

1		icon	"Main.ico"

c)Main.ico  -图标文件


方式一 qEditor版:

开始

准备好Test.asm、rsrc.rc、Main.ICO三个文件,然后运行qeditor.exe,Open Test.asm文件,之后--
1)若直接单击project菜单的Console Build All,得到的是控制台的EXE文件,应用程序不会有ICO图标。
2)若直接单击project菜单的Build All,得到的也是控制台的EXE文件,应用程序不会有ICO图标。
3)若单击project菜单的Compile Resource File,则先得到rsrc.res和rsrc.obj,之后再运行Console Build All或Build All,则可得到带有ICO图标的应用程序。

        但是,莫名其妙的是,若我通过“把Test.asm拖到MASM32 Editor.exe运行程序上”这个方式来打开文件的话,单击project菜单的Compile Resource File,则报错“RC:fatalerror RC1110:could not open rsrc.rc”。显然单击project菜单下的Build All等都将报错。

         ——看来暂时只能先运行Editor.exe,然后通过菜单来打开ASM文件.

        结束


方式二  makefile版:

开始

先准备以下工具---
ml tool 和 ml.err(用于编译.asm文件:ml Test.asm)
rc tool 和 rcdll.dll(生成.res文件:rc rsrc.rc)
Cvtres tool(生成 .obj文件:cvtres rsrc.res)
link tool 和 mspdb50.dll(用于连接.obj文件:link Test.obj rsrc.obj)
nmake tool

然后准备好Test.asm、rsrc.rc、Main.ICO三个文件

再然后写好Makefile文件,最后直接运行nmake.exe即可
(注:所有文件放在同一个文件夹)

        MAKEFILE文件:

#makefile file
NAME = Test
OBJS = $(NAME).obj rsrc.obj

LINK_FLAG = /subsystem:windows
ML_FLAG = /c /coff
RC_FLAG = /v
CVTRES_FLAG = /machine:ix86

$(NAME).exe: $(OBJS)
	Link $(LINK_FLAG) $(OBJS) 

.asm.obj:
	ml $(ML_FLAG) {1}lt;
rsrc.obj:
	rc $(RC_FLAG) rsrc.rc
	Cvtres	$(CVTRES_FLAG) rsrc.res

clean:
	del *.obj

  结束


方式三 makeit版:

开始

准备好Test.asm、rsrc.rc、Main.ICO三个文件。makeit.bat是masm32支持的批处理文件,编写好相应程序,它会自动用masm32提供的库文件和工具(如rc.exe/ml.exe/cvtres.exe...)来编译和连接,最后生成exe文件。

         makeit.bat file

;makeit.bat file
@echo off
: -------------------------------
: if resources exist, build them
: -------------------------------
if not exist rsrc.rc goto over1
\MASM32\BIN\Rc.exe /v rsrc.rc
\MASM32\BIN\Cvtres.exe /machine:ix86 rsrc.res
:over1

if exist %1.obj del Test.obj
if exist %1.exe del Test.exe

: -----------------------------------------
: assemble Calendar.asm into an OBJ file
: -----------------------------------------
\MASM32\BIN\Ml.exe /c /coff /Fl Test.asm
if errorlevel 1 goto errasm

if not exist rsrc.obj goto nores

: --------------------------------------------------
: link the main OBJ file with the resource OBJ file
: --------------------------------------------------
\MASM32\BIN\Link.exe /SUBSYSTEM:WINDOWS Test.obj rsrc.obj
if errorlevel 1 goto errlink

dir Test.*

goto TheEnd

:errlink
: ----------------------------------------------------
: display message if there is an error during linking
: ----------------------------------------------------
echo.
echo There has been an error while linking this project.
echo.
goto TheEnd

:errasm
: -----------------------------------------------------
: display message if there is an error during assembly
: -----------------------------------------------------
echo.
echo There has been an error while assembling this project.
echo.
goto TheEnd

:TheEnd

pause


结束


附录:win7下的汇编工具MASM32的详细设置(转载)

我的操作系统:win7 旗舰版

首先,我建议把MASM32安装在C盘根目录下。MASM32安装包不会对注册表进行写操作,安装后,它会在桌面创建qeditor.exe的快捷方式,qeditor是MASM32的IDE环境,我们要做的事就是对这个IDE进行设置。

右击快捷方式,选择以管理员方式运行(打钩),确定。

选择EDIT->SETTINGS->EDIT MENUS ,进去以后开始进行设置。把所有内容删除,将以下内容粘贴进去然后SAVE。


[&Project]
Compile &Resource File,C:\MASM32\BIN\Bres.bat {b}
&Assemble ASM file,C:\MASM32\BIN\Assmbl.bat {b}
-
&Link OBJ File,C:\MASM32\BIN\Lnk.bat {b}
As&semble && Link,C:\MASM32\BIN\Build.bat {b}
&Build All,C:\MASM32\BIN\Bldall.bat {b}
-
Console Link &OBJ File,C:\MASM32\BIN\Lnkc.bat {b}
&Console Assemble && Link,C:\MASM32\BIN\Buildc.bat {b}
Console Build &All,C:\MASM32\BIN\Bldallc.bat {b}
-
&Run Program,"{b}.exe"

[&Tools]
; -----------------------------
; put you resource editor here
; -----------------------------
&Microsoft Image Editor,C:\MASM32\BIN\Imagedit.exe
Microsoft &Zoomin Utility,C:\masm32\bin\zoomin.exe
-
&TopGun Wordwrap Editor,{e}topgun.exe
-
&Procedure Browser,C:\MASM32\qetb.exe \MASM32\procs
Browse MASM32 &Lib,C:\masm32\qetb.exe \masm32\m32lib
-
Map App &Procedures,{e}procmap.exe {e}qeditor.exe
-
&Hex to Mnemonic,C:\MASM32\Mnemonix.exe
&MASM32 Folder,C:\MASM32\Shellex.exe \masm32
-
Load Binary File As Hex,{e}\plugins\bin2hex.dll
Save Hex File As Binary,{e}\plugins\hex2bin.dll
-
Righ Trim Block,{e}\plugins\rtrim.dll
-
&Dis-assemble EXE file,C:\MASM32\BIN\dasm.bat {b}
Dis-&assemble DLL,C:\masm32\BIN\Dasmd.bat {b}
Format DumpPE Output,{e}\plugins\dumpcu.dll
-
&Indent .IF Block,{e}\plugins\Indentb.dll
Swap 32 bit registers,{e}\plugins\regswap.dll
-
Block &C++ Comment,{e}\plugins\cblockc.dll
Block &ASM Comment,{e}\plugins\ablockc.dll

[&Code]
Create New GUI Application,{e}\prostart.exe
Create New Console Application,{e}\script\console.qse
Create New &DLL,{e}\script\dll.qse
Create New Library Module,{e}\script\libmod.qse
Create New QE Plugin,{e}\script\plugin.qse
-
Create New Control Subclass,C:\MASM32\subclass.exe
Create New Object Module From File,{e}\fda2.exe
-
Create New Jump Table,{e}\tproc.exe
Create New Word List Jump Table,{e}\jtmake.exe
Create New Character Table,{e}\maketbl.exe
-
&Binary to DB Convertor,C:\masm32\Bintodb.exe
&RC Menu to .IF asm code,C:\masm32\Mnutoasm.exe
-
Small &Algo Testbed,C:\masm32\Testbed.qsc
Large Algo Testbed,C:\masm32\bigtstbd.qsc
-
Dialog &Template,C:\MASM32\dlgproc.qsc
Add In &Dialog,C:\MASM32\dlgtmplt.qsc
-
Create bld&lib.bat,C:\masm32\Libbat.qsc
-
Fast Insert Dialog Template,{e}\text\blankdlg.txt
Fast Insert Console Template,{e}\text\contemp.txt

[Scr&ipt]
Convert Text to Script,{e}\plugins\txt2qse.dll
Convert Binary to Script,{e}\plugins\bin2qse.dll
-
Debug MsgBox,{e}\text\dbmsgbox.txt
MessageBox,{e}\text\msgbox.txt
Last Error MessageBox,{e}\text\lasterr.txt
Debug Print,{e}\text\dbprint.txt
-
&Create EXE makeit.bat,C:\masm32\Bldmakit.qsc
&Create CONSOLE makeit.bat,C:\masm32\makecon.qsc
Create Sample &RC file,{e}\text\samplerc.txt
-
Insert switch$ Block,{e}\text\switch$.txt
Insert Normal switch Block,{e}\text\switch.txt
-
Insert Prologue Code,{e}\text\prolog.txt
Insert Epilogue Code,{e}\text\epilog.txt
-
Push 3 regs,{e}\text\push3.txt
Pop 3 regs,{e}\text\pop3.txt
-
3 nops,{e}\text\nops3.txt
-
插入注释分割*******************,{e}\text\comment0.txt

[help]
Quick Editor Help,{e}\help\qeditor.chm
-
&MASM32 Help,{e}\HELP\masm32.chm
High Level Macro Help,{e}\help\hlhelp.chm
MASM32 &Library Reference,{e}\HELP\Masmlib.chm
MASM32 Dialog Help,{e}\HELP\imdialog.chm
&FPU Lib Help,{e}\HELP\Fpuhelp.chm
Date Time Reference,{e}\HELP\datetime.chm
-
&Opcodes Help,{e}\HELP\Opcodes.chm
&ASM Intro Help,{e}\HELP\Asmintro.chm
-
&VKdebug Help,{e}\help\VKDebug.chm

然后找到C盘根目录下MASM32里面的TEXT目录,删掉里面的comment0.txt到comment8.txt,新建一个文本文件,里面的内容如下:


;********************************************************************************

保存为comment0.txt。

现在对bin目录里面的bat文件动手术哈!

(1)assmbl.bat(这个批处理对应qeditor.exe里面的Assemble ASM file选项)

把内容改为:

@echo off

title hunter'assembly batch command
color f0


if exist "%1.obj" del "%1.obj"

c:\masm32\bin\ml /c /coff "%1.asm" > c:\masm32\bin\asmbl.txt

if errorlevel 0 dir "%1.*" >> c:\masm32\bin\asmbl.txt

start c:\masm32\tview.exe c:\masm32\bin\asmbl.txt

(2)bldall.bat(这个批处理文件对应qeditor.exe的Build All选项)

@echo off


if not exist rsrc.rc goto over1
c:\masm32\bin\rc /v rsrc.rc
c:\masm32\bin\cvtres /machine:ix86 rsrc.res
:over1

if exist "%1.obj" del "%1.obj"
if exist "%1.exe" del "%1.exe"

c:\masm32\bin\ml /c /coff "%1.asm"
if errorlevel 1 goto errasm

if not exist rsrc.obj goto nores

c:\masm32\bin\Link /SUBSYSTEM:WINDOWS /OPT:NOREF "%1.obj" rsrc.obj
if errorlevel 1 goto errlink

dir "%1.*"
goto TheEnd

:nores
c:\masm32\bin\Link /SUBSYSTEM:WINDOWS /OPT:NOREF "%1.obj"
if errorlevel 1 goto errlink
dir "%1.*"
goto TheEnd

:errlink
echo _
echo Link error
goto TheEnd

:errasm
echo _
echo Assembly Error
goto TheEnd

:TheEnd

pause
(3)bldallc.bat(bldall.bat的console版本,用于创建命令行窗口)

@echo off


if not exist rsrc.rc goto over1
c:\masm32\bin\rc /v rsrc.rc
c:\masm32\bin\cvtres /machine:ix86 rsrc.res
:over1

if exist "%1.obj" del "%1.obj"
if exist "%1.exe" del "%1.exe"

c:\masm32\bin\ml /c /coff "%1.asm"
if errorlevel 1 goto errasm

if not exist rsrc.obj goto nores

c:\masm32\bin\Link /SUBSYSTEM:CONSOLE /OPT:NOREF "%1.obj" rsrc.obj
if errorlevel 1 goto errlink

dir "%1.*"
goto TheEnd

:nores
c:\masm32\bin\Link /SUBSYSTEM:CONSOLE /OPT:NOREF "%1.obj"
if errorlevel 1 goto errlink
dir "%1.*"
goto TheEnd

:errlink
echo _
echo Link error
goto TheEnd

:errasm
echo _
echo Assembly Error
goto TheEnd

:TheEnd

pause
(4)bres.bat(对应qeditor.exe的Compile Resource File选项,用于编译资源文件)

@echo off

title hunter'assembly batch command
color f0

c:\masm32\bin\rc /v rsrc.rc
c:\masm32\bin\cvtres /machine:ix86 rsrc.res

pause
(5)build.bat(对应qeditor.exe的Assemble & Link选项,编译链接)

@echo off

title hunter'assembly batch command
color f0

if exist "%1.obj" del "%1.obj"
if exist "%1.exe" del "%1.exe"

@echo %1

c:\masm32\bin\ml /c /coff "%1.asm"
if errorlevel 1 goto errasm

if not exist rsrc.obj goto nores

c:\masm32\bin\Link /SUBSYSTEM:WINDOWS /OPT:NOREF "%1.obj" rsrc.obj
if errorlevel 1 goto errlink

dir "%1.*"
goto TheEnd

:nores
c:\masm32\bin\Link /SUBSYSTEM:WINDOWS /OPT:NOREF "%1.obj"
if errorlevel 1 goto errlink
dir "%1.*"
goto TheEnd

:errlink
echo _
echo Link error
goto TheEnd

:errasm
echo _
echo Assembly Error
goto TheEnd

:TheEnd

pause
(6)buildc.bat(build.bat的console版本,用于创建命令行窗口)

@echo off

title hunter'assembly batch command
color f0


if exist "%1.obj" del "%1.obj"
if exist "%1.exe" del "%1.exe"

\masm32\bin\ml /c /coff "%1.asm"
if errorlevel 1 goto errasm

if not exist rsrc.obj goto nores

\masm32\bin\Link /SUBSYSTEM:CONSOLE /OPT:NOREF "%1.obj" rsrc.obj
if errorlevel 1 goto errlink

dir "%1.*"
goto TheEnd

:nores
\masm32\bin\Link /SUBSYSTEM:CONSOLE /OPT:NOREF "%1.obj"
if errorlevel 1 goto errlink
dir "%1.*"
goto TheEnd

:errlink
echo _
echo Link error
goto TheEnd

:errasm
echo _
echo Assembly Error
goto TheEnd

:TheEnd

pause
(7)lnk.bat(链接,对应于qeditor.exe的Link OBJ File 选项)

@echo off


if exist "%1.exe" del "%1.exe"

if not exist rsrc.obj goto nores

c:\masm32\bin\Link /SUBSYSTEM:WINDOWS /OPT:NOREF "%1.obj" rsrc.obj
dir "%1.*"
goto TheEnd

:nores
c:\masm32\bin\Link /SUBSYSTEM:WINDOWS /OPT:NOREF "%1.obj"
dir "%1.*"

:TheEnd

if errorlevel 0 dir "%1.*" > c:\masm32\bin\lnk.txt

start c:\masm32\tview.exe c:\masm32\bin\lnk.txt
(8)lnkc.bat(lnk.bat的console版本,用于创建命令行窗口)

@echo off

var

if exist "%1.exe" del "%1.exe"

if not exist rsrc.obj goto nores

c:\masm32\bin\Link /SUBSYSTEM:CONSOLE /OPT:NOREF "%1.obj" rsrc.obj
dir "%1.*"
goto TheEnd

:nores
c:\masm32\bin\Link /SUBSYSTEM:CONSOLE /OPT:NOREF "%1.obj"
dir "%1.*"

:TheEnd

if errorlevel 0 dir "%1.*" > c:\masm32\bin\lnk.txt

start c:\masm32\tview.exe c:\masm32\bin\lnk.txt

(9)关于dasm.bat和dasmd.bat ,这两个批处理分别对应于Dis-assemble EXE file和Dis-assemble Dll两个选项,用于反向分析,初学者暂时用不到。不过还是要把里面的内容修改完善:

$$$$$$$$$$$$$$$ dasm.bat的内容改为:

@echo off

if not exist c:\masm32\bin\dumppe.exe goto message
c:\masm32\bin\dumppe -disasm "%1.exe" > disasm.txt
c:\masm32\qeditor.exe disasm.txt
goto TheEnd

:message
echo.
echo To use this menu option, you must first unzip
echo the file called DUMPPE.ZIP in the BIN directory.
echo.
echo You can then dis-assemble the executable files
echo that you have assembled.
echo.
pause

:TheEnd

$$$$$$$$$$$$$$$ dasmd.bat的内容改为:

@echo off

if not exist c:\masm32\bin\dumppe.exe goto message
c:\masm32\bin\dumppe -disasm "%1.dll" > disasm.txt
c:\masm32\qeditor.exe disasm.txt
goto TheEnd

:message
echo.
echo To use this menu option, you must first unzip
echo the file called DUMPPE.ZIP in the BIN directory.
echo.
echo You can then dis-assemble the executable files
echo that you have assembled.
echo.
pause

:TheEnd

创建自己的inc文件,把它放在“C:\masm32\bin”里面,现在我展示一下我自己的inc文件(纯文本):

文件名:hunter.inc

文件路径:C:\masm32\bin\hunter.inc

文件内容:(用记事本编辑)

include c:\masm32\include\windows.inc

include c:\masm32\include\masm32.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\gdi32.inc
include c:\masm32\include\user32.inc

includelib c:\masm32\lib\masm32.lib
includelib c:\masm32\lib\kernel32.lib
includelib c:\masm32\lib\gdi32.lib
includelib c:\masm32\lib\user32.lib


好了,这就是我的inc文件,以后我写assemble文件就可以引用这个头文件,比如:

include C:\masm32\bin\hunter.inc

这样就不用麻烦的设置环境变量,也不用麻烦的每次都写出所有的引用头文件了,(*^__^*) 嘻嘻……


好的,以上调整完毕,可以随心所欲的使用这个IDE了,本文原创,转载请注明出处!


注:我本人是把MASM32装在F盘,刚学习win32,如有错误请包涵.......

本文结束,谢谢!








你可能感兴趣的:(Assembly)