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.






Wednesday, August 21, 2013

What do you do if you wanna be true?

In my previous post about powers there was an error because we could only do even powers because we would raise the power twice per loop. Well in this post I'm going to show you a program that you can do any power you want because it only raises the power of 3 once. (if you haven't read my post called the powers you should read it before you read this post.) The way this will work is you divide count by 2 and you don't need to worry about what the answer will be because what we really care about is the remainder which will be in edx.  We compare edx to 1 and if it is = to 1 we je to the multiplication and if it is less than we jl to the addition part. And I advise you that one teeny tiny mistake can throw off the whole program but that's no reason to race through the program and think there's a problem when there's really nothing wrong. (that's the mistake I made.) There are a few ways you can see a fake problem, here I'll list them for you: looking at the wrong thing and thinking it's something else,and getting mixed up with names. And there's one more thing dividing is just like multiplying because you can't use immediate values and you can't type the name of the implied register (eax) in the instruction.



.model flat, c
.stack 100h
.data
powerof3 dword 1
count dword 0
two dword 2
.code
doit proc
mov ecx, 3
mov ebx, 1
again:
call calculateNextPower
mov eax,count
xor edx
div two
cmp edx,1
je multiplication
jl addition

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


hop:
; Repeat if neccesary
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

Monday, August 19, 2013

PROcedures

Now I'm going to talk to you about procedures. Procedures are certain pieces of code that are labeled specially. Take this example: a procedure is like a kingdom or country of it's own. Procedures can help you with what we call redundancy. Redundancy is when you have 2 pieces of code that are exactly alike so when you make a change to 1 of them you have to remember to make the same change to the other 1 and that's just such a headache!

So do you remember that program I did in my post called The powers? Well if you haven't read it yet you should before you read the rest of this post. So if you have read it do you remember how we had to raise to the next power of 3 and we did it twice? That's redundancy. Now look at my program below and notice the word proc. That stands for procedure.



.model flat, c
.stack 100h
.data
powerof3 dword 1
count dword 0
.code
doit proc
mov ecx, 3
mov ebx, 1
again:

call calculateNextPower

; Multiplying the power of 3 and the sum
mov eax,powerof3
mul ebx
mov ebx,eax

call calculateNextPower

; adding the sum and the power of 3
add ebx,powerof3

; Repeat if neccesary
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

How this works is there are 2 procedures and there is something  new, if you look at the program above you'll notice the word call and then calculateNextPower  well if you look right above you'll see calculateNextPower proc. 


When you call a location it's kind of like jl. You go to the location and then execute the code but then you get to the ret and this get's fun. You see when you do the call it moves the value of the instruction to the top of the stack and when you do the ret that's the same as saying pop eip which stands for instruction pointer so you move the value of the instruction after the call into the instruction pointer so you go back to where you came from.




Saturday, August 17, 2013

Reverso

Here's a program that stores 1,2,3,4 in the registers, and then reverses it to 4,3,2,1.


.model flat, c
.stack 100h
.data
store dword 0
.code
doit proc
mov eax,1
mov ebx,2
mov ecx,3
mov edx,4


mov store,eax
mov eax,edx
mov edx,store
mov store,ebx
mov ebx,ecx
mov ecx,store

Here's another way that you can do it except this way uses the stack.

.model flat, c
.stack 100h
.data
store dword 0
.code
doit proc
mov eax,1
mov ebx,2
mov ecx,3
mov edx,4

push eax
push ebx
push ecx
push edx

pop eax
pop ebx
pop ecx
pop edx


Now the stack is a section of R.A.M . I'm going to give you a little lesson to show you a little example of  what it looks like and what it does.

Imagine your'e inside a restaurant. There is a pile of plates on a table. A neat stack if you please. This stack is an example of what we call LIFO which stands for last in, first out. This means: the last plate that gets put on the stack is the first one to get taken off.

Now that I've told you about that take a look at the program. You see how I've got the words pop and push in there? Let me tell you how that relates with the stack of plates. When I "pop" the stack that means I take a plate off the top. When I "push" the stack that means I put a plate on the top. You notice how I'm popping in the same order I'm pushing? That's what reverses the registers.

The powers

On this post I'm going to show you a program that I did with my dad to do a little adding and multiplication. This program is supposed to do this problem: 31 + 32 + 33 + ... 3n



In this program ebx tracks the SUM,and ecx tracks the base. Eax serves as a general purpose multiply. This program was challenging for me because I didn't know very much about powers.

32 = 3 x 3
33 = 3 x 3 x 3
34 = 3 x 3 x 3 x 3
35 = 3 x 3 x 3 x 3 x 3
36 = 3 x 3 x 3 x 3 x 3 x 3   

32 is a short way of saying 3 x 3,33 is a short way of saying 3 x 3 x 3 etc. SUM is the total of adding the 2 powers together. If you go high enough going up power by power you will get HUGE amounts of overflow so much in fact the amount in R.A.M is nothing compared to the real answer. Sometimes when you are doing a big enough problem you have to use R.A.M because edx doesn't cooperate because when you do multiplication it tells you what you didn't use. So you gotta use R.A.M instead. If you don't know what ram is you should read my earlier blogs.

Now I'm going to tell you which registers are doing what while this is all going on. R.A.M tracks the power of 3 which is how many 3's are being multiplied. Ecx is tracking the base which is 3. Ebx is tracking the SUM or total. And as for edx it is not doing anything because as I said before edx wouldn't work. As for R.A.M it takes over for edx as the power of 3.

Here is a chart I made to track everything for each iteration:


As you can see I have shown you what is in R.A.M,and ebx,and I have written down what ecx,eax,and edx are doing.



And here's the program.

.model flat, c
.stack 100h
.data
powerof3 dword 1
count dword 0
.code
doit proc
mov ecx, 3
mov ebx, 1
again:

; Raise to the next power of 3
mov eax, powerof3
mul ecx
mov powerof3,eax
inc count

; Multiplying the power of 3 and the sum
mov eax,powerof3
mul ebx
mov ebx,eax

; Raising to the next power of 3
mov eax,powerof3
mul ecx
mov powerof3,eax
inc count

; adding the sum and the power of 3
add ebx,powerof3

; Repeat if neccesary
cmp count,5
jl again
  


Thursday, August 1, 2013

The Secrets Of cmp

Sign flag and zero flag
efl is flags register

how cmp changes the flags

how the jumps analyze the state of the flag bits


what inc does to the flags, and why we can't inc after we cmp


example programs with issues:

mov eax,1
mov ebx,2
again:
inc ebx   ; inc changes the state of flags due to the value of the last result
mul ebx
cmp ebx,100
jl again


; stops at 0
doit proc
mov eax, -5 ; what will be the sign and carry as we inc eax?
again:
cmp eax, 10
inc eax
jl again
ret


On this blog I'm going to talk about the sign flag and the zero flag. The sign flag and the zero flag are both in the same register and they determine whether or not you pass on when you get to the jl or the jle. Now the secret to what happens in the sign flag and the zero flag lies in cmp. Now if you don't know what cmp means you should read the post called loop de loop 1st.

All that a cmp does is a subtraction. The sign flag shows whether or not the number is negative when you subtract the number that your'e counting to from it. If it is negative the sign flag will be a 1,if it isn't the sign flag will be a 0.  The zero flag shows whether or not the answer is 0. If the answer is 0 the zero flag will be a 1. If the answer isn't 0 the zero flag will be 0.

Here are some examples:

cmp 5, 6

The results would be:      sign flag    1         zero flag    0

cmp 5,5

The results would be:                     0                           1

cmp 6,5                

The results would be:                    0                            0


Those are the 3 possible combinations you can have with this. Now I bet your'e thinking that can't be right if there are 2 bits there should be 4 possible combinations well the one that both of them would be a 1 wouldn't be possible because the answer can't be 0 and negative at the same time.

Loop De Loop

adding 1 + 2 + 3 + 4 + 5 + 6 + 7 up to a really big number like 100, first talk about adding 1 to 5, but then you can change the 5 to a 100

inc
cmp
jl
jle

63
multiple ways to write the same program

talk about the problems that you get when you put the inc between the cmp and the jl



Write blog post down here:

Have you ever wondered what you would get if you added 1+2+3+4  etc.? Well if you have this should be a good learning experience. First you need to put a 0 in eax, then you need to put a 1 in ebx. Then you add them together and after you do that you have two choices you can move the answer into another register and put a 2 in eax and a 3 in ebx and add them together and then add that to the answer and keep moving the numbers in eax and ebx up one time and write tons of code until you reach the number you want to go to, or you can take the easy way that takes a little bit more learning but it will save you a ton of headache if you do it.

The easy way has a lot to do with something called loops. A loop is when you do little bit of code that if repeated will make a good program and that's where the little bit of learning comes in. The first thing you need to know about if you want to take the easy way is cmp which is compare. Now cmp is when you put a number in a certain piece of ram, or register and use it as a counter. To count you can do a little piece of code called inc which means increment. When you inc ram or a register it adds one to that ram or register.
So  when you inc the piece of ram or register it gets bigger and when it gets to the number that you are counting to you are done, but the bit of magic that makes the program start all over again and completely save the headache there is one more thing.

The final piece of the puzzle lies in a little piece of code called jle. Now jl stands for jump less than or equal to that means if the digit in the counter is less than or equal to lets say 5 then go back to again. What I mean by again is you set a point in the program that you want to start over from. I like to call it again.

        mov eax,0
        mov ebx,1
again:
        add eax,ebx
        inc ebx
        cmp ebx,5
        jle again