【问题标题】:x86 Assembly Language: Shift multiplication with 64 bit answerx86 汇编语言:带 64 位答案的移位乘法
【发布时间】:2015-07-05 02:36:00
【问题描述】:
    NOTICE: This question does relate to my homework/classwork. The textbook is poorly written so I can't really rely on it.

我正在使用 linux x86 汇编语言,我正在尝试弄清楚如何 使用移位操作数将两个 32 位数字相乘。 我还需要找到一种方法将 64 位答案存储到两个单独的寄存器中,因为每个寄存器只有 32 位。我知道向左移动一次相当于乘以 2,向右移动相当于除以 2,但这就是我目前所确定的。任何帮助将不胜感激,解释比答案更重要。

【问题讨论】:

标签: linux assembly x86 bit-shift multiplication


【解决方案1】:

我认为应该这样做:

mult:
        #save all caller-saved regs
        #move arg1 to %edi, arg2 to %esi

        xorl    %eax, %eax          #\
        xorl    %edx, %edx          #--clear a 64-bit accumulator
        movl    $31,  %ecx          #set up shift-count 

.L1:    movl    %edi, %ebx          #copy of arg1
        xorl    %ebp, %ebp          #zero scratch-register
        shll    %cl,  %ebx          #isolate bit from arg1
        sarl    $31,  %ebx          #and turn into mask
        andl    %esi, %ebx          #AND arg2 with bitmask
        xorl    $31,  %ecx          #invert shift-count
        shldl   %cl,  %ebx, %ebp    #shift upper bits into scratch-reg
        shll    %cl,  %ebx          #adjust lower bits
        addl    %ebx, %eax          #\
        addl    %ebp, %edx          #-- accumulate results
        xorl    $31,  %ecx          #restore shift-count
        decl    %ecx                #change shift to next bit
        jno .L1                     #if ecx == -1, done!

        #restore caller-saved regs
        #done, return value in edx:eax

请注意,这会将参数视为无符号。

【讨论】:

  • 到目前为止还不错,但是我将如何做 500 万 * 200 万的事情并将其结果存储在 eax 和 edx 中? (低位到 eax,高位到 edx)。
  • @ProgrammingNoob:如果在我的代码开头,%edi 包含 500 万,%esi 200 万,则函数末尾的 %edx 将包含结果,%eax 低位。
  • 哦,好的,很抱歉我的阅读理解能力最近一直很失败。
  • 如果这对您有用,请考虑支持和接受。
  • 我试过了,我刚刚创建了这个帐户,所以我没有足够的代表 (15) 来投票,对不起!
猜你喜欢
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2010-10-09
  • 1970-01-01
  • 2017-10-20
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多