Hey,
When I went through the code, I did get 1,4,9! Okay I will try to go through this using two examples:
Example 1 - Simple - Input = 1:
num = 1
one = 1
count = 0
total = 0
The loop gets executed: In the ACC, total (0) is loaded. We add num (1) to the ACC and we store the result in Total.
num = 1
one = 1
count = 0
total = 1
We then load count (0) and ADD 1 to it. We then store this in count!
num = 1
one = 1
count = 1
total = 1
We the subtract num from what is currently in the ACC (which is count).
Count - num = 1 - 1 = 0
BRZ - because the previous subtraction resulted in a 0, the next line of execution is the end selection block. Here, total (1) is loaded and is printed, the program halts!!
OUTPUT = total = 1
1 x 1 = 1 so it worked!!
Example Two - Harder - Input = 2:
num = 2
one = 1
count = 0
total = 0
The loop gets executed: In the ACC, total (0) is loaded. We add num (2) to the ACC and we store the result in Total.
num = 2
one = 1
count = 0
total = 2
We then load count (0) and ADD 1 to it. We then store this in count!
num = 2
one = 1
count = 1
total = 2
We then subtract num from what is currently in the ACC (which is count).
Count - num = 1 - 2 = -1
The ACC content is NOT 0. So BRZ wont happen! The next line is BRA to loop so the next line to be executed will be line 3.
We once again load total (2) and add num (2) to it. Store this in total!
num = 2
one = 1
count = 1
total = 4
Count (1) is loaded and One (1) is added and is stored in count which is now 1 + 1=2
num = 2
one = 1
count = 2
total = 4
Subtract num from the ACC content (which contains the result to the previous addition). In the ACC, count is currently stored. Subtract num (2) from the ACC. 2 - 2 = 0
BRZ - because the previous subtraction resulted in a 0, the next line of execution is the end selection block. Here, total (4) is loaded and is printed, the program halts!!
OUTPUT = total = 4
2 X 2 = 4 so it worked!!
Hope this helped!!!
Have a great month ahead