嵌套汇编循环产生意外输出

时间:2017-11-03 19:43:17

标签: assembly x86

我正在尝试在程序集中构建嵌套循环。这是我试图转换的代码。

int total = 0 ;
for (i = 1; i <= 10; i++)
{
     for (j = 1; j<=5; j++)
     {
          total = total + i*j;
          System.out.println(total);
     }
}

输出不是我所期望的。代码似乎应该可以工作。

3892
3910
3922
3928
3953
3972
3988
4003
4032

我的代码

%include "io.mac"
.STACK 100H 
.DATA

  total  db 0

   .CODE
        .STARTUP

    mov  ECX,10 ; set outer loop count
L1:             ; begin the outer loop

    push ECX ; save outer loop count

    mov ECX, 5 ; set inner loop count

L2:
    mov ax, bx ;move saved outer loop count into ax to be multiplied by cx
    mul CX  ;multiply inner loop count by outloop count

    add [total], AX  ;add ecx to the total
    nwln
    PutInt [total]   ; print answer
    loop L2 ; repeat inner loop


    pop ECX ;restore outer loop
    mov BX, CX ;move outer loop count into EAX
    loop L1 ;repeat outer loop

        .EXIT

0 个答案:

没有答案