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

No comments:

Post a Comment