简体   繁体   中英

Looping in assembly language

how do I translate this C# code in assembly using loops and/or jumps

        int sum = 0;
        int count = 15;

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

        Console.WriteLine(sum);

a "while" is usually

L_repeat:
    compare while_condition
    jumpIfFalse L_exit

    ... loop body ...

    jump L_repeat
L_exit:

also for an if, you have to invert the condition:

compare if_condition
jumpIfFalse L_skip

 ... inside if

L_skip:

then you need to Registers, one to hold your count, one for the sum, and come out with something like this:

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:    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM