📜 ⬆️ ⬇️

I found a great programmer named Steve Wozniak

Long ago, when computers were large and business was boring, something unexpected happened. Young hackers have found a way to assemble personal computers on cheap microprocessors from teletypes and traffic lights. One of them was Steve Wozniak. These guys took the limitations of their computers as a challenge, sat down and forced these tiny chips to do amazing things. Here is what Dr Dobb's Journal published in August 1976 :



This is a set of arithmetic procedures on real numbers. The microprocessor (6502, the same as in Apple I and II) could only work with bytes, that is, integers between 0 and 255. Worse, it could only add and subtract them. But with the help of this library you can calculate 1.26271099.56or even take the square root of pi. Surprisingly, the author of the program by the name of Steve Wozniak fitted the main functions (addition, subtraction, multiplication and division) into 239 bytes, using a total of 127 instructions.

This is a pretty impressive programming pattern. After reading it several times, I can say that it is simply brilliant. For example, the cycles of the multiplication and division procedures work 23 times, once for each bit in the result. Do you think RR spends two bytes, writing the constant 23 in both places? Of course not! It loads constant 23 into a preliminary subroutine that both cycles use:
')


As another example, algorithms should take absolute values ​​of M1 and M2. Could you write

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

This is not the case. He writes one procedure that takes the absolute value of M1, and then changes M1 and M2. It then calls this procedure twice , so the same result is achieved with half the amount of code. Moreover, the replacement of M1 and M2 is also needed elsewhere, so the code does double work.

Of course, such intensive factoring makes it difficult to track the flow, but it is amazing how far this code is ahead of its time. Reading other articles in the issue of 1976, you can find a lot of not very well written code. This is understandable: people just figured out how to take advantage of the new microprocessors. And in the midst of all this diamond, which may well be worthy of a modern hacker from the demostsen. For comparison, here is the same level of total ingenuity as in this amazing code for 6502 , written 30 years later.

Enough of the story. Why did I thoroughly study this ancient code in 2019? The fact is that I am working on a new companion for I²CDriver and SPIDriver , and he had to perform floating point operations on an 8-bit Silicon Labs EFM8 processor (based on 8051), which I used in previous projects. Now he easily performs these calculations, just like the 6502 library. I didn’t need so small and optimized code, but now it works quite quickly, a little faster than the similar Keil library . Great code never dies!

Source: https://habr.com/ru/post/459584/


All Articles