47

I have found an excellent programmer named Steve Wozniac

 4 years ago
source link: https://www.tuicool.com/articles/zqeIzq6
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A long time ago, when computing was a big boring business, something unexpected happened. Cheap microprocessors, intended for teletypes and stop-lights, were pressed into service as  personal computers by young hackers. People like Steve Wozniak. They took the limitations of their computers as a challenge, sat down, and made these tiny machines do impressive things. This is one of those things, published in the August 1976 issue of Dr Dobbs Jounal .

2QJ3YzY.png!web

It's a set of routines for arithmetic on real numbers. The microprocessor here (the 6502, as used in the Apple I and II) could only operate on bytes, that, is whole numbers between 0 and 255. Even worse, it could only add and subtract them. But with this library you could compute 1.2627 - 1099.56, or even the square root of  pi . Amazingly, Wozniac fit the basic functions (add, subtract, multiply and divide) into 239 bytes, using just 127 instructions total.

That's some pretty impressive coding, and having read through it a few times, it's ingenious. For example both the multiply and divide routines loop 23 times, once for each bit in the result. Does Woz waste two bytes by writing the constant 23 in both places? Of course not! He loads the constant 23 in a prefix routine that both share:

vYb6fmM.png!web

As another example, the algorithms need to take the absolute value of M1 and M2. You might write

​M1 = abs(M1);
M2 = abs(M2);

Woz doesn't repeat himself like that. He writes one routine that takes the absolute value of M1, then swaps M1 and M2. He then calls that routine twice , so the outcome is the same with half the code. Better yet, swapping M1 and M2 is needed elsewhere, so that section does double duty.

this piece of 6502 amazingness

Enough of the history. Why was I scrutinizing this ancient code in 2019? I am working on a new companion to  I²CDriver and SPIDriver , and it needs floating-point running on an 8-bit CPU, the 8051-based Silicon Labs EFM8 that my previous projects have used. It's now happily performing floating point computations just like the 6502 library. I didn't mange to keep it as short as the original, but it is fairly quick now, a little faster than the Keil equivalent . Great code never dies!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK