## Tuesday, December 23, 2014

### Recursion Surgeon

Hilo,Halo,Howdayado! On this post I'm going to teach you about recursion. What is recursion you might ask? Well I'll tell you. You know how in our last program we used a loop? Well we're going to replace that loop with recursion,which is when a procedure calls itself instead of doing some sort of jump,which takes us back to the beginning of the procedure,so since we don't choose where we jump back to,we're going to have to make a few changes to our program.

Speaking of which, here's the program:

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

; gcd(10, 4), gcd(28, 21), gcd(12, 8),
left dword 11, 28, 12
right dword 4, 21, 8

.code

doit proc
push eax
push ebx
push ecx
xor eax,eax
xor ecx,ecx
doagain:
sub esp,4
push left[ecx]
push right[ecx]
call gcd
pop ebx
cmp ecx,12
jl doagain
pop ecx
pop ebx
pop eax
ret

doit endp

gcd proc
push eax
push ebx
push edx
mov eax,[esp + 20]       ; eax is left
mov ebx,[esp + 16]       ; ebx is right
cmp ebx, 0 ; We are done when right is zero
je weAreDone
xor edx,edx
div ebx
mov eax,ebx
mov ebx,edx
sub esp,4
push eax
push ebx
call gcd
pop eax
weAreDone:
; eax has the greatest common divisor
mov [esp + 24],eax
pop edx
pop ebx
pop eax

ret 8

gcd endp

end

We don't change anything in doit so let's just talk about gcd and what we change in there. Once we get to gcd our stack will look like this:

I'll explain the stack from top to bottom: At the bottom of the stack we have the return address that takes us back to main because that's the very first thing we do when we go into our program,next we have the 3 saved registers for our no trace coding,but just because I called them rand doesn't mean they're just a bunch of random numbers,they are those numbers for a reason,I just called them rand because they're always going to be a different number so I can't confirm them with a fixed value. Next we have our little slot for our return value,and our inputs left and right,and at the top we have the return address back from gcd to doit.

The first time through gcd we have our saves which still aren't a fixed value for our procedure,our return value slot,our inputs,and our return address:

The second time we have our saves which are procedure values this time around,our slot,inputs,and address:

The third time we have our saves,slot,inputs,and address:

The fourth time we only have our saves because we're at the base case which is when right is 0 and since we do the compare right after the saves we jump down to where we unwind the stack:

The first thing we change in gcd is we get rid of our jump address,because we don't need it anymore,and the next thing we change is before gcd calls itself we do the same thing that we do in doit just before we call gcd,we subtract 4 from the esp to make a slot for our return value,and push the inputs onto the stack so that we can access them through the esp. Another thing I'd like to point out is that since we call gcd from inside itself when we hit the return we return to the instruction right after the call so then we hit the return again and again,meanwhile we're popping a ton of values off the stack. You might be thinking: "Wait a minute,if we do that aren't we going to get a ton of random values?" well the answer is no because since we do the same thing that we do in doit before the call and since gcd calls itself and we go back to the beginning we do all those pushes,while doing these instructions we are doing something called unwinding the stack and eventually we get to the return that takes us back to doit and then we switch to the next set of numbers and call gcd. Well ya that's all cool,but the true beauty of recursion is that (especially with gcd) when you go back to the beginning if you've set up the code right the inputs are ready,the stack's ready,everything's ready to go around again,take the gcd of 11,and 4 for instance,which is the one I'm showing you in the pictures.

1st time gcd(11,4) = gcd(4,11 % 4) = gcd(4,3)
2nd time gcd(4,3) = gcd(3,4 % 3) = gcd(3,1)
3rd time gcd(3,1) = gcd(1,3 % 1) = gcd(1,0)
4th time gcd(1,0) = 1

The first time around 11,and 4 are on the stack,so we take them and mod them together which turns the 11 into a 3,then we push our numbers onto the stack again and go to the second time and keep changing the inputs on the stack etc.etc.etc.,and that's the beauty of it: we have our inputs,mod them together,switch them go again.

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

## Thursday, December 4, 2014

On this post I changed the program so that it brings the gcd of every set of numbers and adds them together,and also performs no-trace-coding which means we save the registers and restore them so it's like the procedure that changed them was never called.

Here's my program:

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

; gcd(10, 4), gcd(28, 21), gcd(12, 8),
left dword 10, 28, 12
right dword 4, 21, 8

.code

doit proc
push eax
push ebx
push ecx
xor eax,eax
xor ecx,ecx
doagain:
sub esp,4
push left[ecx]
push right[ecx]
call gcd
pop ebx
cmp ecx,12
jl doagain
pop ecx
pop ebx
pop eax
ret

doit endp

gcd proc
push eax
push ebx
push edx
mov eax,[esp + 20]       ; eax is left
mov ebx,[esp + 16]       ; 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
mov [esp + 24],ebx
pop edx
pop ebx
pop eax

ret

gcd endp

end

You might notice something that wasn't there before,a subtraction by four from the esp. I put that in because when we subtract 4 from the esp it goes up four bytes which is one space up which opens up a little spot,and before that we push eax,ebx,and ecx to the top of the stack. When we do that we are saving the values that those registers have when we first get to doit so that we can restore them later for main. Then we take eax and xor it because we want to use it to add up the gcd's and ecx is our indexer. Then we push left and right to the top of the stack and call gcd placing the return address at the top of the stack. Once we get to gcd we save the registers that we change in gcd for doit and then place left and right in eax and ebx. Once we get to weAreDone after the other parts of the procedure we place the gcd (which is currently in ebx) into that empty slot we created with the subtraction and pop the rest of the registers values in doit back into them,and that's very important because for example: even though eax is used in gcd it's also used in doit as our sum so we have to restore it to the amount that it was in doit or else we're stomping on our sum,and then return to doit leaving right at the top of the stack. Once we get back to doit at the instruction after the call we add 8 to the esp which leaves the return value (our gcd) at the top of the stack. Then we pop it into ebx and add that to eax which in doit is the sum of all of the gcd's. Then when ecx (our indexer) reaches 12 we go down to the pops and restore eax,ebx,and ecx to their original values before we even called doit.

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

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