Here is the problem we're trying to solve:
Look carefully at the problem and notice that there's several operations but they are still in a pattern. If you can't see it very well the pattern goes +,+,*,/,%. Now I'm going to talk about the symbol %. If your'e good at division this shouldn't be too hard to understand. That symbol stands for what we call mod (short for modulus). If your'e moding something all your'e doing is dividing it by a number except you don't show the answer you show the remainder. If your'e reading this you probably know what I mean by the remainder,but if you don't I'll tell you. Sometimes you can't divide some thing without having a few left. The remainder is the number that's left. For example: 5 % 3=2.
Here's my program:
.model flat, c
powerof3 dword 1
count dword 0
five dword 5
mov ecx, 3
mov ebx, 1
;Determine the next operation
; Multiplying the power of 3 and the sum
; adding the sum and the power of 3
; Repeat if necessary
; Raising to the next power of 3
If you observe the top parts of the program do you notice how there are a lot of cmps? That part is how we switch between the different operations. It's actually best described as a large modulus. First you divide count by 5(count is the register that we use to count up on the program)and the remainder is placed in edx(by the way you have to make sure edx is all zeroed out because it's gotta be ready to hold the remainder). Then you cmp edx to 0 and you je to addition and if it dosen't jump you cmp it to 1 and je to addition (because there are 2 additions in the pattern) and if it still doesn't jump then you cmp it to 2 and je to multiplication and if it still doesn't jump then you cmp it to 3 and je to division and if it doesn't jump then you cmp it to 4 and you je to modulus and if it doesn't jump by then you got something wrong.
So as I said it's like a giant modulus and we're moding by 5 so when the power get's to 5 the pattern starts all over again.
Now I'm going to talk about congruence. Congruence is something that goes with modulus. Look at this:
0 % 3= 0
1 % 3= 1
2 % 3= 2
3 % 3= 0
4 % 3= 1
5 % 3= 2
6 % 3= 0
7 % 3= 1
8 % 3= 2
Now if you look at the example above, do you notice that the remainders (answers to the problem are not in here because we look at the remainder when we mod) are in a pattern 0,1,2? Well the answers are CONGRUENT to the number that is being moded, for example:0 is congruent to 3, and 6 because they have the same remainder which is 0. 1 is congruent to 4,and 7 because they have the same remainder which is 1. 2 is congruent to 5,and 8 because they have the same remainder which is 2.
This program works, and the way I can tell is it follows along with my chart as I debug it.