Z80 assembly language - sign flag after INC r

1k views Asked by At

one thing with Z80 assembly language bothers me. Does sign flag always represent the sign of the value of the A register? I mean, when I run 'INC B', the result goes back to B, so is the sign flag taken from the value of the A or B register? Thanks in advance

4

There are 4 answers

1
GJ. On

Under Z80 are all registers (A,B,C,D,E,H,L) independent, so any arithemtic or binary operation will affect to flags in F register.

Check Z80 datasheet page 160 for inc r affected flags.

1
Amos On

This page: http://icarus.ticalc.org/articles/z80_faq.html seems to indicate that the sign flag represents the result of any calculation not just those on the A register.

0
Harper Maddox On

The sign flag doesn't always represent A. Overflow (inc on 255), bitwise operations (shift, etc), and logical operators affect all the flags.

However, Zilog set up each register differently, so some operations affect flags with a particular register and not another one. A common optimization is "XOR A", which sets the sign flag and effectively compares A to zero. I'm pretty sure it only works on reg A.

The aforementioned Icarus doc explains the flags and there used to be another even smaller text doc that explains the flags. But last time I saw that was 10+ years ago and i have no idea where it would be.

0
introspec On

The flags in Z80 are always referring to the last operation that modified them. This behaviour could be useful or not so useful. Just to give you several specific examples:

ld l,0           ; L is non-zero, but loading does not affect flags,
                 ; so their state is undefined at this stage
xor a            ; this resets A to 0; affected flags are NC, Z
ld h,a           ; we still have NC, Z
inc hl           ; HL is now equal to 1, but inc/dec of register pairs does
                 ; not affect any flags at all
dec a            ; A is now 255 (i.e. -1). we have NZ (expectedly),
                 ; however flag C is still off (intuitively unexpectedly),
                 ; because DEC of individual registers does not affect state of flag C
add a,1          ; at the same time, addition modifies both Z and C,
                 ; so after this A=0 again and we have flags Z and C both on

Generally speaking, this means that sometimes you can construct more complex conditions that keep track of state of the flag C, while doing other operations that modify flag Z without modifying flag C. This also means that you must remember for each operation, which flags it modifies.

The best online table that I know of with all this information is located at http://clrhome.org/table/