我是程序集新手,我試圖創建一個計數高達10,000的程序並退出。我使用FASM`程序集計數程序
include 'include/win32ax.inc'
.data
inchar DB ?
numwritten DD ?
numread DD ?
outhandle DD ?
inhandle DD ?
strFormat DB "Number %d ",0
strBuff RB 64
.code
start:
;set up console
invoke AllocConsole
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov [outhandle],eax
invoke GetStdHandle,STD_INPUT_HANDLE
mov [inhandle],eax
;loop starts here
mov eax, 0
LoopStart:
add eax,1
invoke wsprintf, strBuff, strFormat, eax ;convert number to String.
;the number eax is now in string form in strBuff
;find out the string length of strBuff
mov ecx,-1
mov al,0
mov edi,strBuff
cld
repne scasb
not ecx
dec ecx
;ecx is now the length.
invoke WriteConsole,[outhandle],strBuff,ecx,numwritten,0 ;write to console
;loop
cmp eax, 10000;loop compare
jne LoopStart;jump to start of loop
invoke ReadConsole,[inhandle],inchar,1,numread,0 ;give the user a chance to read console output before exit
invoke ExitProcess,0
.end start `
它應該打印1號2號3號等,而是將其打印數量2 2 2號2號2號等了一會兒,然後退出,無需等待用戶輸入。我的代碼有什麼問題?
編輯:我得到它的工作!工作代碼:
include 'include/win32ax.inc'
.data
inchar DB ?
numwritten DD ?
numread DD ?
outhandle DD ?
inhandle DD ?
strFormat DB "Number %d ",0
strBuff RB 64
number DD ?
.code
start:
;set up console
invoke AllocConsole
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov [outhandle],eax
invoke GetStdHandle,STD_INPUT_HANDLE
mov [inhandle],eax
;loop starts here
mov eax, 0
LoopStart:
add eax,1
mov [number],eax
mov edi, eax
push eax
invoke wsprintf, strBuff, strFormat, edi ;convert number to String.
add esp, 4 * 3
pop eax
;the number eax is now in string form in strBuff
;find out the string length of strBuff
mov ecx,-1
mov al,0
mov edi,strBuff
cld
repne scasb
not ecx
dec ecx
;ecx is now the length.
push eax
invoke WriteConsole,[outhandle],strBuff,ecx,numwritten,0 ;write to console
pop eax
;loop
mov eax, [number]
cmp eax, 10000;loop compare
jne LoopStart;jump to start of loop
invoke ReadConsole,[inhandle],inchar,1,numread,0 ;give the user a chance to read console output before exit
invoke ExitProcess,0
.END開始
打印將破壞EAX中的值。調用其他函數時必須保存並恢復它。 – 2013-03-16 15:42:12
@BoPersson這將如何完成?我嘗試在內存位置123存儲eax並在打印後恢復它,但我的程序說:Test.exe遇到錯誤,需要關閉。我們對這種不便表示抱歉。 – user2097804 2013-03-16 15:46:01
@ user2097804你爲什麼要寫內存地址'123'?首先,你是如何知道該內存地址不包含某些代碼或數據的?然後,你寫入特定內存地址的動機是什麼?你似乎沒有用'malloc'或類似的東西來保留任何內存。可能操作系統不會給你寫入該內存地址的權利,因此試圖導致分段錯誤。 – nrz 2013-03-16 15:56:49