0

这是具有多个循环的代码,我需要修复这个无限循环。任何人都可以修复它吗?

.data   



 a dword 10

 b dword 1

 d dword 35



 delta dword ?
.code

main PROC

  call clrscr

  mov dh,0

  mov ecx,10

  push ecx

  row:
mov dl,01
mov ecx, a
mov eax,'*'
line:

       call gotoxy

       call writechar

       inc dl

    loop line



    mov ecx, b

    mov eax,'p'

   line1:

     call gotoxy

     call writechar

     inc dl

   loop line1

   mov ecx, d

    mov eax,'c'

   line2:

     call gotoxy

     call writechar

     inc dl

   loop line2

   inc dh

   mov dl,1

   dec a

   inc b

   inc b

   dec d

   dec d

   dec d

   pop ecx

  loop row

帮助无限循环。

4

2 回答 2

0

Don't use loop instruction to make nested loops!

I don't know why, but recently on SO, there are many beginners trying to make several nested loops by using loop instruction (and MASM). These are probably some stupid teacher minds how to make nested loops in assembly.

So, stop this! It is a bad practice at all. Forget about loop instruction and use separate registers for the loop counters. It is so easy and clear:

        mov  edx, 1000
loop1:
        mov  ecx, 2000
loop2:
        mov  ebx, 3000
loop3:
        ......
        dec  ebx
        jnz  loop3

        dec  ecx
        jnz  loop2

        dec  edx
        jnz  loop1

Also, check what registers gotoxy and writechar procedures preserve, because they probably change some registers. Check especially for the registers you use in the loops.

于 2013-11-06T07:21:53.923 回答
0

您应该移动push ecx下面的row:标签,最后一个pop ecxloop吃堆栈的值,因为没有推送。

此外,您应该遵循@johnfound 的回答,因为这些都是很好的建议(dec+jnz 和子例程可能会修改寄存器值)。

编辑:

此外,mov ecx, aandbd实际上正在使用可能非常大的变量的地址,应该mov ecx, [a]或者可能mov ecx, dword ptr [a](asm rusty)。也许incdec可能需要类似的治疗

于 2013-11-06T13:56:58.000 回答