0
votes
    NOTICE: This question does relate to my homework/classwork. The textbook is poorly written so I can't really rely on it.

I am using linux x86 Assembly Language and I am trying to figure out how to multiply two 32-bit numbers together using the shift operand. I also need to find a way to store the 64-bit answer into two separate registers since each register is only 32 bits. I know that shifting to the left once is equivalent of multiplying by two and shifting to the right is dividing by two but that's all I'm sure of at the moment. Any help would be appreciated, explanations moreso than answers.

1

1 Answers

1
votes

I think this should do it:

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

Note that this treats the arguments as unsigned.