Results 1 to 11 of 11

Thread: How do you do Multiplication?

  1. #1

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    How do you do Multiplication?

    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 .
    Last edited by Electroman; Jan 12th, 2005 at 11:07 AM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  2. #2

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: How do you do Multiplication?

    Sorted it:
    MULS.L D1,D2
    Last edited by Electroman; Jan 12th, 2005 at 11:08 AM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  3. #3

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: How do you do Multiplication?

    Sorry no its not sorted, I get an Opperand 1 error when i use that code .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  4. #4
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: How do you do Multiplication?

    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.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  5. #5
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    322

    Re: How do you do Multiplication?

    Quote Originally Posted by Electroman
    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 .
    Well in x86 you would use the FPU to do non-int math.
    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.
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  6. #6
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004

    Re: How do you do Multiplication?

    However, shl and shr only works if you are multiplying by a number that is base 2
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  7. #7

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: How do you do Multiplication?

    Quote Originally Posted by Darkwraith
    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 .

    e.g.

    a *9

    Shift 'a' 3 times to the left
    Then add 'a' onto that and you have it .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  8. #8
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: How do you do Multiplication?

    So similar to:
    Code:
    LOCAL a:DWORD
    LOCAL b:DWORD
    
    push 9
    pop a
    mov b,a
    shl a,3
    add b,a
    ?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  9. #9

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: How do you do Multiplication?

    Quote Originally Posted by chemicalNova
    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 .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  10. #10
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: How do you do Multiplication?

    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

    Visual Studio 6, Visual Studio.NET 2005, MASM

  11. #11
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: How do you do Multiplication?

    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 .

    Code:
    ;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
    I'm currently grappling with the bizarre way that MUL and DIV work.
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width