无法理解汇编语言中的嵌套循环

Unable to understand nested loops in assembly language

这是 Irvine 为基于英特尔的计算机编写的汇编语言练习:

本例中 EAX 的最终值是多少?

   mov eax,0
   mov ecx,10     ; outer loop counter
L1:
   mov eax,3
   mov ecx,5      ; inner loop counter
L2:
   add eax,5
   loop L2        ; repeat inner loop
   loop L1        ; repeat outer loop

我的解决办法是eax=28,因为最初ecx设置为5,所以L2执行了5次,所以eax=3+5*5=28。然后ecx变为0,流移出L2,但由于ecx=0,L1不会重复,它会移动到下一行而不重复L1。所以 eax=28 的最终值。我知道这是错误的,但有人能告诉我我的想法错在哪里吗?

你是对的,还有一个更大的问题是这段代码永远不会结束。

    mov     eax, 0              ; Unnecessary as next instruction changes this
L1: 
    mov     eax, 3
    mov     ecx, 5
L2: add     eax, 5
    loop    L2
    loop    L1                  ; ECX is decremented first

所以这段代码会像 Energizer Bunny 一样,永远继续下去,结果会一遍又一遍地连续 28。

    mov     eax, 0              ; Unnecessary as next instruction changes this
L1: push    ecx                 ; This and the pop below solves the problem
    mov     eax, 3
    mov     ecx, 5
L2: add     eax, 5
    loop    L2
    pop     ecx
    loop    L1                  ; ECX is decremented first

现在程序至少会结束,但结果仍然只会是28。如果需要像我认为的objective那样是253,那么你应该可以看到那个您需要移动才能实现的说明。