assemblyreturnz80

z80 assembler ret z; and a; ret


I have the following Z80 asm code (from pokemon legacy crystal)

CheckOnWater::
    ld a, [wPlayerStandingTile]
    call GetTileCollision
    sub WATER_TILE
    ret z
    and a
    ret

i was wondering what the point is of

ret z
and a
ret

as i understand, sub WATER_TITLE sets the z flag, it feels redundant to conditionally return, then and a just to set the z flag again and then to return.

can't i replace the whole three lines by just ret?

what am i missing?


Solution

  • The logical instructions clear the carry flag unconditionally.

    ret z returns with a cleared C (caused by sub WATER_TITLE), because the result of the subtraction is zero.

    and a clears the C flag, if sub WATER_TITLE left it set.

    None of the ret instructions change a flag.

    You might want to search the sources for the call of CheckOnWater to check whether the carry flag is important. Anyway, it might have been a requirement to return with a cleared carry flag.

    To understand such details on your own, I suggest to do a web research for appropriate documentation. You cannot work without.


    One could argue that ret z is not necessary since and a sets the zero flag in case the subtraction result is zero. Well, that is true.

    This code could have been written out of pure habit, or it could have evolved over time.

    Differences in other PSW flags could be relevant for a following BCD instruction daa, which is mostly improbable.