#### xtatic44

• Guest
« on: November 21, 2013, 05:16:35 PM »
Hello, I have a question about unpacked BCD addition and then storing it in an array.

For example I have 3 arrays:

.data
arr1   DB 5 dup (0),9
arr2   DB 5 dup (0),9
arr3   DB 5 dup (0)

I get the 5th element of the two arrays (9,9) and then add those two together (18). My question is, how do I store the individual digits 1 and 8 into the third array?
So that the 5th element in arr3 is 8 and the 4th element is 1.

Also, I saw a question in the archive that was similar to this but what I didn't understand was how the digits were stored.

#### dedndave

• Member
• Posts: 8829
• Still using Abacus 2.0
« Reply #1 on: November 21, 2013, 10:33:37 PM »
Code: [Select]
`.dataarr1   DB 4 dup (0),9arr2   DB 4 dup (0),9arr3   DB 5 dup (0)`notice that, now, the 9's are the 5th element in arrays 1 and 2

since the days of the 8008, intel has provided special instructions to help handle simple BCD math

even though the "ASCII" names imply the numbers are ASCII, they aren't   :P
they are unpacked BCD numbers that may easily be converted to ASCII when done
these instructions generally work on 2 digits at a time, one in AL, one in AH

the "decimal" instructions are used with packed BCD values
packed BCD has 2 digits in each byte, one in the lower 4 bits, one in the upper 4 bits
there are no DAM or DAD instructions

Code: [Select]
`    mov     al,arr1[4]    ;the 5th element of array 1    mov     ah,0    add     al,arr2[4]    aaa`the "ones" digit is in AL, the "tens" digit is in AH

there is a shortcut you can use to load bytes and clear higher-order bytes in a register
Code: [Select]
`    movzx   eax,byte ptr arr1[4]   ;zero-extend a byte into EAX    add     al,add2[4]    aaa`
the AAA instruction also works for ADC

you can replace the array indexes with a register that holds a variable index
Code: [Select]
`    mov     edx,4    movzx   eax,byte ptr arr1[edx]    add     al,arr2[edx]    aaa`
you can create a loop, you just have to move AH into AL after storing each result digit, and adding it into the next digit

you can use google to find descriptions of all the BCD instructions
« Last Edit: November 22, 2013, 12:48:31 AM by dedndave »

#### FORTRANS

• Member
• Posts: 1109
« Reply #2 on: November 22, 2013, 12:31:03 AM »
Hi,

You can go to "Forum Links", then "Browse The Old UK Forum",
and search for "Unpacked BCD".  You will find treads named:

using decimals for huge numbers

and

Fibonacci Numbers Using Arrays

Dave and I cover BCD arithmetic in those.

Regards,

Steve

#### Gunther

• Member
• Posts: 3723
• Forgive your enemies, but never forget their names
« Reply #3 on: November 22, 2013, 05:27:42 AM »
Hi  xtatic44,

here are some links: decimals for huge numbers, Fibonacci Numbers Using Arrays, String vs. SSE and FPU. Hope that helps.

Gunther
Get your facts first, and then you can distort them.

#### FORTRANS

• Member
• Posts: 1109
« Reply #4 on: November 22, 2013, 07:36:19 AM »
Hi Gunther,

Thank you.  I guess I got lazy.

Cheers,

Steve N.

#### Gunther

• Member
• Posts: 3723
• Forgive your enemies, but never forget their names
« Reply #5 on: November 22, 2013, 07:43:00 AM »
Hi Steve,

Hi Gunther,

Thank you.  I guess I got lazy.

Cheers,

Steve N.

really?  :lol:
Joke apart, I've another impression.

Gunther
Get your facts first, and then you can distort them.

#### xtatic44

• Guest
« Reply #6 on: November 22, 2013, 08:13:48 AM »
Thank you dedndave, FORTRANS and Gunther. I was able to make my program work with the help of those links and with your instructions dedndave.

I'm actually trying to make a Fibbonacci program using arrays and I did come across the aaa instruction when googled unpacked BCD, but I had no idea how it worked until now.

And I was wondering why it they call it ASCII, it didn't look ASCII the first time I saw it.  :lol:

Thanks again

#### dedndave

• Member
• Posts: 8829
• Still using Abacus 2.0