geekybedouin
geekybedouin

Reputation: 559

Assembly Iteration

I wrote the following piece of code, but it's getting the answer wrong. It's supposed to do x^2 but it seems that it iterates one more time than needed:

The result of 1 is 2 // 1+1=2
The result of 2 is 8 // 2+2+2=8

I believe its comparing CX with the ZeroFlag, not the 1 in the first statement. Is that right?

pos:
     MOV CX,Number   
    again:  Add Number,AX
            Dec cx
            cmp cx,1
            jg again ; statement no. 1
    JMP DONE  

Upvotes: 0

Views: 417

Answers (1)

Mohamed Tarek
Mohamed Tarek

Reputation: 385

As long as you're sure AX is initialized to zero, the code should work. Try:

MOV Number, AX

before the loop starts, but also check that your number is greater than 1 before the loop starts just so that it would not produce wrong results on < 1.

Upvotes: 1

Related Questions