Fastest way to test

Door PingPong

Prophet (3495)

afbeelding van PingPong

26-08-2019, 20:10

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?

Aangemeld of registreer om reacties te plaatsen

Van GhostwriterP

Hero (527)

afbeelding van GhostwriterP

26-08-2019, 21:06

inc a and jr z,...

Van PingPong

Prophet (3495)

afbeelding van PingPong

26-08-2019, 23:01

GhostwriterP wrote:

inc a and jr z,...

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

Van Grauw

Ascended (8614)

afbeelding van Grauw

27-08-2019, 11:02

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

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.

Van Timmy

Expert (114)

afbeelding van Timmy

27-08-2019, 14:58

Grauw wrote:

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

Or just do a "ld a,255" afterwards, then you're sure a equals 255. Because by then you'll know it's 255. Smile 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. Running Naked in a Field of Flowers 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.)

Van GhostwriterP

Hero (527)

afbeelding van GhostwriterP

27-08-2019, 17:43

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

Van Danjovic

Expert (88)

afbeelding van Danjovic

27-08-2019, 18:40

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.