# Computer architecture help

Status
Not open for further replies.

#### Jason12345

##### Newbie level 5
Hi, i have a question on computer architecture and its very difficult. My college teacher said the example given has alot of what we need to figure it out so ill post that along with the question as an image

So far i have...

Code:
IN A, (0xff)
XOR A, 0x1b
JP LOOP1
OUT (0xfb), 0xaa

I know its wrong but i cant get my head around it, does anyone have any ideas? anything is appreciated

also if you need the z80 instruction set, here is a link to it - https://clrhome.org/table/

##### Super Moderator
Staff member
You need to check that the value produced by the XOR is 0 (i.e. matches 0x1b) if its 0 you output the status byte 0xaa, otherwise you jump back to the IN A line.

I'm sure the z80 has some sort of compare of some sort...hmm looks like a cp instruction and then jp instruction that looks at the flags.

I'll admit I've never written z80 assembly so there might be a better way to do this.

Jason12345

### Jason12345

Points: 2

#### Jason12345

##### Newbie level 5
yes my college teacher said about cp instructions

#### Jason12345

##### Newbie level 5
im not sure if my replys are working but my college teacher mentioned the cp instructions and that i should use it.
Does anyone know how specificly?
to my understanding it compares 2 value by subtracting one from another or something, im really not sure

##### Super Moderator
Staff member
The following link shows how to use the CP instruction.
https://z80-heaven.wikidot.com/instructions-set:cp

You want to use CP n n is your 0x1b constant, which means you don't need the XOR as it's not needed.

So what you want to do is the following:

2. compare to 0x1b
3. jump on Z set to (5. output status 0xaa)
4. fall through, Z not set jump back to (1. read input)
5. output status 0xaa

It would probably be easier if I drew a flow chart, but I don't have the time.

Jason12345

### Jason12345

Points: 2

#### Jason12345

##### Newbie level 5
Thankyou for the help but how would that be written, like this?

IN A, (0xff)
CP A, 0x1b
OUT (0xfb), 0xaa
JP LOOP1

##### Super Moderator
Staff member
you left out the jump on a Z (i.e. stands for zero result) after you compare.

You should have looked at this page for the JP instruction options. Besides that you didn't follow the exact ordering I posted in #5. Read that until you understand what I'm doing.

Jason12345

### Jason12345

Points: 2

#### Jason12345

##### Newbie level 5
Ok thankyou very much for your time i am determinded to get this right

IN A, (0xff) - ok this reads the input from port 0xff

CP A, 0x1b - this compares that value to 0x1b

JP Z, 4 - this jumps to the output on line 4 i think

OUT (0xfb), 0xaa - this will output the value 0xaa to port 0xfb

JP LOOP1 - this will loop back around

im not sure what "fall through, z not set jump back to 1. read input" means or the instruction to do it,
im really sorry if its fustrating when im not getting it right but im trying my hardest, please bare with me, i am very thankful of you helping me

##### Super Moderator
Staff member
JP Z, 4 - this jumps to the output on line 4 i think
You don't want a hardcoded address, use a label, let the assembler figure out the adddress to put in the instruction.

Jason12345 said:
im not sure what "fall through, z not set jump back to 1. read input" means or the instruction to do it,
im really sorry if its fustrating when im not getting it right but im trying my hardest, please bare with me, i am very thankful of you helping me
When a jump instruction isn't taken (jump condition is not met) the program execution "falls through" (i.e. goes to the next instruction), so following the jump instruction you want to add code that say what you do when the input byte does not match 0x1b. See line 4 in post #5.

so as an example:
Adding two number that will result in a carry...
Code:
START:    IN A, (0xff)        # read input from port 0xff
?? ?, CARRY         # if there is a carry (i.e. A+0x80 has a result with 0x1??)
OUT (0xfb), A       # spit out the sum on port 0xfb when there isn't a carry
?? START            # look for more inputs
CARRY:    OUT (0xfb), 0x01    # spit out carry
OUT (0xfb), A       #   then the sum on port 0xfb when there is a carry
?? START            # look for more inputs

I'm leaving it up to you to figure out what instructions to use where the ?s are.
Pardon any syntax errors, I have no clue what the assembler requires. You should be able extrapolate what you are trying to implement by looking at this code.

Last edited:
Jason12345

### Jason12345

Points: 2

#### Jason12345

##### Newbie level 5
You don't want a hardcoded address, use a label, let the assembler figure out the adddress to put in the instruction.

ok does that mean i should replace JP Z, 4 with JP C, CARRY like you did in the example

im not smart when it comes to stuff like this, it confuses me alot. will you please let me know if i did anything wrong?

IN A, (0xff) - ok this reads the input from port 0xff

CP A, 0x1b - this compares that value to 0x1b

JP C, CARRY - this jumps to the output on line 4 i think

JP START - so after the jump condition isnt met, it will loop back around to 1 and try to read the input again

OUT (0xfb), 0xaa - this will output the value 0xaa to port 0xfb

JP START - this will loop back around

#### Allen6502

Ok thankyou very much for your time i am determinded to get this right

IN A, (0xff) - ok this reads the input from port 0xff

CP A, 0x1b - this compares that value to 0x1b

JP Z, 4 - this jumps to the output on line 4 i think

OUT (0xfb), 0xaa - this will output the value 0xaa to port 0xfb

JP LOOP1 - this will loop back around

im not sure what "fall through, z not set jump back to 1. read input" means or the instruction to do it,
im really sorry if its fustrating when im not getting it right but im trying my hardest, please bare with me, i am very thankful of you helping me

I think it is more like this..........

Code:
LOOP1: IN A, (0XFF)    ;GET INPUT IN ACC
CP A, 0X1B     ;COMPARE WITH 0X1B
JNZ    LOOP1   ;IF NOT SAME THEN GO BACK
LD A, 0XAA     ; LOAD ACC WITH 0XAA
OUT A,(0XFB)  ;OUT 0XAA TO PORT FB
END

Sorry my Z80 memory is very, very rusty.

Allen

Points: 2

### Jason12345

Points: 2

##### Super Moderator
Staff member
I give up...here. study it...understand it.

Code:
START:    IN A, (0xff)      # port 0xff => A
CP 0x1b           # compare A with 0x1b (sets/clears Z flag)
LD A, 0xaa        # thanks Allen, didn't look at these instructions
OUT A, (0xfb)     #   ...Z==1, output 0xaa
NOTEQUAL: JP START          # jump back to START
I noticed an optimization with jumping back to the start directly if the Z flag is clear.
If the Z flag is set then output 0xaa and jump back to the start.

You should probably learn how to draw up a flowchart so you can understand the logic involved.

- - - Updated - - -

Looks like I have an extra jump never claimed to be a software type. I actually can't stand writing software, and tolerate writing Perl scripts. ;-)

...so don't refer to my Verilog/VHDL as software :evil:

Last edited:

Points: 2

### Allen6502

Points: 2

#### Jason12345

##### Newbie level 5
thankyou both of you for helping me, i will show my computer architecture teacher and see what he thinks. Without u guys im sure i wouldnt of got this so your help is very much appriciated :smile::thumbsup:

#### Allen6502

thankyou both of you for helping me, i will show my computer architecture teacher and see what he thinks. Without u guys im sure i wouldnt of got this so your help is very much appriciated :smile::thumbsup:

You're welcomed.

I checked with my Z80 books just now and found some mistakes in my code. The correct one is as below....

Code:
LOOP1: IN A, (0xFF)   ;GET INPUT IN ACC
CP 0x1B        ;COMPARE WITH 0x1B
JP NZ, LOOP1   ;IF NOT SAME THEN GO BACK
LD A, 0xAA     ;LOAD ACC WITH 0XAA
OUT (0xFB),A   ;OUT 0xAA TO PORT FB
END

See that CP compares a byte, register, (HL), (IX+d) or (IY+d) against Acc. So A in the instruction is optional. And I must have mixed up JP NZ with JNZ of 8051.

Allen

Jason12345

### Jason12345

Points: 2

#### Jason12345

##### Newbie level 5
i showed my teacher today and he said everything was correct, thanks again everyone :thumbsup:

Status
Not open for further replies.