Shop OBEX P1 Docs P2 Docs Learn Events
CMPS problem — Parallax Forums

CMPS problem

Graham StablerGraham Stabler Posts: 2,507
edited 2007-10-16 17:14 in Propeller 1
I have some test code roughly like this:


                   cmps   zero,zero       wc
     if_nc       jmp    #:nxt

......

long     zero      0





When wc is set for cmps then c should be set when value1 <= value2 so can anyone tell me why the code acts differently when the line with the jmp is commented out? As zero will always equal zero and hence C should always be set.

The following code works as expected:


                   cmps   zero,one       wc
     if_nc       jmp    #:nxt

......

long     zero      0
long     one       1





Commenting out the jmp line makes no difference.

But this code also does not work as expected:


                   cmps   one,one       wc
     if_nc       jmp    #:nxt

......

long     one       1





Again the jmp is getting called even though one will always equal one.

Graham

Comments

  • Jeff MartinJeff Martin Posts: 756
    edited 2007-03-29 22:23
    Hi Graham,

    · In the cmps instruction, it's like a subs... c is set when there is a signed borrow in the operation, z is set when the result is zero.·

    since zero - zero = zero, only z would be set and c would be clear.

    zero - one = -1, so z would be clear and c would be set.

    After looking at your posting and typing this I began to wonder why you thought c would be set if value1 <= value2... then I looked in the manual and saw that it may be because that's what I mistakenly wrote in the manual... [noparse]:([/noparse]· I'm sorry, I goofed.· I'll fix it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Jeff Martin

    · Sr. Software Engineer
    · Parallax, Inc.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-03-29 22:26
    At least I am not going mad.

    Thanks

    Graham
  • rokickirokicki Posts: 1,000
    edited 2007-03-29 22:51
    Heh, good catch! I have a tendency to, when reading, "read" something I "know" rather than what's actually there. I probably never
    would have caught that <= on the carry bit.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-03-29 23:19
    Well my code now works, cartesian to polar conversion and R.Sin(theta) R.Cos(theta) cordic algorithms are using a common generalized function. Next comes atan, sin etc

    I spent all last night staring at this and most of tonight, armed with this info I only needed about 10 minutes more, phew.

    Graham
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-10-16 15:01
    I think this error is still in the manual and not in the errata.
  • Mark SwannMark Swann Posts: 124
    edited 2007-10-16 16:00
    Jeff Martin (Parallax) said...

    ...· In the cmps instruction, it's like a subs...

    Jeff,

    Are you saying that the following two instructions do exactly the same thing?


    SUBS [font=ArialNarrow,BoldItalic size=2]SValue1[/font], 〈#〉 [font=ArialNarrow,BoldItalic size=2]SValue2[/font]

    [font=ArialNarrow,BoldItalic size=2]CMPS [font=ArialNarrow,BoldItalic size=2]SValue1[/font], 〈#〉 [font=ArialNarrow,BoldItalic size=2]SValue2 WR[/font]

    [font=ArialNarrow,BoldItalic size=2]If so, then that would make make CMPS a redundant opcode, which can be eliminated and reused in Prop, the Sequal, (coming to a theater Radio Shack near you ;-) ).[/font]

    [font=ArialNarrow,BoldItalic size=2]Mark

    [/font][/font][font=ArialNarrow,BoldItalic size=2]

    [/font]Post Edited (lucidman) : 10/16/2007 4:22:01 PM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-16 16:37
    No, no no.. So many misconceptions.. But deSilva comes to the rescue.

    (a) Jeff said: CMPS is something LIKE SUBS
    This is a little bit misleading.. In fact it is not! CMP is something just as SUB.
    But the carry is handled very differently in CMPS and SUBS. In fact the carry handling of SUBS is rarely understood and deSilve strongly recommends NOT to use SUBS if you do not exactly know what you are doing, just use SUB. You cannot find out the sign after an subtraction, simply as there is no sign flag, as in many other procesors. The sign bit IS bit 31 and you have to check for it. The cleanest way is to do CMPS xx, #0 after a SUB.

    (b) There are MANY assembly codes for just the same binary opcode, best known is CALL for JMPRET, or even CMP and SUB which lead to the same opcode.. This is just fine and usage in many assambly languages. It will not help to free any of the op codes...

    Post Edited (deSilva) : 10/16/2007 4:45:12 PM GMT
  • Mark SwannMark Swann Posts: 124
    edited 2007-10-16 16:59
    deSilva,
    I see now that the carry handling is different. Thanks
    deSilva said...
    No, no no.. So many misconceptions.. But deSilva comes to the rescue.

    (a)·... The cleanest way is to do CMPS xx, #0 after a SUB.
    In this example, why not just do CMPS dval, sval WC,WR instead of SUB(S?) dval, sval WC, if you need to need to know the sign after the subtract?

    Generally, I really would like to see some documentation descibing the carry/borrow/overflow logic that defines the carry flag in the add/sub/cmp/adds/subs/cmps instructions. Some simple boolean statements or logic diagrams would suffice.

    Mark
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-16 17:06
    lucidman said...
    CMPS dval, sval WC,WR
    Devilishly clever! Maybe it will work...
  • Mark SwannMark Swann Posts: 124
    edited 2007-10-16 17:14
    deSilva said...
    lucidman said...
    CMPS dval, sval WC,WR
    Devilishly clever! Maybe it will work...
    Perhaps this should be added to the Propeller Trick and Traps thread.
Sign In or Register to comment.