Wednesday, November 27, 2013

The battle of AbstractionMan and Evil Dr.Details

        Ahoy there mateys! This day we shall plunge ourselves down into the sea of C++! I'm serious there so get ready to type! Now you know what we've been doing so far, we've been coding up vectors. But we've had trouble with Evil Dr.Redundancy.....and a new villain Evil Dr.Details. That's why I've prepared two procedures to get rid of them called add, and subtract so that instead of  having a ton of redundant code and a ton of details we can just do the code that's in the procedures and have a lot of abstraction. And for each of those two procedures we changed the're names to something special. On add we changed the name to  operator+ and on subtract we changed the name to operator- so instead of calling the procedures we can just say resultVector=leftVector+rightVector and it will call operator+ or we can say resultVector=leftVector-rightVector and it will call operator-. And guess what, that helps to get rid of details so we fired a shot at Evil Dr.Details. Aaaaaaaannnnnnnd that also gets rid of redundancy so we hit Evil Dr.Redundancy prrrrrretty hard. And AbstractionMan is now here to help us,and you can't have abstraction without the code to produce it so we've had some help from MemoryMan too. And how MemoryMan helps us is that instead of having six floats to keep track of we have 3 vectors called leftVector,rightVector,and resultVector. Now we're on a roll!

Here is my program:

struct Vector
{
float x;
float y;
};
Vector operator- (Vector left,Vector right)
{
Vector resultMan;
resultMan.x=left.x-right.x;
    resultMan.y=left.y-right.y;
return resultMan;
}
Vector operator+ (Vector left,Vector right)
{
Vector resultMan;
resultMan.x=left.x+right.x;
resultMan.y=left.y+right.y;
    return resultMan;
}
Vector leftVector;
Vector rightVector;
Vector resultVector;

void CollinsBasicVectorEquationCallback(const BasicVectorEquationInfo& data)
{
leftVector.x=data.x1*data.scalar1;
leftVector.y=data.y1*data.scalar1;
rightVector.x=data.x2*data.scalar2;
    rightVector.y=data.y2*data.scalar2;

if(data.add)
{
resultVector= leftVector + rightVector;
}
else
{
       resultVector=leftVector - rightVector;
}
}



Thursday, November 21, 2013

Vector!!!!......is back. dendenduuuuuuuuuun!

Direction
Magnitude
X components
Y components
                 Those are what vectors have. Ever watch Despicable Me? Well the rival bad guys name is vector,now your going to learn what a vector is. A vector is an arrow with direction,which is what "direction" the arrow points in,and magnitude,which is how long or short the arrow is. When you subtract 2 vectors then there is a result vector which is a different colored vector that connects the tips of the 2 other vectors to finish the shape. Then there's adding vectors. When you add vectors the 2nd vector starts from the tip of the 1st vector.  On a graph,vectors start from the origin (Middle point) for instance: when you subtract vectors then the vectors start from the origin and the result vector connects the tips of the 2 vectors. But when your adding vectors the 1st vector starts from the origin and the 2nd vector starts from the tip of the 1st one,and the result vector starts from the origin.

     Here's what some vector addition would resemble to:

    Here's what some vector subtraction would look like:

            The blue vectors are the vectors that I've been talking about and the red vector is the result vector. Now I'm going to talk about X components and Y components. The X component is how far right or left the vector points. And the Y component is how far up or down the vector points. When you add vectors you add the individual components,for example:if my X component for one vector was 3 and my Y component was 5 then if I add that to another vector that has a X component that's 4 and a Y component that is 2 then my result vector would be X 7 Y 7. And for subtraction it's the same thing.

Now I'm going to talk about scalar multiplication. There are 2 sliders that we haven't used yet called the scalars. The scalars are multiplication sliders that are usually at 1 because 1 times anything is exactly that value but If you change that 1 to a 2 then it will double the magnitude and change the direction of the vectors.
For example: here are my vectors with scalars of 1.

And here are vectors with scalars of 2.
I bet you can see the difference huh? Well even though they aren't the same size they are the same shape. Oh and make sure that you change the scalar for both vectors not just 1.

And last but not least here is my program:

float myFloats[6];

void CollinsBasicVectorEquationCallback(const BasicVectorEquationInfo& data)
{
myFloats[0]=data.x1*data.scalar1;
myFloats[1]=data.y1*data.scalar1;
myFloats[2]=data.x2*data.scalar2;
myFloats[3]=data.y2*data.scalar2;

if(data.add)
{
myFloats[4]=myFloats[0]+myFloats[2];
myFloats[5]=myFloats[1]+myFloats[3];
}
else
{
myFloats[4]=myFloats[0]-myFloats[2];
myFloats[5]=myFloats[1]-myFloats[3];
}

}  
Catch you later! 

Friday, November 8, 2013

Who's the New Guy?

Your going to hate me when I say this but this post is not about assembly.......it's about C++. Now C++ is a type of code that is very commonly used and instead of using the CPU to build it uses the compiler and (here's the funny part) C++'s disassembly builds as assembly so you can double check that it's working right. SO now I have to talk about how C++ works. For one thing C++ is a lot shorter than assembly because 1 C++ instruction can make several assembly instructions. Plus (surprisingly) it takes care of all the hard stuff for you. But don't get to carried away with it or else your assembly skills will drop. I'm still going to have you do some assembly programs after we go over some C++. C++ procedures and assembly procedures look almost nothing alike and as I said C++ procedures are a lot shorter than assembly procedures.
Here is a C++ procedure:

int main()
{
int total=0;
int termindex=0;
while (termindex < 6)
{
// calculate the next term value
int TermValue = raiseToThePower(bases[termindex],exponents[termindex]);

total = determineOperations(operations[termindex],total,TermValue);
termindex++;
}
std::cout << total;
}


Here is an assembly procedure:

doit proc
push eax
push edx
push ebx
push ebp
push ecx
sub esp,4               ; allocating termindex so we can use it 
mov ebp,esp
mov dword ptr [ebp],0
restart:
mov eax,4 ; calculate 4-byte offset into our data
mul dword ptr [ebp] ; Multiply term index
mov edx,eax ; Saving the 4-byte offset into edx

    ; calculate the next term value
mov ecx,bases[edx]
mov ebx,exponents[edx]
call raiseToThePower

    ; do the operation
mov ecx,eax
mov ebx,total
mov eax,operations[edx]
call determineOperations
mov total,ebx

; See if we need to repeat
inc dword ptr [ebp]               
cmp dword ptr [ebp],6
jl restart
pop ecx 
pop ebp
pop ebx
pop edx
pop eax 
add esp,4 ; deallocating termindex
  ret
doit endp


You see in assembly we need to define everything to the right register and it takes so much time and dudududududududu but in C++ it doesn't take so long and you don't have to worry about the registers because  C++ takes care of  them for us. And you don't have to worry about the "Leave No Trace"rule because C++ automatically does the pushes and pops. Now, you know how we carry certain values back and forth between the different procedures? Well how we do that in C++ is we say "return"and then the name of the dword that has the value that we want to pass to a different procedure. But still don't let it get the best of you because assembly is still pretty important.

Well the previous post (Assembly Town) had a program didn't it. This is that same program except in C++.



#include <iostream>

// We go from main to raiseToThePower
// with the bases and exponents (2,7) and go
// into the loop to multiply them together
// as many times as the exponent

int determineOperations(int operation, int currentTotal, int termValue)
{  
int newtotal;
switch(operation)
{
case 0:
newtotal=currentTotal + termValue;
break;
case 1:
       newtotal=currentTotal - termValue;
break;
case 2:
newtotal=currentTotal * termValue;
break;
}
return newtotal;
}      

int raiseToThePower(int base, int exponent)
{
int count=0;
int answerToThePower=1;
while (count <exponent)
{
answerToThePower=answerToThePower * base;
count++;
}
return answerToThePower;
}

int bases      [] = {2,3,6,9,4,2};
int exponents []  = {7,5,3,2,3,3};
int operations [] = {0,0,1,2,0,2};

int main()
{
int total=0;
int termindex=0;
while (termindex < 6)
{
// calculate the next term value
int TermValue = raiseToThePower(bases[termindex],exponents[termindex]);

total = determineOperations(operations[termindex],total,TermValue);
termindex++;
}
std::cout << total;
}