; by FredMaher Madrid 10 Nov 02, Rewritten for SX by James Newton Feb 03. ; Using an extension of the >4 add 3 algorithm bcd DS BCDdigits bin DS BINdigits IF NDEF count count DS 1 ENDIF IF NDEF temp temp DS 1 ENDIF ; ;----------------------------------------------------------------- ;Hex(Binary) to BCD routine ;----------------------------------------------------------------- Main: BCDclear: mov temp, BCDdigits mov fsr, bcd clr ind djnz temp, BCDclear Testvals: mov W, #$FF mov num, W mov W, #$FF mov num+1, W mov W, #$0F mov num+2, W call bin2bcd Convend: ; remove when in main prg jmp Convend ; remove when in main prg ;----------------------------------------------------------- ; End main Bin Bcd ;------------------------------------------------------------ mov count, #BINdigits * 8 ; count max value Shifting: test count snb Z ret ; bin number is now converted 2 bcd dev count ;---------------------------- ; Now left shift all registers clrb C mov temp, #BCDdigits mov fsr, #bcd BCDlrLoop rl ind inc fsr djnz temp, BCDrlLoop clrb C mov temp, #BINdigits mov fsr, #bin BINlrLoop rl ind inc fsr djnz temp, BINrlLoop ; next step check those which are >4 and add 3 test count snb Z ; finish without testing >4 ret Nibtests: Test BCD1 through BCD5, high and low nibbles. If greater than 4 ( or 40) add 3 (or 30) ;------------------------------------BCD1 mov temp, #BCDdigits BCDadjLoop mov W, #$0B ;(16-5) add W, ind ;DC set if carry from bit 3 to 4 mov W, #$03 ; >4 add3 snb DC add ind, W mov W, #$B0 ; is high nibble more than 5? add W, ind mov W, #$30 ; add 3 to high nibble snb C add ind, W inc fsr djnz temp, BCDadjLoop jmp Shifting