2014-01-07 202 views
0

我只是试图打印数组的元素。从输出我可以看到,循环超出了我的数组分配的内存。为什么这个循环无限?

.386 ; 386 Processor Instruction Set 

.model flat,stdcall 

option casemap:none 
include \masm32\include\masm32rt.inc 
include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
includelib \masm32\lib\kernel32.lib 

.data 

array DWORD 72,50,22,0 
asd DWORD ? 

start: 

mov ecx, 4 
mov edi, 0 
//-- loop start--// 
loop_start: 

mov eax, [array + edi * 4] 

push offset asd 
push eax 
call dwtoa 


Invoke StdOut, addr asd 

inc edi //incrementing edi 
dec ecx // decrementing ecx 
cmp ecx,0 // comparing ecx against 0 


jne loop_start // if not equal loop again 
//--loop end--// 


invoke ExitProcess, 0 
end start 

这里是输出 http://s7.directupload.net/images/140107/2nxsljtc.png http://s7.directupload.net/images/140107/snpycplx.png

编辑:试图在年底

cmp ecx,0 
je loop_end 


loop_end: 
Invoke ExitProcess,0 

没有这些努力的补充。

在此先感谢。

+1

这看起来像是loop_start和DEC ECX之间倒是ECX?您是否尝试过在CALL之前推送它,然后在之后弹出? –

+0

you mean,push ecx, dec ecx, pop ecx, ? – ddacot

+0

'dec'和'inc'会影响ZF,因此您可以减少compare-with-0指令 –

回答

2

看来,这两个指令改变ecx寄存器:

call dwtoa 
Invoke StdOut, addr asd 

我的猜测是在dwtoa它可能返回ASCI数组的长度在ecx寄存器返回。

试试这个:

loop_start: 

mov eax, [array + edi * 4] 

push ecx // saving ecx before call 

push offset asd 
push eax 
call dwtoa 


Invoke StdOut, addr asd 

pop ecx // restore the ecx from before the calls. 

inc edi //incrementing edi 
dec ecx // decrementing ecx 
cmp ecx,0 // comparing ecx against 0 


jne loop_start // if not equal loop again 
+0

这就是问题所在,谢谢! – ddacot