Shop OBEX P1 Docs P2 Docs Learn Events
DCD function for SX — Parallax Forums

DCD function for SX

T'SaavikT'Saavik Posts: 60
edited 2007-04-02 04:15 in General Discussion
Hey all,
I've been playing around with the Stampworks examples with my SX28. I've been trying to keep with the spirit of each project and not cheat my way past the hard stuff [noparse]:D[/noparse]

Today while working on example 5 i ran into the "DCD" command used by the basic stamps, and not available under sx/b.
Regarding DCD the basic Stamp manual said...

The Decoder operator (DCD) is a 2n - power decoder of a four-bit value.
DCD accepts a value from 0 to 15, and returns a 16-bit number with the bit, described by value, set to 1.
for example:

result VAR Word
result = DCD 12 ' Set bit 12
DEBUG BIN16 ? result ' Display result (%0001000000000000)

I wrote this function, and the program works as described in Stampworks, but i wonder if i really got it "right" or just cheated.

' -----[noparse][[/noparse] Variables ]-------------------------------------------------------
dcdIN    VAR   Byte
dcdOut   VAR   Word
' -----[noparse][[/noparse]Subs ]------------------------------------------------------------
DCD Func    1,2




DCD:
  dcdIn = __PARAM1
  dcdOut = $0001
  Do While dcdIn > 1
        dcdOut = dcdOut << 1
        dec dcdIn
  Loop
RETURN dcdOut

Comments

  • JonnyMacJonnyMac Posts: 8,942
    edited 2007-03-30 08:10
    It's easier than that:

    FUNC DCD
      tmpW1 = 1 << __PARAM1
      RETURN tmpW1
      ENDFUNC
    
    



    Be sure to declare your function as returning two bytes (word result) and expecting one (bit position to set):

    DCD        FUNC    2, 1
    
    

    Post Edited (JonnyMac) : 3/30/2007 4:13:44 PM GMT
  • JonnyMacJonnyMac Posts: 8,942
    edited 2007-03-30 14:46
    And here's the complimentary function, NCD, in case you need it as well:

    NCD        FUNC    1, 2
    



    FUNC NCD
      tmpW1 = __WPARAM12
      tmpB1 = 16
      DO WHILE tmpB1 > 0
        IF tmpW1_MSB.7 = 1 THEN EXIT
        DEC tmpB1
        tmpW1 = tmpW1 << 1
      LOOP
      RETURN tmpB1
      ENDFUNC
    

    Post Edited (JonnyMac) : 3/30/2007 4:13:54 PM GMT
  • T'SaavikT'Saavik Posts: 60
    edited 2007-03-31 16:25
    Thank you Jonny!!

    It didn't occur to me to use the value as the shifter and a fixed # as the input, very nice.
    I havn't run into anything yet that uses NCD, but i'm sure i will, Thanks for it!


    Dumb question, since you can recreate these functions so efficiently, why are they not in the sx/b language?
    Or is it more complicated then that?
  • JonnyMacJonnyMac Posts: 8,942
    edited 2007-03-31 16:41
    ... because they can be created so efficiently! Remember, SX/B was designed to be a learning tool for those moving from Basic to Assembly. It turned out to be a great compiler (thanks, Bean!) and many of use use it professionally (I have will EFX-TEK and my personal clients). For me, I'm hoping SX/B doesn't move too close to traditional compilers that hide or confusticate details as I am becoming a better Assembly programmer by studying the SX/B compiler output.
  • T'SaavikT'Saavik Posts: 60
    edited 2007-04-02 03:48
    Ah i see. Maybe someone should grab all these great code snips and stick them in the online help or....something! Can't just leave these gems floating around the forums [noparse]:)[/noparse]

    Stuff like setting multiple break points by putting break in a sub (genius!), etc,etc [noparse]:)[/noparse]

    Ooooh, what if someone made a collection of functions/subs that you could use "load" on.
    Or even better a C like "use" command in the sxkey software that only sucks in the functions/subs pre-compile that you use.

    Then you get the great code snips, and still have your list file.

    Sure glad i never owned a Basic stamp, i sure would be spoiled [noparse]:)[/noparse]
  • JonnyMacJonnyMac Posts: 8,942
    edited 2007-04-02 04:15
    I have a big collection of these "gems" in my book "Practical SX/B" that I am working very hard to finish (there's no forum like this one where I can ask, "Hey, how to I write a chapter on...").

    You can collect useful code snippets into a file and use the LOAD directive in SX/B; see the help file for details.

    Post Edited (JonnyMac) : 4/2/2007 4:22:55 AM GMT
Sign In or Register to comment.