Shop OBEX P1 Docs P2 Docs Learn Events
PASM question - using CMP or CMPS — Parallax Forums

PASM question - using CMP or CMPS

Chris_DChris_D Posts: 305
edited 2011-02-13 06:45 in Propeller 1
Hi PASM folks,

I am curious as to why we have both of these. CMPS can work for sign and unsigned comparisons. So why do we need to have CMP?

Chris

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-12 13:30
    CMPS does a 32-bit signed comparison and CMP does a 32-bit unsigned comparison.
  • Chris_DChris_D Posts: 305
    edited 2011-02-12 13:42
    Hi Dave,

    Yeah I get what they both do, I just don't understand why I can't use CMPS for comparisons of both positive and negative numbersall the time.

    Chris
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-12 14:05
    A CMP of 0 and -1 says that -1 is larger. CMPS says that 0 if larger. You could just use CMPS, but you would need to check the sign bit to determine if $FFFFFFFF is larger than 0 for an unsigned compare. It would require executing more instructions.
  • Chris_DChris_D Posts: 305
    edited 2011-02-13 03:20
    Thanks Dave,

    I think I got it now.


    Chris
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2011-02-13 04:08
    I have to admit finding this confusing when I first started coding in assembly with the propeller, it had been a while since I had considered binary at all and programming on a PC in things like matlab made me think that my variables would know whether they were signed or not. Rather they are just 32bits of 1's and 0's and if you want them to represent signed numbers you must treat them like that by choosing the correct commands etc. I remember playing with "calculator" a lot to remind myself how 2's compliment actually works :)

    Graham
  • Heater.Heater. Posts: 21,230
    edited 2011-02-13 04:58
    Don't forget that CMP/CMPS are the same operations as SUB/SUBS but with the result not being written out.

    The difference between CMP and CMPS is in the way the Carry flag is set. See the Propeller manual.

    With a bit of thought I think you can convince yourself why using CMP for unsigned and CMPS for signed values works.

    Hint:

    In unsigned arithmetic $FFFFFFFF is greater than zero. Of course $FFFFFFFF is the maximum value possible.

    In signed arithmetic $FFFFFFFF represents minus one and is therefore less than zero.
  • kuronekokuroneko Posts: 3,623
    edited 2011-02-13 05:16
    Heater. wrote: »
    Don't forget that CMP/CMPS are the same operations as SUB/SUBS but with the result not being written out.
    FWIW, [thread=126602]how come cmp = sub nr but cmps <> subs nr[/thread].
  • Heater.Heater. Posts: 21,230
    edited 2011-02-13 05:39
    huroneko:

    Hmmm...something subtle I have overlooked. Must investigate....
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-13 06:45
    CMP and SUB use the same opcode, but CMPS and SUBS used different opcodes. It's just the way Parallax assigned the mnenomics. A CMPSO could have been defined, which would be SUBS NR and would set the carry on signed overflows.
Sign In or Register to comment.