Search code examples
assemblypeppep9-assembly

How can you square a number in pep9?


I am trying to multiply a number by itself in pep9 to get the square of that number. However, I am not sure how to do it as, if I try adding the number by itself at the end, it will give me a random value. Please help.

Code:

main1:  STRO msg4,d
        DECI nNum,d          
niLoop: LDWA nNum,d 
        BREQ done1
        SUBA 0x0001,i
        STWA nNum,d
        LDWA result1,d
        ADDA nNum,d 
        STWA result1,d    
        BR niLoop
done1:  BR main2

The output I get when entering a value, e.g. 6, is 15? Not sure why. Any help would be appreciated!


Solution

  • Single step just one iteration, and you'll see the problem:

    What you're doing is computing 0 + 5 + 4 + 3 + 2 + 1 = 15.

    What you want to do is compute either 0 + 6 + 6 + 6 + 6 + 6 + 6 = 36, or just 6 + 6 + 6 + 6 + 6 + 6 + 6 = 36