Windows console(命令行)版的汇编程序例子及解说

下载masm32   http://u.115.com/file/f5e5cb46c4
masm32_v10.exe

安装到c:\


////////////////////////////////////
.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
.data
HelloWorld db "Hello World!", 0
.code
start:
invoke StdOut, addr HelloWorld
invoke ExitProcess, 0
end start
///////////////////////////////////////////////////
将上面程序文本复制粘贴到记事本,另存为:hello.asm 保存类型改为:所有文件
保存到:c:\masm32\bin\ 内
打开命令提示符,键入:cd\ 回车【Enter键】
键入:cd masm32 回车
键入:cd bin 回车
键入:ml /c /Zd /coff hello.asm 回车
键入:link /subsystem:console hello.obj 回车
键入:hello 回车
ok!
注意:Zd的Z要大写字母!你编的源程序xxx.asm放在bin文件夹内时可以简化命令。
完成后可以看到bin文件夹内多出两个文件:hello.obj和hello.exe
调试运行完毕后,再运行时,直接键入hello(即hello.exe)回车即可。
/////////////////////////////////////////////
逐行解释:
.386--告诉汇编器:我要使用386指令集。
.model flat, stdcall--
   指定程序的内存模式为flat,在Win32下,只有一种内存模型,那就是FLAT。 stdcall告诉编译器参数的传递约定。参数的传递约定是指参数传递时的顺序(从左到右或从右到左)和由谁恢复堆栈指针(调用者或被调用者)。在Win16下有两种约定:C 和 PASCAL。C 约定规定参数传递顺序是从右到左,即最右边的参数最先压栈,由调用者恢复堆栈指针。
option casemap :none--
强迫标签为字母大小写敏感。这和高级程序相似。要养成良好习惯。

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc --
windows.inc 它包含了Win32API常数和定义的声明。
kernel32.inc 它包含了退出函数。
masm32.inc 它包含了输出函数(显示在屏幕上)。它在masm32中内置着。

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

退出和输出函数须要有库libraries。

.data --所有程序数据在此指令(伪指令)的下面。
HelloWorld db "Hello World!", 0 --
变量名 定义字节 变量内容:字符串"Hello World!" 0表示字符串结束。

.code --指向程序代码
start: --所有代码在此标签之后开始。
invoke StdOut, addr HelloWorld --
   invoke调用一个输出函数StOut,字符串"Hello World!"的地址:addr 变量名
invoke ExitProcess, 0 --传递0给函数ExitProcess,退出程序。
/////////////////////////////////////////////////

ml /c /Zd /coff

/c 是告诉MASM只编译不链接。这主要是考虑到在链接前您可能还有其他工作要做。
/coff 告诉MASM产生的目标文件用 coff 格式。MASM 的 coff 格式是COFF(Common Object File      Format:通用目标文件格式) 格式的一种变体。
/Zd--Add line number debug info加上行号调试信息。
当您成功的编译了 x.asm 后,编译器会产生 x.obj 目标文件,目标文件和可执行文件只一步之遥,目标文件中包含了以二进制形式存在的指令和数据,比可执行文件相差的只是链接器加入的重定位信息。

link /subsystem:console x.obj

/subsystem:console 告诉链接器可执行文件的运行平台是console即命令行(另一个平台是windows) 。
/////////////////////////////////////////////////////

ML.EXE 6.14 Options


Options

ML [ /options ] filelist [ /link linkoptions ]
/AT Enable tiny model (.COM file)
/Bl<linker> Use alternate linker
/c Assemble without linking
/Cp Preserve case of user identifiers
/Cu Map all identifiers to upper case
/Cx Preserve case in publics, externs
/coff generate COFF format object file
/D<name>[=text] Define text macro
/EP Output preprocessed listing to stdout
/F <hex> Set stack size (bytes)
/Fe<file> Name executable
/Fl[file] Generate listing
/Fm[file] Generate map
/Fo<file> Name object file
/FPi Generate 80x87 emulator encoding
/Fr[file] Generate limited browser info
/FR[file] Generate full browser info
/G<c|d|z> Use Pascal, C, or Stdcall calls
/H<number> Set max external name length
/I<name> Add include path
/link <linker options and libraries>
/nologo Suppress copyright message
/Sa Maximize source listing
/Sc Generate timings in listing
/Sf Generate first pass listing
/Sl<width> Set line width
/Sn Suppress symbol-table listing
/Sp<length> Set page length
/Ss<string> Set subtitle
/St<string> Set title
/Sx List false conditionals
/Ta<file> Assemble non-.ASM file
/w Same as /W0 /WX
/WX Treat warnings as errors
/W<number> Set warning level
/X Ignore INCLUDE environment path
/Zd Add line number debug info
/Zf Make all symbols public
/Zi Add symbolic debug info
/Zm Enable MASM 5.10 compatibility
/Zp[n] Set structure alignment
/Zs Perform syntax check

posted on 2010-04-16 13:32  蓝牙  阅读(1302)  评论(0编辑  收藏  举报