标题: 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