簡體   English   中英

x86匯編語言:64位答案的移位乘法

[英]x86 Assembly Language: Shift multiplication with 64 bit answer

    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,但這就是我目前所能確定的。 任何幫助將不勝感激,解釋多於答案。

我認為應該這樣做:

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

請注意,這會將參數視為無符號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM