Shop OBEX P1 Docs P2 Docs Learn Events
sxsim bug, Z cleared after mov w,variable — Parallax Forums

sxsim bug, Z cleared after mov w,variable

Peter VerkaikPeter Verkaik Posts: 3,956
edited 2007-02-27 19:19 in General Discussion
Hi,
Appended test program has 2 variables, Ktimer and kCount initialized to 1 (at label initRegs)
The mainloop is
main
··;other tasks here
··bank·kernel
··dec·kCount···· ;decrements kCount from 1 to 0, so Z set
··mov·w,kTimer ;in sxsim Z·gets cleared after·executing this instruction
··snz· ; and thus kCount does not get reloaded
··mov·kCount,w
··jmp·main

I am using sxsim 2.08.06

regards peter

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-02-24 15:18
    Workaround:
    interchange
    ··dec·kCount···· ;decrements kCount from 1 to 0, so Z set
    ··mov·w,kTimer ;in sxsim Z·gets cleared after·executing this instruction
    so main becomes
    main
    ··;other tasks here
    ··bank·kernel· ;at this point Z=1
    ··mov·w,kTimer ;in sxsim Z·gets cleared after·executing this instruction
    ··dec·kCount···· ;decrements kCount from 1 to 0, so Z set
    ··snz· ;·now·kCount gets reloaded
    ··mov·kCount,w
    ··jmp·main


    regards peter
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2007-02-25 10:35
    Peter,

    according to the SX datasheets, a mov w, fr instruction affects the Z flag, i.e. when fr contains 0, the Z flag is set, else it is cleared. I have verified this with the SX-Key in debug mode, and I have also double-checked SXSim to make sure that it handles this the same way. So I don't think this is a bug in SXSim.

    BTW, vice-versa, the mov fr, w instruction does NOT affect the Z flag.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-02-25 12:20
    Guenther,
    You are right. I should have checked the datasheet.
    Funny, the mov w,#literal and mov w,m and mov w,<>fr that I use most to reload,
    do not affect the Z flag. So I assumed mov w,fr wouldn't either.

    regards peter
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2007-02-25 16:34
    Peter,

    I have no clue why the SX designers decided to let the mov w, fr instruction affect the Z flag, where other mov instructions don't (except those that increment, decrement, etc. a value "on the fly"). Well, we'll have to live with it smile.gif .

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-02-25 17:34
    This property of the MOV instruction goes back to the early PICs, upon which the SX is based. Microchip's instruction mnemonic for the MOV W,Reg instruction is MOVF, which can take two forms, depending on the destination, which can be either W or the source register itself. Setting Z in this instruction makes it possible to use it for testing a register without doing anything with it or to it. In fact, the SX TEST Reg instruction is just a MOVF Reg,Reg in disguise. The MOV Reg,W instruction, in Microchip mnemonics, is MOVWF, an entirely different instruction.

    When Parallax created new mnemonics for the PIC (which they then transferred to the SX), they made PIC/SX assembly language much easier to read, on the one hand, but obscured the underlying instruction set a little, on the other. What seems like a glaring asymmetry with the Parallax mnemonics, makes a lot more sense when the underlying instruction set is taken into account.

    -Phil

    Update: Incidentally, the way to move a register to W without setting any flags is:

            SWAP Reg
            MOV  W,<>Reg
            SWAP Reg        'Optional, to restore Reg to original state.
    
    
    

    Post Edited (Phil Pilgrim (PhiPi)) : 2/25/2007 5:59:25 PM GMT
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2007-02-27 10:33
    Phil,

    thank you for your explanation - this makes the difference between "mov w, fr" and "mov fr, w" clear, and - yes - under this light, it makes sense that "mov w, fr" affects the Z flag, where "mov fr, w" does not.

    Using the "double swap" method is a nice trick (except that it takes two instructions, or maybe three).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-02-27 19:19
    G
Sign In or Register to comment.