2
\$\begingroup\$

I have this digital circuit design problem where we receive two two-digits numbers in ASCII, I have to convert them to binary, add them, and the result I have to encode it to ASCII again.

I actually did all the ASCII to binary and the addition part easily with multipliers and adders, however, after that, I have, at the most, an 8-bit binary number that I need to convert back to its ASCII equivalent and I can't seem to find the solution easily.

For example, I could do some selections if the number is bigger than 100 and some subtractions, but I don't really know how to get the tens of the number.

I must use combinational circuits, so basically logic gates or CI's of logic gates like multiplexers, adders...

\$\endgroup\$
5
  • 2
    \$\begingroup\$ This is why many early calculators (and some early computers) did math in BCD rather than binary. Avoids the problem altogether. \$\endgroup\$
    – Dave Tweed
    Apr 2, 2022 at 1:15
  • 1
    \$\begingroup\$ Is your ASCII 7-bit? Or 8-bit? Not too terribly important. But I'm asking, anyway. What you have is, effectively, two BCD input digits that need to be converted to binary for binary addition purposes. \$\endgroup\$
    – jonk
    Apr 2, 2022 at 8:20
  • 2
    \$\begingroup\$ Use BCD as an intermediate step. Binary-BCD is easily searchable, you may find "double dabble algorithm" useful. \$\endgroup\$
    – user16324
    Apr 2, 2022 at 9:35
  • \$\begingroup\$ Isaac, I already wrote up a page that can be used to convert from binary to ASCII. It's here. The outputs are grouped into 4-bit BCD. But that is easily converted to ASCII with just a few added wires. \$\endgroup\$
    – jonk
    Apr 2, 2022 at 17:28
  • \$\begingroup\$ Isaac, for example, here's a simulation converting binary <255> into three digits expressed as 8-bit ASCII <'2' '5' '5'>. \$\endgroup\$
    – jonk
    Apr 2, 2022 at 17:42

2 Answers 2

3
\$\begingroup\$

I actually did all the ASCII to binary and the addition part easily with multipliers and adders

That's quite complicated. You don't need multipliers. You have a number in the BCD form, and you can easily just do the addition in BCD as well.

  1. ASCII to binary-coded-decimal (BCD) is trivial: you just leave the 4 lower bits.

  2. The addition can be done using a separate adder for each pair of digits.

  3. Decimal adjustment is done for each digit of the result - the equivalent of DAA opcode on many CPUs.

  4. The decimal adjust detection is the also the decimal carry signal.

  5. And now you got the 4 lower bits of each ASCII digit. The upper bits remain unchanged and can be passed straight from the input, so we can set them aside since their "circuit" is just straight wires from input to output.

BCD adjustment is done as follows: the sum is compared to 10. Such comparison is done by subtracting 10. Instead, we add twos-complement -10 = 0110b. That also happens to be +6! So subtracting 10 and adding 6, modulo 16, are the same operation. If the sum is >9, or >15, the 6 is added to it. Since we already have the result of adding 6, we just choose between the original result of addition, and the result with the 6 added to it.

The >9 or >15 condition from the BCD adjuster also is the decimal carry signal and is fed to the adder for the next digit.

You can play with the simulation below by selecting any of the input digits with the mouse, so that it turns red, and pressing space to toggle it.

As shown, the adder performs 99+42=141.

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
2
\$\begingroup\$

What you're looking for is called a "binary to BCD" circuit. There are ICs for this.
You need to convert the 8 bit binary natural number to three 4 bit binary coded decimal digits (and of course add the ASCII value of '0' to each output).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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