Code:

Loop1: IN A,(59)
XOR A,47
LD H, 100
Loop2: DEC H
JP NZ,LOOP2
OUT (104),A
JP LOOP1

the first thing to know is that the line labels LOOP1: and LOOP2: are exactly that, line labels, they will be used to setup places that you want to goto, or jump to throughout the program.

So at the sdtart of the program there is a line label written as loop1, this could have just as easily been called start: or line1:

the way that the op code works is that there is a function and two arguments passed.

the first opcode is IN, this means get some input, the Z80 is a chip, so the input has to be read from pins, the pins are arranged into ports... and those ports have names, or numbers, some kind of designation.

when you use the input, function (IN) you need to provide two pieces of information, the first is the register that you'd like to work with, (where the data will end up, and the second is the port that you want to get data from.

IN A,(59) quite literally say get whatever data is being presented to the wires that are grouped to form port59 and put that data into A, (where A is often referred to as the accumulator.

so that first line gets information, (probably an 8 bit number) and puts that into the register that we call A

the second line performs an XOR function. with that data and a key.

XOR is exclusive or.

a normal OR gate has a truth table where if input A or input B is true, the output is true. but also if both inputs are truen then the output is true:

A, B, Q

0, 0, = 0

0, 1, = 1

1, 0, = 1

1, 1, = 1

An Exclusive OR function deals with exclusive, inputs the inputs are tied to one another it's like saying IF a if true, and B is not true then output will be true, OR if A = 0 and B = 1 then Q = 1, but if A and B = 1 then Q = 0

q = A xor B

can be expressed as (in boolean):

q = ((A . /B) + (/A . B))

the truth table looks like this:

A, B, Q

0, 0, = 0

0, 1, = 1

1, 0, = 1

1, 1, = 0

So with XOR out the way.

The next line says, take the data in accumulator A, perform an XOR function with that data, and the number 47, then put it back into accumulator A ready to be used later.

For the sake of example assume that the data gathered from port 59 in the first line was the number 84.

in binary this is expressed as 01010100, the number 47 (the exctrytion key) is expressed as 00101111

So 84 xor 47 gives a result like this

01010100 (84)

00101111 (47)

=========XOR

01111011 (123)

So now the number that was in A, has changed from 84 to 251.

Now you want to set up a loop.

So you invoke a new register and you put a number into it. (100)

LD H, 100

Load register H with number 100

Then you set up a point to loop back to (loop2

Then you say take that data, minus 1.

DEC H

(decrement H)

is it zero? no, then go back to loop2 line marker, take 1, is it zero? so on and so forth.

(so you see why the loop marker is where it is?

if it was the line before you'd be going

Make h 100, take 1 off H, is it zero? then go to the start, make H 100, take 1 is that zero? (it's be an infinite loop it could never end).

I said is it zero, but how?

there is no obvious compare (CMP) function, so how is this being done?

Well the line that's invoking that decrement DEC function has half the responbibility.

that line is taking the number contained in H and subtracting 1, again and again in a loop, eventually it will get to zero, when it get to zero it will set a single bit in another internal register to 1, that register is the Zero flag.

so when H gets down to 0, h=0 AND Z = 1

the line jp NZ,LOOP2

means, Jump, (if Not Zero) to Loop2

in plain english, if the zero flag is not set, then jump to that line label. (where when you move away from opcode numonics those line labels will actually be memory addresses in the program stack).

so while that xero flag is not set, the progam keeps going through it's loop, wasting up time.

eventually you get to zero, the zero flag is set to true, and so the program no longer jumps back to loop2.

then we use the OUT function, this works just the same as the in function.

it's saving move data to here, from here.

when we used in we said move data to A from port (59)

now we're saying move data to port 104 from A

then at the end, we start again, jump from this location to the line label LOOP1, there are no conditions this time, it's just saying jump whatever happens.