-2
\$\begingroup\$

I am programming a calculator in assembly language with the MSP430g2553. I have a 4x4 membrane keypad and an 2x16 LCD. I already can display numbers in the LCD with the keypad. The issue is that the number has an ascii code. How can I store the decimal or hex value of the number presented in a register so I can add, subtract, multiply or divide it with other number?

\$\endgroup\$
  • \$\begingroup\$ I think that ancient calculators worked with BCD registers and operations. But today we have parsers. \$\endgroup\$ – venny Nov 27 '15 at 19:45
  • \$\begingroup\$ you got to get to work an lcd in asm and you're asking this? \$\endgroup\$ – Vladimir Cravero Nov 27 '15 at 20:09
  • \$\begingroup\$ Maybe seems easy to you right now, but not me. Initializing the LCD in asm wasnt easy too. \$\endgroup\$ – EMPV Nov 27 '15 at 20:29
  • \$\begingroup\$ You subtract 0x30 from an ASCII digit ('0'..'9') to get a BCD digit 0..9. You can pack the digits and do BCD math or convert BCD to binary and do binary math. BCD math has the advantage that you can represent numbers such as 0.1 exactly you don't have to convert to input and display. Binary math is probably easier to code. \$\endgroup\$ – Spehro Pefhany Nov 27 '15 at 20:29
  • \$\begingroup\$ I already got the digits, how do I packed them? \$\endgroup\$ – EMPV Nov 28 '15 at 0:37
2
\$\begingroup\$

You store your numbers internally as binary or BCD and then convert that to ASCII at display time. BCD is easier to convert for display.

For computations the binary representation is likely to be more well suited to calculations you write up yourself following online algorithms.

On the other hand BCD is going to be easier to adapt to computation with larger number of digits than you can represent in the 9 digits you can get with 32-bit binary.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.