avr汇编中的乘法
Multiplication in avr assembly
我无法理解二进制乘法在 avr 代码中发生的方式。这是来自 avr freaks 的代码。 "ror" 和 "lsr" 到底是做什么的?我认为对于二进制乘法,我们需要 lsl。
.def mc8s =r16 ;multiplicand
.def mp8s =r17 ;multiplier
.def m8sL =r17 ;result Low byte
.def m8sH =r18 ;result High byte
.def mcnt8s =r19 ;loop counter
mpy8s: sub m8sH,m8sH ;clear result High byte and carry
ldi mcnt8s,8 ;init loop counter
m8s_1: brcc m8s_2 ;if carry (previous bit) set
add m8sH,mc8s ; add multiplicand to result High byte
m8s_2: sbrc mp8s,0 ;if current bit set
sub m8sH,mc8s ; subtract multiplicand from result High
asr m8sH ;shift right result High byte
ror m8sL ;shift right result L byte and multiplier
enter code here
dec mcnt8s ;decrement loop counter
enter code here
brne m8s_1 ;if not done, loop more
ret
如您所知,可以使用移位和加法来执行乘法,例如
101
101
===
101
1 01
= ====
1 1001
请注意,虽然数字适合 4 位,但我需要使用更多位来进行加法运算。为避免这种情况,您引用的代码会移动结果而不是要添加的数字。
101
--- -
10 1
-- --
1 01
101
===
110 01
--- --
11 001
-- ----
1 0001
这样只需要4位加法
我无法理解二进制乘法在 avr 代码中发生的方式。这是来自 avr freaks 的代码。 "ror" 和 "lsr" 到底是做什么的?我认为对于二进制乘法,我们需要 lsl。
.def mc8s =r16 ;multiplicand
.def mp8s =r17 ;multiplier
.def m8sL =r17 ;result Low byte
.def m8sH =r18 ;result High byte
.def mcnt8s =r19 ;loop counter
mpy8s: sub m8sH,m8sH ;clear result High byte and carry
ldi mcnt8s,8 ;init loop counter
m8s_1: brcc m8s_2 ;if carry (previous bit) set
add m8sH,mc8s ; add multiplicand to result High byte
m8s_2: sbrc mp8s,0 ;if current bit set
sub m8sH,mc8s ; subtract multiplicand from result High
asr m8sH ;shift right result High byte
ror m8sL ;shift right result L byte and multiplier
enter code here
dec mcnt8s ;decrement loop counter
enter code here
brne m8s_1 ;if not done, loop more
ret
如您所知,可以使用移位和加法来执行乘法,例如
101
101
===
101
1 01
= ====
1 1001
请注意,虽然数字适合 4 位,但我需要使用更多位来进行加法运算。为避免这种情况,您引用的代码会移动结果而不是要添加的数字。
101
--- -
10 1
-- --
1 01
101
===
110 01
--- --
11 001
-- ----
1 0001
这样只需要4位加法