【问题标题】:Multiplication in avr assemblyavr汇编中的乘法
【发布时间】:2016-05-12 23:43:57
【问题描述】:

我无法理解二进制乘法在 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

【问题讨论】:

标签: assembly binary avr multiplication shift


【解决方案1】:

如您所知,乘法可以使用移位和加法来执行,例如

   101
   101
   ===
   101
1 01
= ====
1 1001

请注意,虽然数字适合 4 位,但我需要使用更多位进行加法。为避免这种情况,您引用的代码会移动结果而不是要添加的数字。

  101
  --- -
   10 1
   -- --
    1 01
  101
  ===
  110 01
  --- --
   11 001
   -- ----
    1 0001

这种方式只需要4位加法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多