News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

256 bit addition

Started by elefer, December 04, 2006, 05:03:46 PM

Previous topic - Next topic

elefer

Hi,
I want to do 256 bit addition , take two 256 bit numbers from user  and add them .....

I think I can take number and spilt into digits and send to array and add digit by digit... is this good way or there is another better solution for this......

please help....!

Tedd

Dont split into (decimal) digits, convert it into a series of (eight) dwords and then you can add those..

add.. adc.. adc.. adc.. adc.. adc.. adc.. adc..

However, the result could require 257 bits, so you need to take into account the carry after the last addition.


(But converting the string of digits into a 256-bit number will probably be your first challenge :wink)

Although, you 'could' just perform the addition directly on the digits, and that would work, assuming you don't want to do anything other than display the result -- so there's no need to convert to and from the string. (But the addition itself will be slower than done as dwords.)
No snowflake in an avalanche feels responsible.

elefer

Quote from: Tedd on December 04, 2006, 05:29:41 PM
Dont split into (decimal) digits, convert it into a series of (eight) dwords and then you can add those..

add.. adc.. adc.. adc.. adc.. adc.. adc.. adc..

However, the result could require 257 bits, so you need to take into account the carry after the last addition.


(But converting the string of digits into a 256-bit number will probably be your first challenge :wink)

Although, you 'could' just perform the addition directly on the digits, and that would work, assuming you don't want to do anything other than display the result -- so there's no need to convert to and from the string. (But the addition itself will be slower than done as dwords.)


How can I perform the addition directly I couldn't understand....

and please can you give some little explanation how I copy digits from string in DWORS

Ratch

elefer,

QuoteI think I can take number and spilt into digits and send to array and add digit by digit... is this good way or there is another better solution for this......

     You cannot split a binary number into decimal digits.  If you want a decimal number from a binary number, it has to be converted.  Look into multi-precision arithmetic.  Ratch

dsouza123

An example with two fixed decimal number (radix 10) asciiz strings converted to binary
(as two 256 bit numbers) and added together. 
The conversion routine is from a big integer sqrt program I posted a while ago in the Laboratory.

[attachment deleted by admin]

Tedd

To perform the addition 'directly' you take the lowest digit from each number-string, convert them from characters to values (just subtract 30h), and then add them together to give the value of the lowest digit of the result number-string (add 30h to turn it back into a digit character).
However, when you add two digits together, the result could be above 9 and so will not fit into one digit, so you need to "carry" the extra ten over into the addition for the next set of digits (just as you do with addition by hand) - and then subtract 10 from 'this' value to get the lowest digit's value.
So the second addition works the same, except you need to add one more, if the previous addition went above 9.
And so on, for each of the other digits.


Small Example:

(A) = "8624"
(B) = "1735"

(A) + (B) = ...


4 + 5 = 9  (lowest digit of answer)
2 + 3 = 5
6 + 7 = 13 (TWO digits -- so this digit is really '3' and we carry the 1 for the next addition)
8 + 1 (+1) = 10 (TWO digits!! -- use '0' and carry 1)
(+1) = 1

Answer = 10359


(Don't forget that CHARACTERS are not VALUES - so you have to convert between the two; which I left out of this example to make it easier to understand.)
No snowflake in an avalanche feels responsible.