[英]Multiplication in avr assembly
我無法理解在AVR代碼中進行二進制乘法的方式。 這是來自avr怪胎的代碼。 “ 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位加法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.