Friday, November 14, 2014

CopyC++at

                   On this post we're going to take our refactored programs and write them again in C++,so it's like the C++ is copying the assembly.



Here's the program from the COPS and ROBBERS post:




extern "C" void doit();

int myNumbers[] = { 7, 3, 8, 2, 2, 9, 8, 5, 7, 4, 8, 3, 8, 13, 8, 7, 4, 8 };

void main()
{
// doit();
int counter =0;
for (int i = 0; i < 18; i++)
{
if (myNumbers[i] == 8)
{
counter++;
}
}


}


               So first things first,we changed myNumbers into an int because this is C++,and eax is replaced by i,ecx is replaced by counter,instead of 68 we're going until 18 because to get to a different number you only need to add one because myNumbers isn't a dword it's an int,and also we say ++ in C++ which means increment. The for stands for the beginning of the loop,and the only thing we actually have to type is the cmp and CountingCounter because the curly braces take care of everything else so whoopee!!!!!!!!!!


Here's the program from the "I'm looking for someone really small............." post in C++:                                                    

extern "C" void doit();

int myNumbers[] = { 7, 3, 8, 2, 7, 9, 8, 5, 7, 4, 8, 3, 8, 1, 8, 7, 4, 8 };

void main()
{
// doit();
int i =0;
int first = myNumbers[i];
for ( i = 1; i < 18; i++)
{
if (first>myNumbers[i])
{
first = myNumbers[i];
}
}

}

                 On this program I replaced ebx with first,and set that to the first number of myNumbers so that the first number doesn't get used twice in the loop,and for that same reason i starts out in the loop as 1. In the loop we're saying the same thing as the assembly loop,simply "If the amount in first is greater than the amount represented by myNumbers[i] than move that amount into first and eventually that will give us the smallest number in myNumbers.

WELL TTFN TA TA FOR NOW!!!!!!!!!!!!!!!!!!!!




No comments:

Post a Comment