標題: AVR 系列單片機匯編 數值運算子程序 [打印本頁] 作者: 522240752@qq.co 時間: 2021-4-5 22:31 標題: AVR 系列單片機匯編 數值運算子程序 ;***************************************************************************
;*
;* "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)
;*
;***************************************************************************
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