# Fastest way to test

Hi, what is the fastest way to test if after a logical bitwise operator the accumulator contains 255?
Like this
Ld a, 127
Or 128

Instead of cp 255 then jp or Jr what I could do?

Entrar ou registrar-se para comentar

inc a and jr z,...

GhostwriterP wrote:

inc a and jr z,...

forgot to say that a should not be destroyed....

It isn’t destroyed, the callee can do “dec a” to restore it can’t it?

I think cp 255 is the fastest way otherwise. Keep the value 255 around in a register for quickness.

p.s. Worth mentioning whether either of those values 127 or 128 are constant or both are fully variable, which might open or close the door to possible optimisations (although I don’t think so in this case). Some properties of the input values could be exploited too, e.g. parity, or if the bits never overlap it could xor with the complement. More context of the algorithm could help too, e.g. the “dec a” restore can perform better than cp 255 if the branch is taken in the unlikely case.

p.p.s. Excellent Zilog Z80 documentation of the P/V flags for logical operations:

XOR s: “P/V is set if parity even; reset otherwise” <— alrighty then
OR s: “P/V is set if overflow; reset otherwise” <— eh, overflow how?
AND s: “P/V is reset if overflow; reset otherwise” <— what?

And people complain about Yamaha’s documentation :D.

Grauw wrote:

It isn’t destroyed, the callee can do “dec a” to restore it can’t it?

Or just do a "ld a,255" afterwards, then you're sure a equals 255. Because by then you'll know it's 255. Also, "dec a" will change your flags again, what may not be the intention.

That may sound strange, but lot of times you don't really want to check if a equals 255. Especially if you're probably doing a loop for 255 times.

The fastest way in those cases, is just NOT checking. Instead, just unroll the loop 255 times.

Personally I'm often really lazy and I'd use "djnz" instead. However you can't use register A with that.

EDIT: (Sorry if this sounds like advanced assembly programming.)

or also cp r, jr z,... where r = b,c,d,e,h or l = 255

let's use some TRIZ...
invert your logic to have your bitwise operation end in 'zero' on your desired condition then use a 'test for zero' instruction.