******************************************************************************* * This code runs on the easy68K emulator. ******************************************************************************* org $3000 START ******************************************************************************* * Unit test of _bcd_to_binary ******************************************************************************* pea BCDEX ; sample call to _bcd_to_binary move.w #BCDEXN,-(sp) jsr _bcd_to_binary adda.l #6,sp clr.l d1 ; print result as unsigned long move.l d0,d1 move.l #3,d0 trap #15 move.b #9,d0 trap #15 BCDEX dc.b 1,9,7,9 BCDEXN equ 4 ******************************************************************************* * unsigned int bcd_to_binary(unsigned short n, unsigned char * bcds); * * In: n > 0 - number of BCD bytes * bcds - BCD bytes, one BCD/byte * Out: d0.l - -1 if error, otherwise binary representation of BCD * * A BCD (binary coded decimal) number is a sequence of bytes, each byte of * which is one of 0 - 9. For example, the four-byte sequence * * $01,$09,$07,$09 * * represents the decimal number 1979. This function converts from this * encoding into binary. ******************************************************************************* _bcd_to_binary: link a6,#0 movem.l d1/d2/a0,-(sp) clr.l d0 ; clear accumulator move.w 8(a6),d2 ; set up counter subi.w #1,d2 move.l 10(a6),a0 ; (a0) := address of BCD bytes loop1 move.l d0,d1 ; (d0) := 10 * (d0) lsl.l #3,d0 bcs error1 ; (unsigned) overflow lsl.l #1,d1 ; no overflow possible here add.l d1,d0 bcs error1 ; (unsigned) overflow clr.l d1 ; d1 gets next BCD byte move.b (a0)+,d1 add.l d1,d0 bcs error1 dbf d2,loop1 bra done1 error1 move.l #-1,d0 ; (d0.l) = -1 on exit done1 movem.l (sp)+,d1/d2/a0 unlk a6 rts end START