2014-01-07 46 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