2015-04-26 255 views
0
NOTICE: This question does relate to my homework/classwork. The textbook is poorly written so I can't really rely on it. 

我正在使用linux x86 x86彙編語言,並試圖找出如何使用shift操作數將兩個32位數字相乘。 我還需要找到一種方法將64位答案存儲到兩個單獨的寄存器中,因爲每個寄存器只有32位。我知道,向左移一次相當於乘以二,向右移兩分,但這是我現在確定的。任何幫助將不勝感激,解釋不僅僅是答案。x 86彙編語言:移位乘以64位答案

+1

查看http://courses.cs.vt.edu/~cs1104/BuildingBlocks/multiply.040.html –

回答

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 

注意這把參數爲無符號。

+0

好的信息到目前爲止,但我怎麼會做一些像500萬* 200萬和他們的結果存儲在EAX和edx? (低位進入eax,高位進入edx)。 – ProgrammingNoob

+0

@ProgrammingNoob:如果在我的代碼開始時,'%edi'包含500萬,'%esi' 200萬,函數結束時'%edx'將包含結果的高位,'% eax的低位。 – EOF

+0

噢好吧,對不起,我的閱讀理解技巧最近讓我失望了很多。 – ProgrammingNoob