看下面的小程序,很簡單,你認為執行結果會是什麼?
------------------------------------------------------------
;文件名:11.ASM
;利用console顯示一個字符串信息
.386
.model flat,stdcall
option casemap:none
include \MASM7\INCLUDE\windows.inc
include \MASM7\INCLUDE\kernel32.inc
include \MASM7\INCLUDE\masm32.inc
includelib \MASM7\LIB\kernel32.lib
includelib \MASM7\LIB\masm32.lib
.code
messAdd dd 0
mess db 'How are you !',0
start:
lea eax,mess
mov messAdd,eax ;寫代碼段中的數據
invoke StdOut,messAdd
invoke ExitProcess,NULL
end start
-----------------------------------------------------------
太簡單啦,不就是利用console輸出一個字符串?對啦,這就是程序的原意!
但結果卻不是想象的。因為在執行程序時,映入我們眼簾的首先是一個錯誤的消息框!
為什麼會這樣呢?因為Windows在鏈接時設置代碼段一個屬性,那就是“讀\執行\代碼”,就是不允許寫。所以就出錯啦!有沒有解決的辦法呢?
照著下面的操作吧,它可使代碼段有寫的屬性:
D:\MASM7>ml /coff 4.asm /link /subsystem:console /section:.text,rw ;R-讀,W-寫
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997. All rights reserved.
Assembling: 4.asm
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/subsystem:console /section:.text,rw
"4.obj"
"/OUT:4.exe"
D:\MASM7>11
How are you !
D:\MASM7>_