Ich meine, das habe ich hier schon mal reingestellt, ist für 16Bit ,
also 2 Byte. Da die Wandlung von rechts nach links erfolgen muß, aber
die Ausgabe von links nach rechts, wird auf dem Stack
zwischengespeichert:
;***********************************************************************
**
;* subroutine: convert 16bit-word -> decimal -> ASCII -> output
;***********************************************************************
**
DecOut:
rcall Div10 ; get "ones" digit
ori Temp1,$30 ; convert to ASCII
push Temp1 ; and save it
rcall Div10 ; get "tens" digit
ori Temp1,$30 ; convert to ASCII
push Temp1 ; and save it
rcall Div10 ; get "hundreds" digit
ori Temp1,$30 ; convert to ASCII
push Temp1 ; and save it
rcall Div10 ; get "thousands" digit
tst Temp1 ; omit leading zero
breq DecOu1 ;
ori Temp1,$30 ; convert to ASCII
rcall Serout ; print thousands
DecOu1:
pop Temp1 ; fetch hundreds
rcall Serout ; and print it
pop Temp1 ; fetch tens
rcall Serout ; and print it
pop Temp1 ; fetch ones
rcall Serout ; and print it
ret ;
;***********************************************************************
**
;* subroutine: divide 16bit word in TempH/TempL by 10,
;* results: quotient in TempH/TempL, remainder in Temp1 = decimal
digit
;* taken from "Trampert, AVR-RISC-Mikrocontroller"
;* Franzis-Verlag www.franzis.de ISBN 3772354742 pg. 327
;***********************************************************************
**
Div10:
push Cnt ;
clr Temp1 ;
lsl TempL ; input word is shifted out to the left
rol TempH ; filled up with zeroes from the right
rol Temp1 ;
lsl TempL ;
rol TempH ; most significant 3 bits are always < 10
rol Temp1 ; so just shift out and in via Cy 3 times
lsl TempL ;
rol TempH ;
rol Temp1 ;
ldi Cnt,13 ; start loop for next 13 bits
Div10a:
lsl TempL ;
rol TempH ;
rol Temp1 ;
subi Temp1,10 ; remainder >10 ? -> subtract 10
brlo Div10b ; was <10, LSB of output word = 0
inc TempL ; LSB of output word = 1
rjmp Div10c
Div10b:
subi Temp1,-10 ; back to old value <10
Div10c:
dec Cnt ; next bit
brne Div10a ;
pop Cnt ;
ret ;