Few Z80 instructions change flag

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
Post Reply
User avatar
mrtinb
Posts: 1911
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Few Z80 instructions change flag

Post by mrtinb »

Coming from 6502 it seems like Z80 has a lot fewer instructions that change the flags.

My code was

Code: Select all

LD HL,(x)
JR NZ y
But LD HL,(x) does not change the flags. I assumed Z-flag was set if content of address x was $0000.

So I think the code could be

Code: Select all

LD HL,(x)
ADC HL,HL
JR NZ y
but that will set Z-flag if content of address x is $0000 and $8000 which is not what I'm looking for.
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Few Z80 instructions change flag

Post by XavSnap »

Code: Select all

LD HL,(x)
LD DE,FFFF; SET UPPER VALUE
ADC HL,DE ; Add HL+DE
JR C y ; TEST IF <>0 goto y
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
sirmorris
Posts: 2811
Joined: Thu May 08, 2008 5:45 pm

Re: Few Z80 instructions change flag

Post by sirmorris »

Code: Select all

ld a,h
or l
jr z,...
User avatar
mrtinb
Posts: 1911
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: Few Z80 instructions change flag

Post by mrtinb »

sirmorris wrote: Wed Mar 15, 2017 12:54 pm

Code: Select all

ld a,h
or l
jr z,...
Image
Attachments
thumbsup-transparent.png
thumbsup-transparent.png (1.08 KiB) Viewed 2640 times
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
User avatar
1024MAK
Posts: 5118
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Few Z80 instructions change flag

Post by 1024MAK »

mrtinb wrote: Wed Mar 15, 2017 9:40 am Coming from 6502 it seems like Z80 has a lot fewer instructions that change the flags.

My code was

Code: Select all

LD HL,(x)
JR NZ y
But LD HL,(x) does not change the flags. I assumed Z-flag was set if content of address x was $0000.
A lot of what the Z80 does in relation to flags is because the Z80 was made code compatible with the Intel 8080 CPU.

For the instructions like LoaD (LD) that don't change the flags, this can be an advantage in some situations.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
User avatar
PokeMon
Posts: 2264
Joined: Sat Sep 17, 2011 6:48 pm

Re: Few Z80 instructions change flag

Post by PokeMon »

Typically a load (LD) (or comparable mov - MOV) does not change flags at all processors I know (didn't use 6502 yet).
This behavior is normally unwanted and only arithmetic or logic operations do change flags.
The flag behavior is also different form instruction to instruction.

INC sets flags only for 8 bit register but not for the 16 bit registers.
ADD HL,reg does not change zero or sign flag while ADC HL,reg does.
The carry flag can be reset with any AND/OR instruction.
Typically AND A or OR A is used to check the accumulator contents and sets flags respectively without changing the value.
This can be used in programs effectively and keep flag states for a longer time.

This is the best reference for instructions and behaviors:
http://www.zilog.com/appnotes_download. ... 5Ca1pnPT0=

Fortunately they updated the document in september 2016 as the older version has several copy and paste bugs. I documented them Zilog a long while ago - probably the had time to correct these bugs. ;)
Post Reply