;tempH,M,L convert to BCD Dig[0..6]:
ldi ZL,Dig+7
clr temp
clrout: st -Z,temp
cpi ZL,Dig-1
brne clrout ;output array clear
ldi tempK,24 ;input bits count
hloop: lsl tempL ;input array shift left
rol tempM
rol tempH
ldi YL,Dig+7
sloop: ld temp,-Y
rol temp
subi temp,-0x06 ;temp+6, C=1
sbrs temp,4
subi temp,0x06 ;temp-6, C=0
andi temp,0x0f
st Y,temp
cpse YL,ZL ;ZL = Dig-1
rjmp sloop
dec tempK ;YL = Dig-1
brne hloop
Или на Си:
//Convert binary digit to BCD:
//x - input binary digit (32 bits, unsigned)
//buff - output array (10 digits)
void Long2BCD(unsigned long x, char *buff)
{
for(char i = 0; i < 10; i++)
buff[i] = 0; //output buffer clear
for(char i = 0; i < 32; i++) //cycle for input bits count
{
char c = (x >> 31) & 1; //save carry
x = x << 1; //input value shift
for(char p = 0; p < 10; p++) //cycle for digits number
{
char s = buff[p]; //read digit
s = (s << 1) | c; c = 0; //shift with carry
if(s > 9) { s += 0x06; c = 1; } //digit correction
s &= 0x0F; //select nibble
buff[p] = s; //save digit
}
}
}