2016-10-18 57 views
1

我正在做家庭作业的任务,我需要随机打印20行20随机字符到屏幕上。我对汇编语言非常陌生,不明白为什么我的循环不会结束,即使我将ecx设置为20,并且每次都递减。组装无限循环[家庭]

当前屏幕正确地打印了随机字母,但从未停止打印。

我的代码如下:

INCLUDE Irvine32.inc 
    .data 
     buffer byte 20 dup(?) ;buffer of size 20 initialized ? 
     L dword 20  ;length of size 20 
    .code 

    main proc 

     l1: 
      mov ecx,L ;ecx = 20 
      call RandomString ;call Random String 
      dec ecx ;ecx -- 
      cmp ecx,0 ;compare ecx to zero 
      jne l1 ;jump if not equal back to l1 

      call WaitMsg ;press any button to continue 

    exit 
    main endp 

    RandomString PROC USES eax ecx edx 
     mov eax,26  ;eax = 26 
     call RandomRange ;call RandomRange 
     add eax, 'A' ;eax = random number between 0 and 25 + 'A' 
     mov buffer,al ;buffer = random letter 
     mov edx, OFFSET buffer ;edx = address of buffer 
     call WriteString ;write string to console 

    ret 
    RandomString ENDP 

    end main 

回答

1

你不停重置ECX:

l1: 
     mov ecx,L ;ecx = 20 <--set ecx to 20 
     call RandomString 
     dec ecx ;ecx --  <--ecx is now 19 
     cmp ecx,0 ;compare ecx to zero 
     jne l1    <-- jump to l1, and ecx becomes 20 again 

你应该移动到movl1标签:

 mov ecx,L ;ecx = 20 
    l1: 
     call RandomString ;call Random String 
     dec ecx ;ecx -- 
     cmp ecx,0 ;compare ecx to zero 
     jne l1 
+0

完美!这就是它!非常感激! – GreenFerret95

+0

@ GreenFerret95当某人提供了有意义的答案时,您应该将其标记为这样,当其他读者查看问题列表时,可以轻松识别出具有答案的答案。如果还有多个答案,则您选择的答案将立即显示在您问题的下方。 –