mettemfurfur000 / fasm-bcd-mul

0 stars 0 forks source link

A suggestion to add packed bcd numbers. #1

Closed egor-erm closed 1 month ago

egor-erm commented 2 months ago

Is it possible to add a multiplication and addition function for packed numbers when 1 byte contains 2 digits at once? In the picture you can see the representation of the number 1234 in this form. image

The code for converting two-digit decimal numbers to packed BCD and back.

char BcdToDecimal(char bcd) {
    return (char)((((bcd >> 4) & 0x0F) * 10) + (bcd & 0x0F));
}

char DecimalToBcd(char decimal) {
    return (char)((decimal / 10) * 16) + (decimal % 10);
}
mettemfurfur000 commented 2 months ago

as i know my bcd addition functions already works with packed digits as i use daa to adjust the result of adding two packed decimal digits

but multiplication only works with unpacked digits, because of aam, as said in fasm manual at section 2.1.4 Decimal arithmetic instructions

https://flatassembler.net/docs.php?article=manual

these guys also explained why aam only works with unpacked numbers

https://retrocomputing.stackexchange.com/questions/14990/the-8086-has-aam-ascii-adjust-after-multiply-why-not-the-same-for-packed-bcd

looks like aam thing is as old as x86 instruction set, and you can find more info about it here

https://www.felixcloutier.com/x86/aam

so you could just unpack numbers before multiplying them and then pack them back...

also idk why would you need bcd to decimal convertors, but i hope anything of these will help you

egor-erm commented 1 month ago

Thanks, in the end I converted the bcd from packaged to unpacked, multiplied and converted back.