用汇编语言循环

时间:2016-09-30 03:27:20

标签: c# loops assembly x86

如何使用循环和/或跳转在程序集中翻译此C#代码

        int sum = 0;
        int count = 15;

        while (count != 0)
        {
            if (count % 2 != 0)
                sum = sum + count;
            count--;
        }

        Console.WriteLine(sum);

1 个答案:

答案 0 :(得分:0)

“while”通常是

L_repeat:
    compare while_condition
    jumpIfFalse L_exit

    ... loop body ...

    jump L_repeat
L_exit:

对于if,你必须反转条件:

compare if_condition
jumpIfFalse L_skip

 ... inside if

L_skip:

然后你需要注册,一个用于计算,一个用于总和,并且得出类似的东西:

mov Rsum,0
mov Rcount,15

L_repeat:
   cmp Rcount,0
   jz L_exit          ; jump if not (Rcount != 0) gets jump if Rcount==0
       mov Rtemp, Rcount
       and Rtemp, 1
       jz L_skip
       add Rsum, Rcount
    L_skip:
       dec Rcount
       jmp L_repeat
l_exit: