Decrement BCD
![JonnyMac](https://forums.parallax.com/uploads/userpics/922/n74EUX0AORWZS.jpg)
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
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)
Notice that this calculates the more general x-y rather than x-1
regards peter
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
·
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).