LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-04-2004, 09:58 PM   #1
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Rep: Reputation: 34
multipling 64bit data type in assembly


Hi all


I have to multiply a 64-bit Qword data type using assembly language on a 32bit processor:

now we know
that
mul instruction, uses that edx register to store the portion of the result which does not fit in the eax register.
hence
edx:eax makeup the result of an multiplication
now
if the data type is 64 bit
I but the upper 32 bit in edx
and the lower 32 bit in eax

when I multiply say by 10 the edx register is overwritten and I only get the result of eax*10 not edx:eax *10

so how should i go about it?
 
Old 12-05-2004, 03:31 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,539

Rep: Reputation: 149Reputation: 149
You can't do this with just two 32-bit registers. Result of multiplying two 64 bit numbers may be up to 128 bits. It means four registers only for the result. You need to divide it into four multiplications (the same way you use to multiply long numbers on paper, but this time for hexadecimals) and add the results in the right way.
Code:
    A  B
  x C  D
--------
 
Old 12-06-2004, 01:45 AM   #3
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Original Poster
Rep: Reputation: 34
show me how?
 
Old 12-06-2004, 05:41 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,539

Rep: Reputation: 149Reputation: 149
It's quite simple. You ahev two 64 bit values. One you represent as A B (A is higher 32 bit, B lower 32 bit), second as C D. You can multiply them this way:
Code:
        A    B
      x C    D
----------------
       AD   BD
  + AC BC
----------------
AC  AD+BC  BD
Seems simple but there's one problem. Every multiplication of two 32 bit values can give max 64 bit result. So AC, BD etc are not just 32 bit values. You'd need to correct the result. It will be:
lowest byte of the result = lower 32 bit of B*D
second byte of the result = lower 32 bit of (A*D+B*C+higher 32 bit of B*D)
third byte of the result = lower 32 bit of (A*C+higher 32 bit of (A*D+B*C+higher 32 bit of B*D))
fourth byte (highest 32 bits) of the result = higher 32 bit of (A*C+higher 32 bit of (A*D+B*C+higher 32 bit of B*D))

So you'd need to make four multiplications: A*C, A*D, B*C, B*D. Their results will be in two registers each. When you have them (8 registers total, so you'd probably need to use stack), calculate the result using the formulas as above. Please check them, so I might have made a mistake somewhere. I hope that the idea is clear.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is pointer a data-type or what aditya Programming 39 02-09-2014 01:27 AM
Data type conversion in C zaichik Programming 6 09-10-2005 06:47 PM
strange data type in c... HELP alaios Programming 9 09-04-2005 12:40 AM
How to do type casting in Sparc Assembly Language foxele Programming 1 10-09-2004 05:40 PM
Data Type 9 bob Linux - Newbie 1 02-10-2001 09:10 AM


All times are GMT -5. The time now is 07:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration