Shop OBEX P1 Docs P2 Docs Learn Events
Decrement BCD — Parallax Forums

Decrement BCD

JonnyMacJonnyMac Posts: 9,214
edited 2008-12-09 15:27 in General Discussion
I came up with this little function to decrement a BCD value -- is there a cleaner way?

FUNC DEC_BCD
  ASM
    SUB   __PARAM1, #1                          ' decrement
    JB    DC, DBCD_Exit                         ' exit if no roll-under
    SUB   __PARAM1, #$06                        ' correct 1s digit
    CJBE  __PARAM1, #$99, DBCD_Exit             ' exit if legal
    AND   __PARAM1, #$0F                        '  else correct 10s digit
    OR    __PARAM1, #$90
  ENDASM

DBCD_Exit:
  ENDFUNC

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-12-09 10:37
    According to
    http://www.sxlist.com/techref/ubicom/lib/math/sub/bcdp2_sx.htm

    the following should also work (I removed the carry restore instructions as they are not needed)
    'calculate x-y where x and y are packed bcd values
    'use val = sub_bcd x,y
    FUNC sub_bcd
    asm
      mov W,__PARAM2 ;__PARAM1 = __PARAM1 - __PARAM2
      sub __PARAM1,W
      clr W
      sb DC          ;if lsn of __PARAM1 < lsn of __PARAM2 (dec)
      or W,#$06      ;then substract extra 6
      sb C           ;Similarly for the msn
      or W, #$60
      sub __PARAM1,W ;correct result
    endasm
      RETURN __PARAM1
    ENDFUNC
    
    

    Notice that this calculates the more general x-y rather than x-1

    regards peter
  • BeanBean Posts: 8,129
    edited 2008-12-09 13:03
    Jon,

    Unless you can guarentee the address of the routine, you should use "JB DC,@DBCD_Exit" and "CJBE __PARAM1,#$99,@DBCD_Exit" (note use of @). Otherwise if a page break occurs between the jump and the label, it will end up jumping to the wrong address.

    Your "AND __PARAM1,#$0F" and "OR __PARAM1,#$90" can be replaced with "MOV __PARAM1,#$99".

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    "The welfare of the people in particular has always been the alibi of tyrants." ~ Camus
    www.iElectronicDesigns.com

    ·
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-12-09 15:27
    Thanks, guys.

    Peter: I swear, I looked through the SXList but did not see the code you referenced, I was probably a little to specific in what I was looking for. I'm going to use your version with a fixed y of #1.

    Bean: I pulled that code from my test program; in my project file I had the @ versions of the instructions (moot now, as I'll use the code without jumps).
Sign In or Register to comment.