Saturday, November 29, 2014

Cleanup time....dun dun DUN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

On this post I'm going to show you how I cleaned up my previous program and explain why.

Here's my modified program:

.586
.model flat, c
.stack 100h
.data

; gcd(10, 4), gcd(31415, 14142), gcd(3241343, 948324),
; gcd(31243,68455), gcd(75324,729345), gcd(327256,647384)
left dword 10, 31415, 3241343, 31243, 75324, 327256
right dword 4, 14142, 948324, 68455, 729345, 647384

.code

doit proc
xor ecx,ecx
doagain:
push left[ecx]
push right[ecx]
call gcd

cmp ecx,24
jle doagain
ret

doit endp

gcd proc
mov eax,[esp + 8]       ; eax is left
mov ebx,[esp + 4]       ; ebx is right
again:
xor edx,edx                 ; zeroing out edx to prevent Integer overflow
div ebx                        ; divides left by right
cmp edx, 0     ; We are done when remainder is zero
je weAreDone

mov eax,ebx     ; we have a new left
mov ebx,edx               ; our remainder is our new right
jmp again                    ; repeat

weAreDone:
; ebx has the greatest common divisor
ret

gcd endp

end

First I moved my compare from the end of gcd into the middle,and the reason we do that is because if we leave the compare at the end then once we've got our greatest common divisor then every thing else after that would just be extra work which is why I moved the compare to right after the division problem,if the 2nd number is 0 we jump straight to the return,and if it's not we just continue and switch all the numbers. Also I changed it so that we only read off of the inputs and we don't change them at all,because it's like doit sent a letter to gcd and if we change the inputs it's like we drew a picture all over that letter. And now since we use the inputs a lot less we use the registers a lot more,and a good thing about that is using the registers is way faster. Also I changed my commentary.

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

Monday, November 24, 2014

On this post we're going to take the program from the previous post and change it so that it takes 4 sets of numbers and finds the gcd between them one by one.

Her'es the program:

.586
.model flat, c
.stack 100h
.data

;gcd(3241343, 948324), gcd(31243,68455),gcd(75324,729345),gcd(327256,647384)
left dword 3241343, 31243, 75324, 327256
right dword 948324, 68455, 729345, 647384

.code

doit proc
xor ecx,ecx
doagain:
push left[ecx]
push right[ecx]
call gcd
cmp ecx,16
jle doagain
ret

doit endp

gcd proc
mov eax,[esp + 8]      ; move the larger number of the first division problem into eax
repeatHere:
again:
xor edx,edx             ; zeroing out edx to prevent Integer overflow
mov ebx,[esp + 4]       ; hijacking ecx to represent right in the division problem
div ebx                 ; performing the first division problem of the loop
mov eax,[esp + 4]
mov [esp + 4],edx       ; preparing the numbers for the next time around
xor edx,edx             ; hijacking edx to represent the 0 in the compare
cmp [esp + 4], edx      ; comparing right to zero because we stop at when the second number becomes zero
jne again               ; if right isn't zero we jump back to the beginning

ret

gcd endp

end

As you can see it's almost the same as the previous program but with a few small changes,well actually big changes,such as the fact that I now have 2 procedures instead of 1 and that's because a procedure is supposed to do one thing and do it well,also left and right don't appear to be here,but they are. They've been pushed onto the stack which is why we use the stack pointer to represent them. You might think: "Why not just access the stack pointer,and the stack pointer plus 4?" Well we have to skip past the return address of the call to gcd. Also when we get back around to the point where we push left and right onto the stack we take them off the stack by adding 8 to the stack pointer which lowers the stack pointer down to the return address of that call so that we don't have to lower it past all those numbers at the very end kinda like this....

And the reason it's like that is because gcd isn't supposed to do everything at once,it's supposed to do one thing and do each part of it one at a time,kinda like this dentist cartoon,if you wait to floss for the entire year all the plaque builds up and then it's hard to get it all out,but if you do it consistently every day there will be a lot less plaque so it will be a lot easier.

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

Wednesday, November 19, 2014

The GREAT,the common,and the divi/sor.......

On this post I'm going to talk about how to get the greatest common divisor between 2 numbers. The greatest common divisor is the largest number they can each be divided by,for example the greatest common divisor between 2,and 3 is 1 because the numbers aren't related,on the other hand the greatest common divisor between 2 and 4 is 2 because the numbers are both even and multiples of 2. I know you're probably thinking " this isn't going to be too hard but too bad for you the numbers we're using are 31415,and 14142,dun dun dun!!!! You might still be thinking that because one is odd and one is even,but what if I told you that you had to show me the step-by-step work to get the greatest common divisor of those 2 numbers? Well that's what we're going to do,but luckily there's a mathematical equation we can use. gcd(31415,14142) = gcd(14142,31415 mod 14142): gcd means "the greatest common divisor of" and mod means you divide 31415 by 14142 and then replace 31415 with the remainder of that division problem so the problem would become (14142,3131) = (3131,14142 mod 3131) and then you'd keep going etc.etc. until your problem would look something like this:

Luckily I have a program that can do this,and here it is:

.586
.model flat, c
.stack 100h
.data

left dword 31415
right dword 14142

.code

doit proc
xor ecx,ecx ; zero out ecx to count our loop
again:
mov eax,left    ; move the larger number of the first division problem into eax
mov edx,right   ; moving the larger number of the next division problem into a position where we can move it into left
mov left,edx    ; executing the previous comment
xor edx,edx     ; zeroing out edx to prevent Integer overflow
div right       ; performing the division problem
mov right,edx   ; preparing the numbers for the next time around
inc ecx         ; noting that we have gone around so we can add it to how many times we have already gone around
cmp ecx,10      ; comparing ecx to ten because we stop at 10
jne again       ; if ecx isn't ten we jump back to the beginning

ret

doit endp

end

As you can see I have the code commented and ready to go,so I'll explain how it works: So the first thing I did was grab two pieces of memory,I named one left,and the other right,and the reason I did that is because I needed two places to store the numbers that wouldn't get erased every frame. Then we xor ecx because we need it to be our counter because we need to go around 10 times. Then we move left into eax because eax represents the larger number of the division problem,and then we move right into edx because right contains the larger number of the next problem which needs to be placed in left and you can't move memory into memory,only memory into register or register into memory. Then we move edx into right,and after that we xor edx to prevent Integer overflow. And after that we do the division problem,take the remainder,and move it into right. Afterwards we inc ecx to keep track of how many times we have gone through the loop and eventually we stop at 10,and jne back to again.

Well there's an itty bitty problem with this,our chart is in decimal,but when we run the code the numbers are shown in hex,so it's pretty hard to keep track of where you're at in the program
or if the numbers are right,well I've got a solution:

I made you a hex chart,ta da! This chart will help you keep track of what line you're at and if you have the right numbers.

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

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!!!!!!!!!!!!!!!!!!!!

Tuesday, November 11, 2014

To refactor,or not to refactor,that is the question...........

On this post  I'm going to show you how I refactored both the program from the previous post and the post before that one too. Refactoring is cleaning up and simplifying a program so that it's less headache to keep track of.

Here's the program from the previous post refactored:

extern "C" void doit();

void main()
{
doit();
}

.586
.model flat, c
.stack 100h
.data

myNumbers dword 7,3,8,2,2,9,8,5,7,4,8,3,8,1,8,7,4,8

.code

doit proc
xor eax,eax
mov ebx,myNumbers[eax]

again:
cmp myNumbers[eax],ebx
jge repeatIfNeccesary
mov ebx,myNumbers[eax]
repeatIfNeccesary:
cmp eax,68
jle again
ret

doit endp

end

The first thing I changed was that I got rid of ecx's involvement in the program,which seems kinda weird,but the reason I did it was because I realized I didn't need a register to represent myNumbers because myNumbers can represent itself. The second thing I changed was instead of jumping to switcheroo I put it in the loop and said jump greater than or equal to repeatIfNeccesary so instead of jumping to the part where we store the smallest number we jump past it if we currently don't need to do it.

Here's the program from the post before that one refactored:

extern "C" void doit();

void main()
{
doit();
}

.586
.model flat, c
.stack 100h
.data

myNumbers dword 7,3,8,2,2,9,8,5,7,4,8,3,8,13,8,7,4,8

.code

doit proc
xor eax,eax
xor ecx,ecx
again:
cmp myNumbers[eax],8
jne repeatIfNeccesary
repeatIfNeccesary:
cmp eax,68
jle again
ret

doit endp

end

On this program I changed from jumping to CountingCounter to jumping past the instructions inside it if we currently don't need to use them just like in the other program.

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

I'm looking for someone really small...................................

We're going to talk about how to find the smallest guy in a crowd. I tweaked my program from the last post,so now instead of finding out how many of the numbers are 8's,we're going to find out which one of the numbers is the smallest.

Here's the program:

The C++ part:

extern "C" void doit();

void main()
{
doit();
}

And the assembly part:

.586
.model flat, c
.stack 100h
.data

myNumbers dword 7,3,8,2,2,9,8,5,7,4,8,3,8,1,8,7,4,8

.code

doit proc
xor eax,eax
xor ecx,ecx
mov ebx,myNumbers[eax]
again:
mov ecx,myNumbers[eax]
cmp ecx,ebx
jl switcheroo
back:
cmp eax,68
jle again
ret
switcheroo:
mov ebx,ecx
jmp back

doit endp

end

So get all that typed in and I'll explain how it works.

This is how it works,just like the program from the last post we xor eax by eax,and ecx by ecx,and also we move myNumbers into ebx,except we only do that once because of the again. Then as always we add 4 to eax so that we can access the next number of myNumbers,except not because we want to place the next number of myNumbers in ebx,we want to place it in ecx,and here's why.
We take the next number of myNumbers and compare it  to the number currently in ebx and if it is smaller than the number currently in ebx then we jump to switcheroo. In switcheroo we move the number currently in ecx into ebx so that we always  have the smallest number we've seen so far in ebx. Then we jump to back compare eax to 68 and if it is less than or equal to 68 we jump back to again.

WELL TTFN TA TA FOR NOW!!!!

Monday, November 10, 2014

COPS and ROBBERS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

We're going to make a program that finds all of the 8's in a jumble of numbers and then tells you how many of them there are, sorta like cops investigating a bunch of people and trying to find out how many of them are criminals.

Here's the program:

1st the C++ part:

extern "C" void doit();

void main()
{
doit();
}

And then the assembly part:

.586
.model flat, c
.stack 100h
.data

myNumbers dword 7,3,8,2,2,9,8,5,7,4,8,3,8,13,8,7,4,8

.code

doit proc
xor eax,eax
xor ecx,ecx
again:
mov ebx, myNumbers[eax]
cmp ebx,8
je CountingCounter
back:
cmp eax,68
jle again
ret

CountingCounter:
jmp back

doit endp

end

So get all that typed in and then I'll explain how it works.

The program starts out in C++, and from there calls doit an assembly procedure.Then we create MyNumbers which is our jumble of people. So how do we find out how many are criminals (8's)? Well first we need 2 registers,eax,and ecx to be completely zeroed out because we're going to use one: eax to access MyNumbers correctly,and we'll use ecx to count how many criminals (8's) there are.Now we say mov ebx MyNumbers and you see how I've got eax in the brackets? Well that's because we're using eax to access MyNumbers,and since eax is currently 0 we will access the first number in MyNumbers. Now since the criminals are 8's we compare ebx to 8 and if it is 8 we would do that jump equal to (je) but I'll get back to that later. That "back" is where we jump to from the je so don't worry about it right now,so next we add 4 to eax because every number is a dword and a dword is 4 bytes. Then we compareeax to 68 because that's the number we stop at. Then we do the jle which means if eax is less than or equal to 68 jump back to again. Now I'll talk about what would happen if ebx was an 8. We would do the je  down to CountingCounter. Then we would add 1 to ecx because we're using it to count how many 8's there are, Then we jump to back.

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