Shop OBEX P1 Docs P2 Docs Learn Events
Does JMP always set the Z flag? — Parallax Forums

Does JMP always set the Z flag?

jazzedjazzed Posts: 11,803
edited 2011-01-17 21:49 in Propeller 1
I see code like this sometimes:

jmp #label wz

Does it always set the Z flag? The latest datasheet/manual do not document this as far as I can tell.

Guess I get to write some test code. :grumble: :P ... these new emoticons are awful.

Comments

  • ericballericball Posts: 774
    edited 2011-01-17 10:50
    The datasheet says "Result = 0", which I would take to be the value stored into D[8..0] which is PC+1; and since PC should never be $1FF then the Z flag (if WZ specified) would be always be cleared.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-01-17 10:52
    I coded the jmp instructions as follows in SpinSIm.
    	    case 7: // ret, jmp, call, jmpret
    	    cflag = ((uint32_t)value1) < ((uint32_t)value2);
    	    result = (value1 & 0xfffffe00) | pasmvars->pc;
    	    pasmvars->pc = value2 & 0x1ff;
    	    zflag = (result == 0);
    	    break;
    
    I haven't verified it, but I believe that's how it works.

    Dave
  • jazzedjazzed Posts: 11,803
    edited 2011-01-17 12:20
    So if JMP dest == 0 at beginning of instruction and result is not to be written,
    (JMP = JMPRET ... NR) I guess that means Z will be set with WZ.

    I'd say that's an acceptable answer.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-01-17 12:29
    The Z flag is set or not, according to the computed value of the result, regardless of whether the result is actually written. And the result value of a JMP/JMPRET instruction is always the address of the instruction following the JMP/JMPRET. So Dave is right: it could only be zero if the JMP/JMPRET were at address $1ff. But even then, the result value, as interpreted by WZ, could conceivably be $200.

    -Phil
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-01-17 13:06
    It gets even more complicated. The JMP instruction is the same as CALL/JMPRET, but with an implicit NR. The Spin compiler generates a destination address of 0 for the JMP instruction, so the result is actually a combination of the program counter and the contents of location 0. In theory, the only way to get a zero result would be to jump from location $1ff and the 22 most significant bits of location 0 must all be zero.

    The cflag, zflag and result variables I show in my code snippet above are only temporary variables. These values aren't actually stored in the cog's struct unless the appropriate bits are set in the ZCRI field of the instruction. The last few lines of the ExecutePasmInstruction routine in SpinSim are:
        // Conditionally update flags and write result
        if (zcri & 8) pasmvars->zflag = zflag;
        if (zcri & 4) pasmvars->cflag = cflag;
        if (zcri & 2) pasmvars->mem[dstaddr] = result;
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-01-17 13:27
    Good point, Dave. I hadn't actually considered that the "result" is the entire value of the destination after a presumptive write, not just the value written. D'oh!

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2011-01-17 13:40
    Surely JMP #1 WZ (5E7C0001) would set the Z flag.
  • jazzedjazzed Posts: 11,803
    edited 2011-01-17 13:42
    Dave Hein wrote: »
    In theory, the only way to get a zero result would be to jump from location $1ff and the 22 most significant bits of location 0 must all be zero.
    But that only applies for JMPRET. No?
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-01-17 13:51
    JMP #1 WZ is the same as JMPRET 0, #1 WZ, NR. They generate the same instruction. The Z flag will be written, but it will probably have a value of 0. The state of the Z flag doesn't depend on whether the result is written or not. The only way the Z flag can be one is if the combination of location zero and the program counter results in a zero value.

    The TEST or CMP instructions wouldn't work if the C or Z flags depended on whether the result was written or not. These are just NR versions of AND and SUB.
  • jazzedjazzed Posts: 11,803
    edited 2011-01-17 14:04
    Dave Hein wrote: »
    JMP #1 WZ is the same as JMPRET 0, #1 WZ, NR. They generate the same instruction. The Z flag will be written, but it will probably have a value of 0. The state of the Z flag doesn't depend on whether the result is written or not. The only way the Z flag can be one is if the combination of location zero and the program counter results in a zero value.

    The TEST or CMP instructions wouldn't work if the C or Z flags depended on whether the result was written or not. These are just NR versions of AND and SUB.

    But Dave if the dest starts at 0 why would it ever become anything else unless it was written?
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-01-17 14:27
    The difference between the JMP and JMPRET instructions is the same as the difference between CMP and SUB. Consider the following code:
            mov 0, #0
            cmp 0, #1 wz
    
    The Z flag will be zero, even though the destination contains a value of zero. The important thing is the result, even if it is not written back to memory.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-01-17 14:47
    Jazzed,

    Even with NR, the result is always computed -- just not written. The Z flag is set according to that computed result, IOW what the destination would have been had the result actually been written.

    -Phil
  • kuronekokuroneko Posts: 3,623
    edited 2011-01-17 16:30
    Dave Hein wrote: »
    In theory, the only way to get a zero result would be to jump from location $1ff and the 23 most significant bits of location 0 must all be zero.
    Indeed (or any location in the destination slot). But that's where it all falls apart as execution from $1FF (or rather any location with PC == $1FF) is not possible ([thread=118374]nop behaviour[/thread]). So for practical purposes a jmp[ret] clears Z (if enabled) and performs unsigned borrow.
  • jazzedjazzed Posts: 11,803
    edited 2011-01-17 21:49
    Thanks guys. I got what I was looking for.
Sign In or Register to comment.