The MASM Forum

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

Title: Unpacked BCD addition
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.

Title: Re: Unpacked BCD addition
Post by: dedndave on November 21, 2013, 10:33:37 PM
.data
arr1   DB 4 dup (0),9
arr2   DB 4 dup (0),9
arr3   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

AAA ASCII adjust after addition
AAS ASCII adjust after subtraction
AAM ASCII adjust after multiplication
AAD ASCII adjust before division

DAA decimal adjust after addition
DAS decimal adjust after subtraction

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

so, to answer the addition question
    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
    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
    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
Title: Re: Unpacked BCD addition
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
Title: Re: Unpacked BCD addition
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
Title: Re: Unpacked BCD addition
Post by: FORTRANS on November 22, 2013, 07:36:19 AM
Hi Gunther,

   Thank you.  I guess I got lazy.

Cheers,

Steve N.
Title: Re: Unpacked BCD addition
Post by: Gunther on November 22, 2013, 07:43:00 AM
Hi Steve,

Quote from: FORTRANS on November 22, 2013, 07:36:19 AM
Hi Gunther,

   Thank you.  I guess I got lazy.

Cheers,

Steve N.

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

Gunther
Title: Re: Unpacked BCD addition
Post by: xtatic44 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
Title: Re: Unpacked BCD addition
Post by: dedndave on November 22, 2013, 08:23:50 AM
all the documentation i remember seeing refers to it as "ASCII"
it was developed long ago - who knows
the original designers may have intended "Arithmetic" or even "Accounting"   :P