gpt4 book ai didi

windows - assembly 计数程序

转载 作者:可可西里 更新时间:2023-11-01 10:00:07 25 4
gpt4 key购买 nike

我是汇编的新手,我正在尝试编写一个计数达到 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 `

它应该打印 Number 1 Number 2 Number 3 等,但它会打印 Number 2 Number 2 Number 2 Number 2 Number 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

.结束开始

最佳答案

库函数根据自己的需要使用寄存器,不会将它们恢复为原始值。如果您不想丢失它,则需要将其保存在内存中。最简单的方法是使用堆栈:

push eax ; put value of eax on the top of stack

pop eax ; remove value from top of stack, save it in eax.

关于windows - assembly 计数程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15451099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com