DEV Community

Alexey Medvecky
Alexey Medvecky

Posted on

Building a Fraction Calculator for the Commodore 64 in Assembly Language

I will guide you through creating a fraction calculator for the Commodore 64 using assembly language in this article. We will utilize KickAssembler, primarily its macros, to simplify the process.

Formulating the Problem

My journey through the 80s using the Commodore 64 to tackle mathematical problems continues. Previous parts of my journey:

Fractions have always been a challenge for calculators, and even in the 80s, their support was limited.

Let’s leverage the power of the Commodore 64 to solve this mathematical puzzle.
In this part, we’ll dive into creating a fraction calculator, a task that was particularly challenging back in the 80s. Follow along to see how I use the Commodore 64’s capabilities to overcome this challenge.

Solution.

As previously mentioned, the 6510 processor lacks support for floating-point operations, so we’ll rely on the software implementation from BASIC ROM. I’ll use KickAssembler and its macro capabilities to expedite the development process, a luxury that may not have been available with assemblers of the 80s.
The most commonly used macros for arithmetic operations and a macro for modulo division will be essential components of our solution.

The source file, complete with macro definitions and constants, is in this [link to git gist].

The program begins by displaying and processing menu selections, where the active use of macros becomes apparent.

Entering a String Representation of a Number

In this project, I’ve revamped the routine for the string representation of numbers, ensuring it processes only valid characters for our program. This includes the numbers 0 to 9, “-”, “return”, and “del”.

Here, in addition to the exact comparison based on the zero flag, a comparison of “more” and “less” is used based on the carry flag.
This level of detail highlights the need for a deep understanding of the processor’s operation. It’s moments like these that make me love it.

Fraction Simplification and Result Display

Simplifying a fraction is the core routine we’ve implemented first because other routines rely on it. The key to simplifying fractions is finding the greatest common divisor (GCD), which we accomplish using the Euclidean algorithm.

The simplification function handles several steps:

  • The routine receives arguments from the user.

  • The routine checks whether the denominator is zero.

  • The routine copies them and passes them to the GCD routine.

  • The routine divides the numerator and denominator by the GCD result.

  • The routine outputs the result with the following function :

  • Check whether the fraction is proper or not.

  • If improper, convert it to mixed:

  • Prints the result of the transformation

Converting a Mixed Fraction to an Improper Fraction

Our program also supports converting mixed fractions to improper fractions using a well-defined algorithm. The implementation details are provided there:

Multiplying and Dividing Fractions

Multiplication and division of fractions are relatively straightforward operations, each with its dedicated function:

Adding and Subtracting Fractions

Adding and subtracting fractions are more complex due to the need to find a common denominator. We’ve tackled this challenge by implementing a casting operation based on GCD, enabling us to perform addition and subtraction efficiently.

The casting operation is done using the following formula:

It is implemented with the following code:

The next happens in the code:

  • Entering the numerator and denominator of both fractions.

  • Denominators are checked for zero.

  • Denominators are passed as arguments to the GCD routine.

  • The common denominator and multipliers for the numerators are found based on the resulting GCD.

  • Fractions are reduced to a common denominator.

When the casting function is ready, addition and subtraction are implemented by this code:

Conclusion

The final executable file is 8322 bytes in size, which may seem significant for a calculator, but future optimization is possible.

The program works as follows:

The complete source code can be found here:

This program not only serves as a powerful tool but also brings back memories of school mathematics. I’ve thoroughly enjoyed this journey into the 80s and plan to continue exploring the capabilities of the Commodore 64 for mathematical challenges. If you’re interested, stay tuned for more adventures.

In this endeavor, I’ve deviated from the norm using macros, which greatly expedited development and made the process more enjoyable. I’ve created a tool I missed during my school days, and it’s a testament to the power of the Commodore 64. Join me in this journey through the 80s, where we continue to harness the capabilities of this remarkable machine for mathematical purposes.

Top comments (0)