;***************************************************************************
;*
;* "mpy16u" - 16x16 Bit Unsigned Multiplication
;*
;* This subroutine multiplies the two 16-bit register variables
;* mp16uH:mp16uL and mc16uH:mc16uL.
;* The result is placed in m16u3:m16u2:m16u1:m16u0.
;*
;* Number of words :14 + return
;* Number of cycles :153 + return
;* Low registers used :None
;* High registers used :7 (mp16uL,mp16uH,mc16uL/m16u0,mc16uH/m16u1,m16u2,
;* m16u3,mcnt16u)
;*
;***************************************************************************
;***** Subroutine Register Variables
.def mc16uL =r16 ;multiplicand low byte
.def mc16uH =r17 ;multiplicand high byte
.def mp16uL =r18 ;multiplier low byte
.def mp16uH =r19 ;multiplier high byte
.def m16u0 =r18 ;result byte 0 (LSB)
.def m16u1 =r19 ;result byte 1
.def m16u2 =r20 ;result byte 2
.def m16u3 =r21 ;result byte 3 (MSB)
.def mcnt16u =r22 ;loop counter
;***** Code
mpy16u: clr m16u3 ;clear 2 highest bytes of result
clr m16u2
ldi mcnt16u,16 ;init loop counter
lsr mp16uH
ror mp16uL
m16u_1: brcc noad8 ;if bit 0 of multiplier set
add m16u2,mc16uL ;add multiplicand Low to byte 2 of res
adc m16u3,mc16uH ;add multiplicand high to byte 3 of res
noad8: ror m16u3 ;shift right result byte 3
ror m16u2 ;rotate right result byte 2
ror m16u1 ;rotate result byte 1 and multiplier High
ror m16u0 ;rotate result byte 0 and multiplier Low
dec mcnt16u ;decrement loop counter
brne m16u_1 ;if not done, loop more
ret
;......................
;被除數:R19(HIGH)R18 R17 R16(LOW)
;除數:R21(HIGH)R20
;結果:R17(HIGH)R16
;余數:R19(HIGH)R18
;計數器,R22
;...................................................
d32v16u: ; 32位/16位無符號除法
cp r18,r20 ;被除數高16位 > 除數
cpc r19,r21 ;結果溢出
brcc cc
ldi r22,$10 ;初始化循環計數器
rol r16 ;左移被除數
rol r17
aa:
rol r18 ;左移余數(被除數移到除數)
bst r19,7
rol r19
sub r18,r20 ;余數-除數
sbc r19,r21
brts loop ;夠減,跳至 loop
brcc loop
add r18,r20 ;不夠減,再加除數
adc r19,r21
clc ;清進位位
rjmp loop1
loop:
sec ;置進位位
loop1:
rol r16 ;左移結果
rol r17
dec r22 ;計數器減 1
brne aa ;不為 0,再循環
clt ;清 t 標志
ret
cc:
set ;置 t 標志
ret
;...................................
;......................
;被除數:R17(HIGH) R16(LOW)
;除數:R21(HIGH)R20
;結果:R17(HIGH)R16
;余數:R19(HIGH)R18
;計數器,R22
;...................................................
d16v16u: ; 32位/16位無符號除法
; cp r18,r20 ;被除數高16位 > 除數
; cpc r19,r21 ;結果溢出
; brcc cc
clr r19
clr r18
ldi r22,$10 ;初始化循環計數器
rol r16 ;左移被除數
rol r17
vaa:
rol r18 ;左移余數(被除數移到除數)
bst r19,7
rol r19
sub r18,r20 ;余數-除數
sbc r19,r21
brts vloop ;夠減,跳至 loop
brcc vloop
add r18,r20 ;不夠減,再加除數
adc r19,r21
clc ;清進位位
rjmp vloop1
vloop:
sec ;置進位位
vloop1:
rol r16 ;左移結果
rol r17
dec r22 ;計數器減 1
brne vaa ;不為 0,再循環
clt ;清 t 標志
ret
;cc:
; set ;置 t 標志
; ret
;..............................
;...r17:r16(hex)--->r20(萬位):r19(千位):r18(百位):r17(十位):r16(bcd)
; 結果值在每字節里只存一位,即在每字節的低半字節里
;.....................;調用16位二進制轉5位十進制子程
b16td5:
ser r20 ;r20先送-1
b16td5_1:
inc r20 ;r20增1
subi r16,low(10000) ;(r17:r16)-10000
sbci r17,high(10000)
brcc b16td5_1 ;夠減則返回b16td5_1
subi r16,low(-10000) ;不夠減+10000,恢復余數
sbci r17,high(-10000)
ser r19 ;r19先送-1
b16td5_2:
inc r19 ;r19增1
subi r16,low(1000) ;(r17:r16)-1000
sbci r17,high(1000)
brcc b16td5_2 ;夠減則返回b16td5_2
subi r16,low(-1000) ;不夠減+1000,恢復余數
sbci r17,high(-1000)
ser r18 ;r18先送-1
b16td5_3:
inc r18 ;r18增1
subi r16,low(100) ;(r17:r16)-100
sbci r17,high(100)
brcc b16td5_3 ;夠減則返回b16td5_3
subi r16,low(-100) ;不夠減+100,恢復余數
sbci r17,high(-100)
ser r17 ;r17先送-1
b16td5_4:
inc r17 ;r17增1
subi r16,10 ;(r17:r16)-10
brcc b16td5_4 ;夠減則返回b16td5_4
subi r16,-10 ;不夠減+10,恢復余數
ret
;...........................................
;...............;r16(high)r19(low)/r17 =r16(結果)+r19(余數)
d16v8u: ;16/8無符號除法
cp r16,r17 ;被除數高字節 > 除數
brcc ddd ;結果溢出
ldi r18,8 ;初始化循環計數器
rol r19 ;左移余數(被除數低字節)
aaa:
bst r16,7
rol r16 ;左移被除數高字節
sub r16,r17 ;r16減r17
brts bbb
brcc bbb
add r16,r17 ;結果為負,則r16加r17
clc ;清進位位
rjmp ccc
bbb:
sec ;夠減,置進位位
ccc:
rol r19 ;左移余數
dec r18 ;減計數器
brne aaa ;沒完成再循環
clc
mov r17,r16
mov r16,r19 ;r16存放結果
mov r19,r17 ;r19存放余數
ret
ddd:
set
ret
;..................................
|