https://mcyoung.xyz/2022/07/24/curta/ Yeah, I drew this. Check out my art blog. mcyoung I'm Miguel. I write about questionable C++ and Rust code. I also have an art blog linked below. --------------------------------------------------------------------- Home * About * Posts * Tags Art * GH * Twt * Resume (c) 2022 Miguel Young de la Sota CC BY-SA #etc #assembly 2022-07-24 3Hz Computer, Hold the Transistors I'm not really one to brag publicly about expensive toys, but a few weeks ago I managed to get one that's really something special. It is a Curta Type II, a mechanical digital^1 calculator manufactured in Liechtenstein between the 50s and 70s, before solid-state calculators killed them and the likes of slide-rules. I have wanted one since I was a kid, and I managed to win an eBay auction for one. The Curta The Curta Type II (and Solomon the cat) It's a funny looking device, somewhere between a peppermill and a scifi grenade. Mine has serial number 544065, for those keeping score, and comes in a cute little bakelite pod (which has left hand thread?!). I wanna talk about this thing because unlike something like a slide rule, it shares many features with modern computers. It has operations, flags, and registers. Its core primitive is an adder, but many other operations can be built on top of it: it is very much a platform for complex calculations. I'm the sort of person who read Hacker's Delight for fun, so I really like simple numerical algorithms. This article is a survey of the operation of a Curta calculator and algorithms you can implement on it, from the perspective of a professional assembly programmer. Many of the algorithms I'm going to describe here exist online, but I've found them to be a bit difficult to wrap my head around, so this article is also intended as a reference card for myself. Let's dive in! A Well-Lubricated ALU There are two Curta models, Type I and Type II, which primarily differ in the sizes of their registers. I have a Type II, so I will focus on the layout of that one. The Curta is not a stored program computer like the one you're reading this article on. An operator needs to manually execute operations. It is as if we had taken a CPU and pared it down to two of its most basic components: a register file and an arithmetic logic unit (ALU). The Register File The Curta's register file consists of three digital registers, each of which contains a decimal integer (i.e., each digit is from 0 to 9, rather than 0 to 1 like on a binary computer): * sr, the setting register, is located on the side of the device. The value in sr can be set manually by the operator using a set of knobs on the side of the device. The machine will never write to it, only read from it. It has 11 digits. * rr, the results register, is located at the top of the device along the black part of the dial. It is readable and writable by the machine, but not directly modifiable by the operator. It has 15 digits. * cr, the counting register, is located next to rr along the silver part of the dial. Like rr, it is only machine-modifiable. It has 8 digits. [sr] sr, set to 1997. [cr_rr] rr is the black dial; cr is the silver one. There are also two settings on the device that aren't really registers, but, since they are changed as part of operation, they are a lot like the control registers of a modern computer. The carriage (there isn't an abbreviation for this one, so I'll call it ca) is the upper knurled ring on the machine. It can be set to a value from 0 to 7^2. To set it, the operator lifts the ring up (against spring tension), twists it, and lets it spring back into the detent for the chosen value. This is a one-hand motion. There is a small triangle in the middle of the top of the device that points at which of the digits in cr will get incremented. [ca] ca raised and in motion. Finally, rl, the reversing lever, is a small switch near the back of the device that can be in the up or down position. This is like a flag register: up is cleared, down is set. [rl] rl in the up position. The Instruction Set We have all this memory, but the meat of a machine is what it can do. I will provide an instruction set for the Curta to aid in giving rigorous descriptions of operations you can perform with it. The core operation of the Curta is "add-with-shift-and-increment". This is a mouthful. At the very top of the machine is the handle, which is analogous to a clock signal pin. Every clockwise turn of this handle executes one of these operations. Internally, this is implemented using a variation on the Leibniz gear, a common feature of mechanical calculators. [pturn] The handle in "addition" mode. This operation is not that complicated, it just does a lot of stuff. It takes the value of sr, left-shifts it (in decimal) by the value in ca, and adds it to rr. Also, it increments CR by 1 shifted by ca. In other words: rr += sr << ca cr += 1 << ca Text Recall that this is a decimal machine, so << is the same as multiplication by a power of 10, not a power of 2. Addition can overflow, and it wraps around as expected: adding one to 999_999_999_999_999_999 already in rr will fill it with zeroes. Pulling the handle up reveals a red ring, indicating the machine is in subtraction mode. This flips the signs of both the rr and cr modifications: rr -= sr << ca cr -= 1 << ca Text [mturn] The handle in "subtraction" mode. The Curta cannot handle negative numbers, so it will instead display the ten's complement^3 of a negative result. For example, subtracting 1 from 0 will produce all-nines. You can detect when underflow or overflow occurs when the resulting value is unexpectedly larger or smaller than the prior value in rr, respectively. (This trick is necessary on architectures that lack a carry flags register, like RISC-V.) Setting rl will reverse the sign of the operation done on cr during a turn of the handle. In addition mode, it will cause cr to be subtracted from, while in subtraction mode, it will cause it to be added to. Some complex algorithms make use of this. Finally, the clearing lever can be used to clear (to zero) sr or rr, independently. It is a small ring-shaped lever that, while the carriage is raised, can be wiped past digits to clear them. Registers cannot be partially cleared. [pturn] The clearing lever. Notation Let's give names to all the instructions the operator needs to follow, so we can write some assembly: * mr, or Machine Ready!, means to clear/zero every register. All Curta instructions use the term "Machine Ready" to indicate the beginning of a calculation session. * pturn is the core addition operation, a "plus turn". * mturn is its subtraction twin, a "minus turn". * set requests the operator set one of rl or sm. * clr is the opposite of set. * zero request a clear of one of rr or cr using the clearing lever. * add , requests manual addition of an immediate to sr or ca. This is limited by what mental math we can ask of the operator. * copy , sr requests a copy of the value in rr or cr to sr. * wrnp , indicates we need to write down a value in any register to a handy notepad (hence write notepad), marked with . * rdnp , asks the operator to read a value recorded with wrnp. * if ,