Ich habe in AVR ein Programm geschrieben , um binäre Zahlen ohne Vorzeichen in Dezimalzahlen ASM
umzuwandeln , basierend auf dem . (Ich weiß, das ist mehr als 8-stellig, aber ich brauche nur 8.)32-bit
8 digit
shift-add-3
32-bit
Der 32-bit
Eingang ist in R16-R19
(Low-High).
Die 8 digit
Ausgabe erfolgt in R20-R24
(Low-High), 2 Zahl/Byte, eine im Lower Nibble, eine im Higher Nibble.
Mein Problem: Es dauert ~ 1500 Zyklen, um eine 16-bit
Zahl zu berechnen, und ~ 2000 Zyklen, um eine zu berechnen 32-bit
.
Kann mir jemand eine schnellere, professionellere Methode dafür vorschlagen? Das Ausführen eines Verfahrens mit 2000 Zyklen auf einem ATtiny bei 32,768 Khz
ist etwas, mit dem ich mich nicht wohl fühle.
Speichernutzungskarte:
Definitionen:
.def a0 = r16
.def a1 = r17
.def a2 = r18
.def a3 = r19
.def b0 = r20
.def b1 = r21
.def b2 = r22
.def b3 = r23
.def i = r24
.def j = r25
Der Code:
BinaryToBCD:
clr b0
clr b1
clr b2
clr b3
ldi i, 32
sts 0x0068, i ;(SRAM s8)
BinaryToBCD_1:
clc
rol a0
rol a1
rol a2
rol a3
rol b0
rol b1
rol b2
rol b3
lds i, 0x0068 ;(SRAM s8)
dec i
sts 0x0068, i ;(SRAM s8)
brne BinaryToBCD_2
ret
BinaryToBCD_2:
cpi b0, 0
breq BinaryToBCD_3
mov i, b0
rcall Add3ToNibbles
mov b0, i
BinaryToBCD_3:
cpi b1, 0
breq BinaryToBCD_4
mov i, b1
rcall Add3ToNibbles
mov b1, i
BinaryToBCD_4:
cpi b2, 0
breq BinaryToBCD_5
mov i, b2
rcall Add3ToNibbles
mov b2, i
BinaryToBCD_5:
cpi b3, 0
breq BinaryToBCD_1
mov i, b3
rcall Add3ToNibbles
mov b3, i
rjmp BinaryToBCD_1
Add3ToNibbles:
mov j, i
andi j, 0b00001111
cpi j, 5
in j, SREG
sbrs j, 0
subi i, -3
mov j, i
swap j
andi j, 0b00001111
cpi j, 5
in j, SREG
sbrs j, 0
subi i, -48
ret
Dies basiert auf Vennys Ansatz (Venny nannte es Triangulation), ausgedrückt auf einem "Pseudo-C":
uint32 x; // input variable to convert
w = { 2, 1, 4, 7, 4, 8, 3, 6, 4, 8 }; // 2^31
r = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // initial result = 0
for (i = 31; i >= 0; i --)
{
if ( 2^i AND x ) // is x's bit i up?
add(r, w); // if yes, 1 ASCII ADD and 9 ASCII ADD w/CARRY MAX
divide(w, 2) // 10 SHIFT RIGHT MAX
}
Routinen addieren und dividieren sind keine Erklärung erforderlich, imo.
Es gibt eine Reihe von Artikeln und Anwendungshinweisen zu diesem Thema. Beispiel: http://www.element14.com/community/servlet/JiveServlet/downloadBody/47820-102-3-258641/Cypress.Application_Notes_35.pdf
Venny
Golaž
Gabor DANI
Venny
vlad_tepesch
kabZX