Ich habe die 16 Bit /10 Divisionsroutine aus dem Trampert-Buch zum AVR
hier, die braucht man immer wenn man eine binär vorliegende Zahl als
ASCII-Ziffern ausgeben will. Für jede Stelle wird die Routine einmal
aufgerufen, anschließend ist die ursprüngliche Zahl in TempH/TempL
durch 10 geteilt, die Ziffer steht in Temp1. Die Ziffern kommen von
rechts nach links, das muß man bei der Ausgabe berücksichtigen:
;***********************************************************************
**
;* 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 ;