I'm using 68k and I can't figure out how you do multiplication, at the moment I'm just using left shifts and addition but there must be another way? Also if I needed to do none integer multiplication I don't have a clue how I'd go about that :(.
Printable View
I'm using 68k and I can't figure out how you do multiplication, at the moment I'm just using left shifts and addition but there must be another way? Also if I needed to do none integer multiplication I don't have a clue how I'd go about that :(.
Sorted it:
MULS.L D1,D2
Sorry no its not sorted, I get an Opperand 1 error when i use that code :(.
I hear assemble math in some cases is alot faster than regular.
It would be great to have a Vector class built with operators working in ASM.
Well in x86 you would use the FPU to do non-int math.Quote:
Originally Posted by Electroman
http://www.ray.masmcode.com/tutorial/index.html
mul for unsigned and imul for signed.
To be honest though, shl and shr is the faster way of doing multiplication and division.
However, shl and shr only works if you are multiplying by a number that is base 2
Yea but as I said the idea was to use Binary shifts to get as close as I could then do a few additions to get to the exact number ;).Quote:
Originally Posted by Darkwraith
e.g.
a *9
Shift 'a' 3 times to the left
Then add 'a' onto that and you have it ;).
So similar to:
?Code:LOCAL a:DWORD
LOCAL b:DWORD
push 9
pop a
mov b,a
shl a,3
add b,a
chem
Yea apart from there would be code to control how many shifts would be required and even possibly code so that when adding on numbers it could reuse numbers passed on the shifts to make it even more efficient. But That was just my idea of making it with just using limited tools, I was more searching for a better way to do it if it exists in a single statement but it seemed not :(.Quote:
Originally Posted by chemicalNova
There could possibly be a 1 line statement which performs this, just not in the 8086 / family of instruction sets.
Try searching for different instruction sets/syntaxes, and reading up on their documentation.
chem
For what its worth I solved this very problem because its a question in my ASM book.
It works on the premise of shifting the multiplicand left by x bits every time you find a 1 in the binary of the multiplier then you just add up all the shifted versions of the multiplicand.
I dare say its farfrom the most efficient longhand method, but it works and I'm proud of it :D.
I'm currently grappling with the bizarre way that MUL and DIV work. :DCode:
;SYNTAX:
; mov eax, 1357
; mov ebx, 371
; CALL Multiply
;-------------------
Multiply PROC USES ecx edi esi
; Multiplies EAX by EBX and leaves the answer in EAX;
;-------------------
mov esi, 0 ; running total
mov ecx, 0 ; the loop counter and bit test value
L1:
bt ebx, ecx ; test the bit
jnc L2 ; jump if the bit is zero
mov edi, eax ; take a copy of the original eax value
shl edi, cl ; shift left
add esi, edi ; add it to the running total
L2:
inc ecx
cmp ecx, 32 ; stop after 32 bits
jb L1
mov eax, esi
ret
Multiply ENDP