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