BCD 數(shù)轉(zhuǎn)換為二進(jìn)制數(shù)(義隆單片機(jī))
兩字節(jié)壓縮BCD 碼轉(zhuǎn)換為兩字節(jié)二進(jìn)制數(shù)。算法如下:
BCD 碼abcd=1000a+100b+10c+d=10{10[10a+b]+c}+d,將各位BCD 碼分離出之后,即可根據(jù)此式轉(zhuǎn)換為二進(jìn)制
數(shù)。涉及到乘法運(yùn)算和多字節(jié)加法運(yùn)算。
0X20,0X21 中為BCD 碼,0X21 高4 位為最高位。轉(zhuǎn)換結(jié)果放在0X30,0X31 中。
main: MOV a,@0x79
MOV 0x20,a
MOV a,@0x54
MOV 0x21,a ;賦值
MOV a,0x20
and a,@0x0f
MOV 0x22,a
swapa 0x20
and a,@0x0f
MOV 0x23,a
MOV a,0x21
and a,@0x0f
MOV 0x24,a
swapa 0x21
and a,@0x0f
MOV 0x25,a ;BCD 碼展開后存于0X22,0X23,0X24,0X25
MOV a,0x25, ;0X25 為最高位
MOV 0x30,a
MOV a,@0x0 ;多字節(jié)加法高位為0
MOV 0x31,a
MOV a,0x24
MOV 0x32,a
call a_b ;調(diào)子程
MOV a,0x23
MOV 0x32,a
call a_b
MOV a,0x22
MOV 0x32,a
call a_b
self: jmp self
a_b:
MOV a,@0x0 ;0X34,0X35 存儲中間結(jié)果
MOV 0x34,a
MOV 0x35,a
MOV a,@0x0a ;實現(xiàn)乘10
MOV 0x33,a
a1: MOV a,0x35 ;兩字節(jié)二進(jìn)制加法,在本例中高字節(jié)肯定無進(jìn)位
add a,0x31
MOV 0x35,a
MOV a,0x34
add a,0x30
MOV 0x34,a
jbc 0x03,0
inc 0x35
djz 0x33
jmp a1
MOV a,0x32
add 0x34,a
jbc 0x03,0
inc 0x35
MOV a,0x34
MOV 0x30,a
MOV a,0x35
MOV 0x31,a
ret
eop