x86汇编语言:使用64位应答移位乘法

时间:2015-04-26 19:09:45

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

1 个答案:

答案 0 :(得分: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

请注意,这会将参数视为unsigned。