Shop OBEX P1 Docs P2 Docs Learn Events
Z-flag and JMP — Parallax Forums

Z-flag and JMP

agsags Posts: 386
edited 2013-10-02 11:29 in Propeller 1
The manual is not clear to me on when the Z-flag is set by the JMP PASM instruction. The opcode table shows "Result=0" - but what does that mean for JMP? The destination field is unused (it's not specified at all for JMP), and the destination is not written unless WR is specified. So what is result value that must be =0 for the Z-flag to be set?

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-09-30 16:13
    ags wrote: »
    The manual is not clear to me on when the Z-flag is set by the JMP PASM instruction. The opcode table shows "Result=0" - but what does that mean for JMP? The destination field is unused (it's not specified at all for JMP), and the destination is not written unless WR is specified. So what is result value that must be =0 for the Z-flag to be set?
    Forget it. You can't set the Z flag with a jump insn. The result in this case is the destination register where the potential return address is inserted (movs, for a jump it defaults to $000). The remaining 23 bits would have to be zero as well. While the latter is possible the former isn't (return to 0) simply due to the fact that an insn at $1FF (which would cause a 0 return address) is aborted (acts as nop).

    See also the Similar Threads box below.
  • agsags Posts: 386
    edited 2013-09-30 17:43
    Thanks for the reply. This is actually OK. I was imprecise in my post. I actually want to take advantage of a convenient jmp instruction to make sure the z-flag is cleared, not set. So, it appears that I can be sure that this is the case, as the value contained in the register I am jumping to is not zero.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-09-30 17:53
    The JMP instruction is just a JMPRET instruction with NR implicitly set. That's why kuroneko is referring to the return address = 0 or not.
  • BeanBean Posts: 8,129
    edited 2013-10-01 04:24
    So will a jmp always clear the zero flag if you specify WZ ?
    And what about the carry flag if you specify WC ?

    Might be a useful trick to tighten up code.

    Bean
  • kuronekokuroneko Posts: 3,623
    edited 2013-10-01 05:46
    Bean wrote: »
    So will a jmp always clear the zero flag if you specify WZ ?
    Yes.
    Bean wrote: »
    And what about the carry flag if you specify WC ?
    Unsigned borrow (dst ? src).
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-10-01 05:50
    The code in spinsim that implements jmpret is as follows:
    	    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;
    
    The carry bit is the unsigned comparison of the destination value with the source value. The zero bit is determined by the result, which consist of the 23 most significant bits of the source destination value or'ed with the 9-bit value of the next address in the program counter.
  • BeanBean Posts: 8,129
    edited 2013-10-01 08:17
    Dave,
    So it looks to me that the carry would always be set if WC was used since value1 would be zero and value2 would be non-zero (zero < non-zero).
    And that the zero flag would always be cleared if WZ was used since "pc" would be non-zero (zero | pc != 0).

    Bean
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-10-01 09:24
    I believe the only way the result can be zero is if the return address location contained zero (at least the 23 MSBs would have to be zero) and the jmp was made from location 511. I don't know it's possible to execute code from location 511, and it wouldn't happen with "normal" code.

    The carry depends on the value located at the address given by the destination field, and the value given by the source field. For a jmp instruction compiled by the Spin Tool, the destination address is 0, but the contents of location 0 may contain a non-zero value. The source field could specify either a 9-bit immediate value, or it could be the address for a 32-bit value. The C flag comparison is done on 32-bit values.

    So if you really wanted to be tricky you could perform a jmp and compare two 23-bit values at the same time. The 23-bit values would be stored in the most significant bits of the locations addressed by the source and destination fields. The jump target address would be stored in the 9 least significant bits of the location addressed by the source field. The 9 LSBs of the value addressed by the destination field would have to match the 9 LSBs located at the source address, or you could bias the comparison by making the 9 LSBs all zeros or all ones.
  • agsags Posts: 386
    edited 2013-10-01 16:10
    So summarizing (regarding the z-flag at least): the jmp instruction will always clear the z-flag. This is because the 9 LSB of the dst register will always contain PC+1, and it is not legal (possible) to execute an instruction from cog $1FF.

    Out of curiosity, what value does the PASM assembler place in the dst section of the instruction? I presume it's all $00 (not that it matters)
  • kuronekokuroneko Posts: 3,623
    edited 2013-10-01 16:18
    ags wrote: »
    Out of curiosity, what value does the PASM assembler place in the dst section of the instruction? I presume it's all $00 (not that it matters)
    It is in fact $000 (see Dave's comment) but feel free to put anything else in there, e.g. jmpret $, #label wc,nr will clear carry. I remember a command interface I wrote for someone which hid some data in there ... up to you really.
  • agsags Posts: 386
    edited 2013-10-02 08:18
    Dave Hein wrote: »
    The code in spinsim that implements jmpret is as follows:
            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;
    
    The carry bit is the unsigned comparison of the destination value with the source value. The zero bit is determined by the result, which consist of the 23 most significant bits of the source value or'ed with the 9-bit value of the next address in the program counter.

    @Dave Hein: not to nitpick, instead to make sure I'm clear on this - did you mean to say "The zero bit is determined by the result, which consist of the 23 most significant bits of the destination value or'ed with the 9-bit value of the next address in the program counter"?
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-10-02 11:29
    Yes, I meant the destination value and not the source value. I'll strike out my error and fix it in my earlier post. Thanks.
Sign In or Register to comment.