Shop OBEX P1 Docs P2 Docs Learn Events
CMPSUB: a rather odd duck (not so odd, after all -- sorry for the trouble) — Parallax Forums

CMPSUB: a rather odd duck (not so odd, after all -- sorry for the trouble)

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2009-04-02 04:00 in Propeller 1
The cmpsub assembly instruction can be very useful, but there are a couple gotchas that can trap the unwary:

1. Even though it's a subtract instruction, it defaults to nr. You have to specify wr for the subtraction to take place.

2. The sense of the carry flag is the opposite of all the other compare and subtract instructions, in that it's set iff D > S (rather than D < S, as the others do).

Unless there's just an error in the docs, these make cmpsub a bit of an anomaly. I don't want to second-guess Chip, since the instruction set as a whole is so elegant and logical; but it would be interesting to know what prompted these two design decisions.

One thing that #2 makes possible (assuming the docs are correct) is a sure-fire way to set the carry flag without any other side effects:

        cmpsub  any_register,#0 wc




-Phil

Addendum: Apparently #1 is the result of an error in the manual. wr is the default. The carry flag behaves as specified. It's this way to keep the division routine as short as possible.

Post Edited (Phil Pilgrim (PhiPi)) : 4/2/2009 4:04:59 AM GMT

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2009-04-01 21:07
    Manual says:
    CMPSUB compares the unsigned values of Value1 and Value2, and if Value2 is equal to or
    greater than Value1 then it is subtracted from Value1 (if the WR effect is specified)

    So, it's only subtracting if source is smaller than destination. That's nice for calculations where the result should definitely not be negative.
  • heaterheater Posts: 3,370
    edited 2009-04-01 21:14
    For example there is a simple but slow algorithm for calculating square roots:

    Subtract ever increasing odd numbers, 1, 3, 5, 7... from your input number until you can't subtract any more without going negative. The number of subtractions performed is the square root of your input.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-04-01 21:34
    Yes, yes: conditional subtraction is what the instruction is all about. It's also useful for managing index wraparound for circular buffers. But the thrust of this thread was meant to point out the default nr and the odd sense of the carry flag, to question whether the documentation is correct on these two issues and, if so, to probe the logic behind this apparent departure from instruction set regularity. That's the part I just don't quite get...

    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 4/1/2009 9:41:09 PM GMT
  • ericballericball Posts: 774
    edited 2009-04-02 01:04
    Please, please, PLEASE read the Erratta as CMPSUB in the Manual is just plain wrong. (Why the Errata isn't integrated into the Manual is beyond me. Sure, have an Errata for those with printed versions of the manual, but there's no reason not to update the softcopy.)

    CMPSUB defaults to WR! C is set if Dest is LESS THAN Source (i.e. same as opposite from SUB).

    CMPSUB is really cool for circular counters and division code.

    Post Edited (ericball) : 4/12/2009 11:56:06 PM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-04-02 01:17
    Oh, phew! My faith in the instruction set has been restored! And you're right: I should've scanned the errata before posting. sad.gif

    <lame_excuse>
    ····One reason I did not is that I found the same info in two independent sources. (Or maybe they weren't so indpendent after all.)
    </lame_excuse>

    Thanks for the correction!
    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 4/2/2009 1:27:07 AM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-04-02 04:00
    Geez! This thread has become a veritable comedy of errata! What was the lesson from the cold war? Oh, right: trust but verify.

    Here's the real dope: wr is, indeed, the default (as shown in the Errata), but carry is set if D > S (not the same as cmp and sub), as the manual correctly states. There's a very good reason for this, too, which I had momentarily forgotten. It helps to keep the divide routine more compact, viz (from "Propeller Guts"):

    divide        shl       y,#15      'get divisor into y[noparse][[/noparse]30..15]
                  mov       t,#16      'ready for 16 quotient bits
    :loop         cmpsub    x,y wc     'if y =< x then subtract it, quotient bit into c
                  rcl       x,#1       'rotate c into quotient, shift dividend
                  djnz      t,#:loop   'loop until done
    divide_ret    ret                  'quotient in x[noparse][[/noparse]15..0], remainder in x[noparse][[/noparse]31..16]
    
    
    


    I really do hope this settles it and rather wish I could just delete the whole thread.

    -Phil
Sign In or Register to comment.