linux - x86 汇编语言 : Shift multiplication with 64 bit answer

标签 linux assembly x86 bit-shift multiplication

    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 位。我知道向左移动一次相当于乘以二,向右移动一次等于除以二,但这就是我目前所能确定的。任何帮助将不胜感激,解释多于答案。

最佳答案

我认为应该这样做:

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

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

关于linux - x86 汇编语言 : Shift multiplication with 64 bit answer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29882226/

相关文章:

c - 如何短语修剪字符值并删除第一个点前面的所有内容?

c - 在 32 位机器上实现 64 位算法

c++ - 我可以使用 cmpxchg16b 以原子方式将指针复制到指针和 int,同时递增 int(原子引用计数)吗?

assembly - 结构体如何在汇编中作为参数传递

组装事业部

assembly - 什么是学习MASM代码(不是HLA)的好资源?

ruby-on-rails - ruby rails : How can I specify runner script environment

php - Codeigniter 通过命令行访问方法

assembly - MIPS汇编中的两条顺序分支指令?

linux - 如何将 vim 设置为默认的 cscope 编辑器?