Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 01-29-2015, 01:23 PM   #1
Beta Member
 
Join Date: Jan 2015
Location: United Kingdom
Posts: 1
Unhappy Computer architecture help

Hi, i really need help with this computer architecture question. My college teacher said the example given is very useful so i have included that with the question in the image.

So far i have got...

IN A, (0xff)

XOR A, 0x1b

JP LOOP1

OUT (0xfb), 0xaa

I know its wrong but i cant get my head around it.



Above is the image containing the question and example. Also if you want the z80 instruction set, here is a link to it - Z80 instruction set - ClrHome

I know its complicated but any help is appriciated
Attached Images
File Type: png help.png (47.2 KB, 8 views)
__________________

jason123454 is offline   Reply With Quote
Old 01-30-2015, 07:06 AM   #2
Fully Optimized
 
Draygoes's Avatar
 
Join Date: Nov 2014
Location: United States
Posts: 1,653
Default Re: Computer architecture help

Welcome to CF!
That... actually scares me a little.
__________________

__________________
Will have a youtube channel up soon. Link will be here if I remember.
Draygoes is offline   Reply With Quote
Old 02-01-2015, 10:34 AM   #3
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,006
Default Re: Computer architecture help

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.
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 02-01-2015, 11:48 AM   #4
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,006
Default Re: Computer architecture help

With the printed example out of the way...
The first problem is you haven't included the example op-codes mentioned in the question. (If you hand in work using op codes from the whole instruction set when you're only meant to use a reduced instruction set then that's a problem!)
but lets assume that the ones you are allowed to use are at least the ones used in the example

So first you know you're writing a program to constantly check the port, running forever, so set-up a line label.

START:
then you know that you want to load data from port 0xFF
so use that IN function. load into A from port FF

IN a, (0xff)
IN a, (256) (in decimal rather than hex)

now that you have got that data you want to compare it to a value that you have in mind. Hex, ox1b, Binary 00011011 or decimal 27
so, you might have a cmp function to compare the two things, then branch out to some routine to set a flag.
but the easiest way is to think back to that truth table

0 + 0 = 0
0 + 1 = 1
1 + 1 = 1
1 + 1 = 0

so, if the numbers are different, there would be a one matched up with a zero, but if they are the same, (zeros and ones aligned) then the result will be zero

1010
0110
====
1100
Different numbers leave ones in the output

but
1010
1010
====
0000
Same numbers leave all Zero...

so, having loaded that number into A you want to do an XOR function on that read input. with the number 0x1b

XOR A,27

If the value that comes out IS zero then the Z flag will be set.
so just as with that example, if there is no zero flag we want to continue in a loop.

so jp NZ, start

now if we manage to get this far through the code (i.e the correct number is entered) then we want to set the output to 0xaa (binary 10101010 decimal 170) on port 0xfb (1111,1011 = 251)
so you want to set A to 170, and then output that to port 251

LD A,170
OUT (251),A


So the complete program:
Code:
START:	IN	A,(256)
	XOR	A,27
	JP	NZ,START
	LD	A,170
	OUT	(251),A
or in other words you're almost exactly right, the only thing you were missing was the line label at the start.
and the condition to only jump when the result of the previous operation (the XOR) was 0
(there is no NZ in your jp statement).
Quote:
LOOP1:
IN A, (0xff)

XOR A, 0x1b

JP NZ,LOOP1

OUT (0xfb), 0xaa
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Reply

Tags
architecture, programming, z80

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 09:00 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0