Wednesday, September 25, 2013

The pattern destroying procedure

On this post we're gonna learn about some stuff a lot like what we learned on the previous post. (You should read the previous post before you read this post.) 1st we'll start with the way that we switch between the different operations. Before we were just doing a ton of addition but now we're gonna do some multiplication and subtraction too.

Here is the problem we're trying to solve:
                                                              0+2^7+3^5-6^3*9^2+4^3*2^3
And you notice how there isn't really a pattern like on other programs? Well because of that we have to describe the pattern using memory. This is a data driven approach because the data is memory so instead of using a pattern to do the problem we use memory (ram) to do the problem.

 You remember how we did the termindex thing on the previous post? Well that's what we're gonna be doing a lot of so you better remember it! (by the way you might wanna have some paper handy.)On the previous post on our program we used  termindex to offset  R.A.M to get the bases and exponents and we have to multiply termindex by 4 because the different numbers are 4 bytes apart and to get to the next number on the next time around we have to inc termindex so when we multiply by 4 we get bigger numbers to get to the numbers that are further in the program. That's how we're going to get the numbers we need. 0,1,2,0,2 those are the numbers in operations 0=addition 1=subtraction 2=multiplication so when we termindex and get the number that we're at at the time we are in a procedure called determineOperations so we cmp the number to 0 and je to addition we cmp it to 1 and je to subtraction then we cmp it to 2 and je to multiplication.

One thing I have to tell you,if you want to do a long program like this you are gonna have to get organized. And what comes in handy there is a chart because last I checked its really hard to hold a million numbers in you're head. You'll want to keep track of the bases and the exponents,total,and hex total.

Here is my chart:
Now I'm going to talk about scope. What scope means is that you can't jl to a label outside of the procedure that your at. For example: If I put a label called restart in doit and I said jl restart in raiseToThePower I wouldn't be able to do that.And one more thing I need to talk aboutprocedures only worry about themselves they don't care for any thing else. For example:If determineOperations needed to use ebx it woudn't care if raiseToThePower had done something with it it would just take it and use it.
Here is my program:

.model flat, c
.stack 100h
.data
 count dword 0
 total dword 0
 bases dword      2,3,6,9,4,2
 exponents dword  7,5,3,2,3,3
 operations dword 0,0,1,2,0,2
 termIndex dword 0
.code
doit proc

restart:
mov eax,4
mul termIndex
mov ebx,eax
mov ecx,bases[ebx]
mov ebx,exponents[ebx]
call raiseToThePower
call determineOperations
inc termindex
cmp termindex,6
jl restart
ret
doit endp

determineOperations proc
mov ebx,eax
mov eax,4
mul termindex

cmp operations[eax],0
je Addition
cmp operations[eax],1
je Subtraction
cmp operations[eax],2
je Multiplication

ret
  Multiplication:
mov eax,total
mul ebx
mov total,eax
ret
  Subtraction:
sub total,ebx
ret
  Addition:
add total,ebx
ret
determineOperations endp

; inputs: bases ecx,exponents ebx,output:eax (answer)
raiseToThePower proc
mov count,0
mov eax,1
  again:
mul ecx
inc count
cmp count,ebx
jl again

ret
raiseToThePower endp

end

Thursday, September 12, 2013

PROcedures-The battle of Memoryman and Evil Dr.Redundandancy

On my previous post I told you that on this post I would show you how to deal with the redundancy,and yes I am. The first thing we gotta do is make 3 more pieces of R.AM and we'll label them bases,exponents and termindex. The next thing we do is put these numbers 2,3,6,9,4 (make sure you put comma's in between the numbers)and these numbers in exponents 7,5,3,2,3 and a 0 in termindex example:

bases dword 2,3,6,9,4
exponents dword 7,5,3,2,3
termindex dword 0

P.S don't forget to put count and total in.

Now I'm going to talk about a new word............consecutive. Consecutive means things come right after each other. You notice that in bases and exponents there are numbers just all together in a line? Well how consecutive go's in with this is that the 1st number is in the 1st 4 bytes and the next number is in the next 4 bytes so on so on. Her'es my program that uses memory to get rid of the redundancy and simplify the process.


.model flat, c
.stack 100h
.data
count dword 0
total dword 0
bases dword 2,3,6,9,4
exponents dword 7,5,3,2,3
termIndex dword 0
.code
doit proc

restart:
mov eax,4
mul termIndex
mov ebx,eax
mov ecx,bases[ebx]
mov ebx,exponents[ebx]
call raiseToThePower
add total,eax


inc termIndex
cmp termIndex,5
jl restart
ret
doit endp

raiseToThePower proc
mov count,0
mov eax,1
again:
mul ecx
inc count
cmp count,ebx
jl again

ret
raiseToThePower endp


end


Now I'm going to explain how this program is running through. Well the first few lines are what I'm going to talk about first (because they're the FIRST few lines),you see there's mov eax,4 then mul termindex,and what that's doing is there are 4 bytes in between the different numbers so there there needs to be a 4 in termindex at 1st nothing happens because termindex is a 0 and the first 2 numbers are right there but you see  how there's an inc termindex at the end of the doit procedure? Well  that means that termindex will be a 1 so when we come around to the beginning of the loop it does that again and walla! We skip 4 bytes to the next number.


So you see how the numbers go down in a row? The top 5 numbers are the bases and the next 5 are the exponents. And you see how there are 3 sets of 0's (bytes) before the next number? Well the number counts as 1 so all together that makes 4 bytes.

Now I'm going to talk about something called byteswapping. What byteswapping is is the way memory show's number's. It shows the numbers backward's. The reason for that is because of the way memory uses big endian and little endian. When you use big endian you store the most significant byte in the smallest address. When you use little endian it's the other way around you store the LEAST significant value in the smallest address.

 If you want to learn more go to this address: http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

Have you ever heard of an alias? It's like a fake name,example: You know how Dr.Seuss puts the name Theo lesieg on his books? That's an alias. I bet your wondering what does any of this have to do with what we're doing? Well I'll answer that question. You know these names that we're using for R.A.M such as termindex and count? They're aliases for ram addresses. But you know how bases and exponents have several numbers? The first number is the only one with an exact alias so to get to the other numbers you need to termindex into them.



Tuesday, September 10, 2013

PROcedures-The warning

On my previous post about procedures I showed you how to do several different operations or in other words a very long problem. Well there was a big problem with that..........REDUNDANCY!(see below)


mov ecx,3
mov ebx,4
call raiseToThePower
add total,eax

mov ecx,2
mov ebx,7
call raiseToThePower
add total,eax

Now the reason these 2 sections are redundant is because they are very similar,because if you look at it it all makes sense,both sections have 2 mov's then a call and last but certainly not least an add. The only things that are different between them are the numbers that are being moved,the 1st one has 3,and 4 and the 2nd one has 2,and 7.Now this equation isn't pretty but it get's worse when you do a program and you have several numbers so it's like evil Dr. Redundancy right there. Example:

        mov ecx,2
mov ebx,7
call raiseToThePower
add total,eax
mov ecx,3
mov ebx,5
call raiseToThePower
add total,eax
mov ecx,6
mov ebx,3
call raiseToThePower
add total,eax
mov ecx,9 1
mov ebx,2
call raiseToThePower
add total,eax

The problem we're trying to solve is 2^7+3^5+6^3+9^2. But look at all that redundant code!!!!!(I've highlighted the redundant code in different colors.)On my next post I'll show you how to fix this using arrays.

Thursday, September 5, 2013

PROcedures-The great switcheroo

Note to reader: if you haven't read my post called PROcedures you should read it before you read this post.


On my previous post about procedures I told you what a procedure is and what they do, on this post I'm going to show you something about them that allows you to use the same procedure to do 2,3,4,5,6 etc things it just depends on how much code you are willing to write,and sadly loops can't help with this,on my program I just did 2. Now I'm not saying you can do like you can do subtraction and addition in fact the "things" that I'm talking about are powers like: 3^3 + 2^4. Now I'm going to talk about input and output.  So all an input is is a number so look at 3^3 the 3's are input and the answer (27) is the output. Example:if you put 2 different types of materials such as wool or thread into a sweater machine they come out as a sweater. Or 2^4 7^8 or even 10^9.

.model flat, c
.stack 100h
.data
count dword 0
total dword 0
.code
doit proc


mov ecx,3
mov ebx,4
call raiseToThePower
add total,eax
mov ecx,2
mov ebx,7
call raiseToThePower
add total,eax

ret
doit endp

raiseToThePower proc
mov count,0
mov eax,1
again:
mul ecx
inc count
cmp count,ebx
jl again

ret
raiseToThePower endp


end


Now we get to the part I've been talking about: the part where you switch between the 2 different powers! If you look at the top part of the program you will see that I'm calling the raiseToThePower proc twice and in between those calls I'm doing some mov's. The only thing I'm changing with those mov's is the base (ecx) and the exponent (ebx). Now ebx and ecx are the input's that we've been talking so much about. And That's all there is to it really in between the call's change the base and the exponent.