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

없음에 추가했습니다.

미리 감사드립니다.

call dwtoa 
Invoke StdOut, addr asd 

내 생각은 dwtoa로는 ASCI 배열의 길이가 ecx 레지스터에 반환 반환 할 수있을 것입니다 :

+1

는 loop_start DEC이 ECX 사이 ECX에 닿을? 당신은 CALL 전에 그것을 밀고 그 후에 튀어 오르는 시도를 했습니까? –

+0

뜻, 푸시 ecx, dec ecx, pop ecx, ? – ddacot

+0

'dec'과'inc'가 ZF에 영향을 미치므로 0과 비교 명령을 줄일 수 있습니다. –

답변

2

이 두 가지 지침은 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

관련 문제