基于EM78系列單片機的數(shù)值轉(zhuǎn)換子程序
sub 0x22,a
jbc 0x03,2
ret ;低字節(jié)相等,無借位,返回
jbc 0x03,0
ret ;無借位,返回
dec 0x23 ;低字節(jié)相減有借位,高字節(jié)結(jié)果減1
ret
eop
六 BCD數(shù)轉(zhuǎn)換為二進制數(shù)
兩字節(jié)壓縮BCD碼轉(zhuǎn)換為兩字節(jié)二進制數(shù)。算法如下:
BCD碼abcd=1000a+100b+10c+d=10{10[10a+b]+c}+d,將各位BCD碼分離出之后,即可根據(jù)此式轉(zhuǎn)換為二進制數(shù)。涉及到乘法運算和多字節(jié)加法運算。
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é)二進制加法,在本例中高字節(jié)肯定無進位
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
七 二進制數(shù)轉(zhuǎn)換為BCD碼
本例為單字節(jié)二進制數(shù)(0X20)轉(zhuǎn)換為非壓縮BCD碼,存在0X25,0X24,0X23中,0X25為百位,0X23為個位。
main: mov a,@0xa4 ;賦值
mov 0x20,a
mov 0x21,a
mov 0x22,a
mov a,@0x0 ;0x23,0x24,0x25單元清0
mov 0x23,a
mov 0x24,a
mov 0x25,a
mov a,@0x64 ;對100的個數(shù)計數(shù)
mov 0x26,a
mov a,@0x25 ;百位存在0x25中
mov 0x04,a
call a0 ;調(diào)計數(shù)子程
mov a,@0x0a ;對10的個數(shù)計數(shù)
mov 0x26,a
dec 0x04 ;個位存在0x24中
call a0
mov a,0x22 ;除去百位,十位,余下的即個位,存入0x23
mov 0x23,a
self: jmp self
a0: ;計數(shù)子程
mov a,0x26
sub 0x22,a
jbs 0x03,2
jmp a1
inc 0x00 ;無余數(shù)則對應(yīng)位加1
mov a,@0x0 ;0x21與0x22在返回時應(yīng)保持相同
mov 0x21,a
ret
a1: jbs 0x03,0 ;小于則跳a2
jmp a2
inc 0x00 ;大于則計數(shù)值加1
mov a,0x22
mov 0x21,a ;將0x22保存到0x21中
jmp a0 ;跳回a0繼續(xù)計數(shù)
a2: mov a,0x21 ;0x21中保存的減之前的數(shù)據(jù),此時恢復(fù)到0x22
mov 0x22,a
ret
eop本文引用地址:http://www.ex-cimer.com/article/171138.htm
評論