Shop OBEX P1 Docs P2 Docs Learn Events
CALL / RET flags preserve — Parallax Forums

CALL / RET flags preserve

Hello,

Time for another explanation:

The documentation say that CALL pushes C and Z into stack along with address, and RET pops them back, however if you run this code, it seems that the flags are not preserved:

                mov     t1, #$08
                cmp     t1, #$08    wz  ' Z=1
        if_z    call    #func           ' reset Z ?
        if_z    mov     t1, #$FF
        if_nz   mov     t1, #$00
                debug(uhex_byte(t1))
                jmp     #$

func            cmp     t1, #$FF    wz  ' Z=0
                ret

t1              long    0

It should set t1=$FF but it is $00 meaning that Z is reset by the called function. To prove it, comment the wz effect on the func row, and the assignment is correct.

Also have some doubt about_ret_ because while the description for ret(instruction) say that the flags are restored (but apparently not), the description for_ret_ (prefix) does not!

What I'm missing ?

Comments

  • AribaAriba Posts: 2,682

    I think you need to write:

    func            cmp     t1, #$FF    wz  ' Z=0
                    ret     wcz
    

    to restore the flags to the state they were at subroutine entry.

    Andy

  • RET WCZ restores C and Z.
    RET WC restores C only.
    RET WZ restores Z only.
    RET does not restore C or Z.
    _RET_ does not restore C or Z.

  • Yes, of course I missed the effects!
    It is also a logic behaviour because someone may want the calling function to change the flags in some cases.

    Thanks, sorry for the silly questions, I'm having lot of difficulties interpreting this documentation.

Sign In or Register to comment.