Building a Fraction Calculator for the Commodore 64 in Assembly Language
Tags: commodore64, retrocomputing, assemblylanguage, math
Published: 2023-10-04 06:46:48

---
title: Building a Fraction Calculator for the Commodore 64 in Assembly Language
published: true
description: 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.
tags: #commodore64, #retrocomputing, #assemblylanguage,#math
# cover_image: https://cdn-images-1.medium.com/max/2000/1*_jCrCqcmAj9J5I8_KYvaQQ.png
# Use a ratio of 100:42 for best results.
# published_at: 2023-10-04 06:39 +0000
---

![](https://cdn-images-1.medium.com/max/2000/1*_jCrCqcmAj9J5I8_KYvaQQ.png)

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:

* [A Retro Adventure: Implementing an Aspect Ratio Calculator for the Commodore 64 in Multiple Programming Languages](https://dev.to/medvecky/a-retro-adventure-implementing-an-aspect-ratio-calculator-for-the-commodore-64-in-multiple-programming-languages-349m)

* [80s Time Travel: Commodore 64 Math with Square Root Approximation & Factorization using Assembly](https://dev.to/medvecky/80s-time-travel-commodore-64-math-with-square-root-approximation-factorization-using-assembly-4chg)

* [Creating a Fraction Converter for the Commodore 64 in Assembly Language (6510/6502](https://dev.to/medvecky/creating-a-fraction-converter-for-the-commodore-64-in-assembly-language-65106502-4gbn)

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

![](https://cdn-images-1.medium.com/max/2000/1*eDvUF4oWyMIsOKCR-abi5Q.jpeg)

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.

![](https://cdn-images-1.medium.com/max/2976/1*5NGtYls7J5n8n-v-GlYsLg.png)

![](https://cdn-images-1.medium.com/max/2688/1*sqF8GFdYAb4qA8ogCXIt2w.png)

The source file, complete with macro definitions and constants, is in this [[link to git gist](https://gist.github.com/medvecky/0c5e063f79aa05972b18f4da61e056d8)].

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

![](https://cdn-images-1.medium.com/max/2688/1*_tKW7RLVd9koDLMqDZrXCQ.png)

![](https://cdn-images-1.medium.com/max/2688/1*C9zpEaZcH387kIRqSjwrdA.png)

### 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”.

![](https://cdn-images-1.medium.com/max/2688/1*cwNN2DrK6Hdnk3e53v8Htw.png)

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.

![](https://cdn-images-1.medium.com/max/2000/1*MSd1Gf3ciguFoAB844RnZQ.png)

![](https://cdn-images-1.medium.com/max/2688/1*MB4ALCob6VgYffOjOl4Ryg.png)

The simplification function handles several steps:

![](https://cdn-images-1.medium.com/max/2688/1*M5_hvA4Z7hm4JJGKDUmUHQ.png)

* 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 :

![](https://cdn-images-1.medium.com/max/2688/1*aJl90lWrKBTPuyYsy_FarQ.png)

* Check whether the fraction is proper or not.

* If improper, convert it to mixed:

![](https://cdn-images-1.medium.com/max/2000/1*_00GDCmMKRRjvEECyFwLGw.png)

![](https://cdn-images-1.medium.com/max/2688/1*J75MrwFFpc755ZV1WeIOfQ.png)

* Prints the result of the transformation

![](https://cdn-images-1.medium.com/max/2688/1*pngujHw46YIQ4gerQAb7JQ.png)

### 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:

![](https://cdn-images-1.medium.com/max/2000/1*_YYduflwMa21xsAnjPXqhQ.png)

![](https://cdn-images-1.medium.com/max/2688/1*pjtNadv1_UjZ98-h8jdghQ.png)

### Multiplying and Dividing Fractions

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

![](https://cdn-images-1.medium.com/max/3200/1*dwi-GB1Fq1--Z4SAg7rZ-A.jpeg)

![](https://cdn-images-1.medium.com/max/2688/1*LjiakW7WGZhNkGJJBPyA2A.png)

![](https://cdn-images-1.medium.com/max/2000/1*5VXcIdTaEND7mORBZ_h2uw.png)

![](https://cdn-images-1.medium.com/max/2688/1*eXONun-pgn6k8fuOnHUM3w.png)

### 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:

![](https://cdn-images-1.medium.com/max/2000/1*Ka-2b-enzVyDaid5QtZONw.png)

It is implemented with the following code:

![](https://cdn-images-1.medium.com/max/2688/1*Ju5lvE-Y1HfdQ-JIDpD6yQ.png)

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:

![](https://cdn-images-1.medium.com/max/5000/1*MfxYvvzK30I9TkmaN2vA3w.jpeg)

![](https://cdn-images-1.medium.com/max/2688/1*YcWEWa0XVL2QOARSNeVi3Q.png)

![](https://cdn-images-1.medium.com/max/5000/1*9VNsQSD7sbtXigtriBsRmg.jpeg)

![](https://cdn-images-1.medium.com/max/2688/1*3-6LJWQTnVxS2pyIPVfx7g.png)

## 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:

![](https://cdn-images-1.medium.com/max/2000/1*GzMDurtYhZ1sh-fZQJ01Vg.gif)

The complete source code can be found here:

* Main: [[link to gist](https://gist.github.com/medvecky/e1fd9f9658489a295b91e93187e4885a)]

* Subroutines: [[link to gist](https://gist.github.com/medvecky/01f090aef633d32ec54f5565c2a6d301)]

* Definitions: [[link to gist](https://gist.github.com/medvecky/0c5e063f79aa05972b18f4da61e056d8)]

* Data: [[link to gist](https://gist.github.com/medvecky/51b4b3c8b98a96db6baae862b6dd6304)]

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.

![](https://cdn-images-1.medium.com/max/2048/1*k7pSZIioQgpW3VSsQJMkkQ.png)

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.