# The MASM Forum

## Miscellaneous => 16 bit DOS Programming => Topic started by: xtatic44 on November 21, 2013, 05:16:35 PM

Post by: xtatic44 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.

Post by: dedndave 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
Post by: FORTRANS 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
Post by: Gunther on November 22, 2013, 05:27:42 AM
Hi  xtatic44,

here are some links: decimals for huge numbers (http://www.masmforum.com/board/index.php?topic=18146.0), Fibonacci Numbers Using Arrays (http://www.masmforum.com/board/index.php?topic=12771.0), String vs. SSE and FPU (http://www.masmforum.com/board/index.php?topic=15082.0). Hope that helps.

Gunther
Post by: FORTRANS on November 22, 2013, 07:36:19 AM
Hi Gunther,

Thank you.  I guess I got lazy.

Cheers,

Steve N.
Post by: Gunther 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