******************************************************************************* * This code runs on the easy68K emulator. ******************************************************************************* org $3000 START ******************************************************************************* * Unit test of _ascii_to_bcd and _bcd_to_binary ******************************************************************************* pea BCDSPC ; convert ASCII to BCD pea ASCIIEX jsr _ascii_to_bcd adda.l #8,sp pea BCDSPC ; convert BCD to binary move.w d0,-(sp) ; return value from _ascii_to_bcd: # of digits 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 ******************************************************************************* * unsigned short ascii_to_bcd(unsigned char * ascii, unsigned char * bcdspc) * * In: ascii - source string of decimal numbers * bcdspc - space to write BCD sequence; should be at least as long as * input string * Out: length of string (in d0.w) * * 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 a string of * ASCII-encoded decimal numbers into a string of BCD bytes; for example it * converts '1979',0 (where the final 0-byte terminates the string) into * $01,$09,$07,$09. ******************************************************************************* _ascii_to_bcd: link a6,#0 movem.l d1/a0-a1,-(sp) move.l 8(a6),a0 ; arg 1: ascii move.l 12(a6),a1 ; arg 2: bcdspc clr.w d0 ; count, return value loop0 move.b (a0)+,d1 beq done0 subi.b #$30,d1 ; ASCII to BCD move.b d1,(a1)+ addi.w #1,d0 ; increment count bra loop0 done0 movem.l (sp)+,d1/a0-a1 unlk a6 rts ******************************************************************************* * 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 ******************************************************************************* _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 ******************************************************************************* * DATA ******************************************************************************* ASCIIEX dc.b '1979',0 ; sample input: the C string "1979" BCDSPC ds.b 256 ; space for conversion to BCD end START