masm32之环境搭建
1.安装
访问官网,即可根据电脑系统选择合适的版本进行下载。
下载以后,双击运行即可安装。
2.环境变量
安装以后,需要设置环境变量,这里默认大家都会设置环境变量,这里只列出需要设置的属性。
include=d:\masm32\include
lib=d:masm32\lib
path=d:\masm32\bin
3.第一个源程序
.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
.data
szText db 'HelloWorld',0
.code
start:
invoke MessageBox,NULL,offset szText,NULL,MB_OK
invoke ExitProcess,NULL
end start
我们保存为1.asm
编译:
ml -c -coff 1.asm
链接:
link -subsystem:windows 1.obj
完整:
E:\jl\blogs\code>ml -c -coff 1.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997. All rights reserved.
Assembling: 1.asm
***********
ASCII build
***********
E:\jl\blogs\code>link -subsystem:windows 1.obj
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
此时,已经编译成功,双击生成的1.exe即可运行。
运行结果如下:
4.标注输入输出
代码:
.386
.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
includelib kernel32.lib
include masm32.inc
includelib masm32.lib
include msvcrt.inc
includelib msvcrt.lib
.data
szText db 'Hello',0
szTexta db '1%s',0
len dd 3
szPrint1 db 'please input a num',0dh,0ah,0
szinput1 db '%d',0
szOut1 db 'num is %d',0
num dd ?
szPrint2 db 'please input a str',0dh,0ah,0
szinput2 db '%s',0
szOut2 db 'your input is %s',0dh,0ah,0
szstr db ?
.code
start:
;invoke StdOut,addr szText
;invoke StdIn,addr len,len
;invoke StdOut,addr len
invoke StdOut,addr szPrint1
invoke crt_scanf,addr szinput1,addr num
invoke crt_printf,addr szOut1,num
invoke StdOut,addr szPrint2
invoke crt_scanf,addr szinput2,addr szstr
invoke crt_printf,addr szOut2,addr szstr
invoke ExitProcess,NULL
end start
StdOut和StdIn是masm32.inc里面的。
crt_scanf和crt_printf是msvcrt里面的,基本和C语言是一样的,支持变长参数。