Thursday, August 29, 2013

Congruence-Numbers that are same, but not equal?

On this post I'm going show you how to do multiple operations on a program that involves powers. Also I'm going to show you how to switch between the different operations such as: +,/,%,and *.

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
.stack 100h
.data
powerof3 dword 1
count dword 0
five dword 5
.code
doit proc
mov ecx, 3
mov ebx, 1
again:
call calculateNextPower

;Determine the next operation
mov eax,count
xor edx,edx
div five
cmp edx,0
je addition
    cmp edx,1
je addition
cmp edx,2
je multiplication
cmp edx,3
je division
cmp edx,4
je modulus

multiplication:
; Multiplying the power of 3 and the sum
mov eax,powerof3
mul ebx
mov ebx,eax
jmp hop


addition:
; adding the sum and the power of 3
add ebx,powerof3
jmp hop

division:
xor edx,edx
mov eax,ebx
div powerof3
mov ebx,eax
jmp hop

modulus:
xor edx,edx
mov eax,ebx
div powerof3
mov ebx,edx
jmp hop

hop:
; Repeat if necessary
cmp count,5
jl again

ret
doit endp

calculateNextPower proc
 ; Raising to the next power of 3
mov eax,powerof3
mul ecx
mov powerof3,eax
inc count
ret
calculatenextpower endp

end

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.






No comments:

Post a Comment