This article is a description of Charles Babbage’s first computer program, which he sketched out almost 200 years ago, in 1837. The Analytical Engine (AE), the computer for which the program was intended, did not actually exist; sadly, it was to remain unfinished. Only some portions of Babbage’s calculating machine were built during the lifetime of the English mathematician and inventor. Had it been completed, it would have been the world’s first computer.1,3 Of course, many algorithms had already been described before Babbage—for computing the greatest common divisor (GCD), for example—but Babbage’s code is the first attempt to specify how to mechanize complex algorithms with a computer. This was the heyday of the first industrial revolution, the age of steam engines and mechanization. Electricity, light bulbs, and telephones were still decades away, but the computer was taking shape on Babbage’s drawing board.
Key Insights
The Analytical Engine (AE) would have been the first computer, had it been finished.
The AE had a processor (“mill”) and a separate memory (“store”).
The AE could compute the basic arithmetic operations and was programmed using strings of punched cards.
Charles Babbage wrote the first computer program in 1837.
Babbage (1791-1871) developed detailed blueprints for the AE and sketched 26 programming examples between 1836 and 1841. The Science Museum in London has digitized the Babbage Archives so that today we can inspect existing diagrams of the Analytical Engine (with first drafts from 1835 on) and the 26 programming examples from the comfort of a home computer. In a 2021 paper titled “The Computer Programs of Charles Babbage,” I discuss the architecture of the AE and review some of its programs.5
The design of the AE consisted of a processor for the four arithmetic operations, called the “mill,” and a separate memory for decimal integers, called the “store” (Babbage once considered building the AE to be able to store hundreds of variables with 40 digits).2 This separation of processor and memory is typical of today’s computers. However, the program was not stored in memory, but encoded on punched cards that were read one at a time. One stream of cards was used by the processor, while a separate stream was used for the memory (for the addresses of the arguments). In this article, I sometimes refer to the “mill” as the processor and the “store” as the memory of the machine. When Babbage talks about the contents of memory cells, he calls them “variables.” The address of a variable is its subindex. For example, the memory cell with address 3 would be . The AE performed all calculations using fixed-point arithmetic. The number of digits to the right of the decimal point could be chosen by the programmer.
For example, when the operation punched card was read to calculate an addition, the mill would go into the “addition” state, while the variable cards would instruct the store to retrieve the contents of the addresses of the two needed arguments and send them to the mill. Since the processor waits for its arguments and the store waits for the result, this automatically synchronizes the operation and the variable cards.
Reading a variable was a destructive operation; it reset the variable to zero. However, it was possible to store the complement of this variable in another memory address while sending it to the mill. Re-reading this complement and transferring it back to the original memory address (complementing again) restored this address to its original contents.
The First Code Table
The program we are discussing was written by Babbage in 1837. The title of the sketch is “Notations and Calculations” and the first line reads “No. 1. 4 August 1837.” This was the first of the series of programs that Babbage decided to carefully sketch out, and we even have the date of the program. The Babbage Archive lists this program as “BAB L1”. There is a small program for computing a simple formula in the archive, but it is undated and unnumbered (BAB L26). The Babbage Archive dates this code fragment to August 1837, without further information. There is also a sketch of how to assign coefficients of a linear equation to memory addresses, but no code. The date of this fragment is given as 1836.
The program in BAB L1 deals with the solution of a system of two linear equations in two variables. It is easy to find a closed formula for the result. Babbage considered the two linear equations
We have six parameters, the six coefficients , in the two equations. The solution for is
Given , the solution for is
In the first expression, we assume that the denominator is non-zero, while in the second we assume that is non-zero, so that the solutions exist. Babbage did not check these two conditions in his program.
First, Babbage assigns the six coefficients , to the six variables to in the memory of the AE. He then computes successively the intermediate results , and finally the quotient for finding . The complete computation for requires four multiplications, two subtractions, and a final division. That is a grand total of five “big” and two “small” operations (as Babbage called them).
Babbage drew up two complete tables for the calculation. Table 1 shows the code and the order of the seven operations required. The second column shows the operation and the third column is a comment for each calculation. The program ends when the value of is found.
Mill | Store | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Numbers of | Nature of | in variables | ||||||||||
Operations | operations | variables in store | ||||||||||
1 | 0 | 0 | ||||||||||
2 | 0 | |||||||||||
3 | 0 | 0 | ||||||||||
4 | 0 | |||||||||||
5 | 0 | 0 | ||||||||||
6 | 0 | 0 | ||||||||||
7 | 0 | 0 |
The six memory addresses contain, at the beginning, the coefficients of the six terms . In the first multiplication, the processor uses the variables and for computing . The table shows that after the first operation, both variables are reduced to zero and the result is stored in . The last column is a comment about the operation which has been performed, that is, .
The second multiplication computes and stores it in . Symbolically, the computation is . The quote means that the original content of has been overwritten once. However, there is a problem.
Variable was read destructively for the multiplication in the first line. The arithmetic operations need two arguments, in this case for the multiplication. Babbage designed the AE so that an argument could be reused repeatedly. Since the first two computed terms are and , the first argument can be kept in the processor. After the multiplication with , we only need to load argument to the processor. This way of reusing an argument in the processor is not described in BAB L1, but it is something that Babbage exploited in other programs. In BAB L1, Babbage explicitly mentions that is reused for a multiplication table with .
The two columns of comments, displayed side by side, tell the whole story for this computation (Table 2). In a sense, this is the program that Babbage has in mind, but what the punched cards contain are the specific operations and the addresses needed. As can be seen from the code, Babbage reuses memory addresses, and each time a memory address is overwritten he adds a quote to the variable’s name. Variables 1 and 3 are reused (overwritten) twice in the program, so that their names become and .
Computation | Code | |
---|---|---|
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 |
In other programs written after this first one, Babbage simplified. Later, he did not always keep track of variable reuse (with the quotes), since it does not affect the computation. Also, he did not always add symbolic comments to the tables, writing only the necessary arithmetic operation and the addresses used.
The Second Code Table
Babbage wrote the second part of the computation in the same document (L26 in the Babbage archive). Having found with the first seven lines of the program, we can now compute using the value of . The computation for is the same as before, but is then computed as , since the first linear equation is . The program is shown below.
Mill | Store | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Numbers of | Nature of | in variables | ||||||||||||
Operations | operations | variables in store | ||||||||||||
1 | 0 | 0 | ||||||||||||
2 | 0 | |||||||||||||
3 | 0 | 0 | ||||||||||||
4 | 0 | |||||||||||||
5 | 0 | 0 | ||||||||||||
6 | 0 | 0 | ||||||||||||
7 | 0 | 0 | ||||||||||||
8 | 0 | |||||||||||||
9 | 0 | |||||||||||||
10 | 0 | |||||||||||||
11 | 0 | 0 | ||||||||||||
12 | 0 | 0 | ||||||||||||
13 | 0 | 0 |
There is something new in the rows 1, 2, and 3. Now, Babbage has made explicit that the coefficients need to be refreshed, storing their complements in auxiliary variables. The complement of is stored in , the complement of in , and the complement of in . We need , , and for the computation of .
What Babbage intended to do with the AE was to store the value of a variable, that was still needed, in another auxiliary variable when it was sent to the processor. Since the AE used gears (think of a clock face with the digits 0 to 9), storing a number was performed by turning the gear counterclockwise, for example, and retrieving the contents was accomplished by turning it in the opposite direction until the variable was reduced to zero. For example, suppose we stored the number ‘3’ by turning a gear counterclockwise three positions (out of 10 possible positions, one for each decimal digit). When the number is read, the gear turns back three positions, clockwise. Starting from zero, a receiving auxiliary gear will be rotated clockwise to position 7 (the decimal complement of 3). Therefore, the auxiliary variable will not store the original decimal number, but its complement, for each digit of the number. If we had the number 345 in , and its complement was saved temporarily in , we would have 765 in the variable . Reading back from to , we would complement the number again, digit by digit, and would be restored to the original 345.
In the program, the stored complements are transferred back (complementing again) to the variables , , and in the auxiliary steps 8, 9, and 10. The value of is computed in steps 11, 12, 13, and the program finally stops. There is a mistake in the table in line 7: Babbage writes “=x” in the column for variable 4, which is correct, but also in the column for variable 7, which is incorrect.
In other programs, written after this one, Babbage overlapped the storing of the complement of a variable, with its subsequent reading and complementing in a single program step. That is, the store would send a number to the mill, store it temporarily as a complement, and when the result of the computation was returned by the processor, the stored variable could be refreshed. It is not quite clear whether the refresh happened while the processor was busy or after it had delivered its result to memory. The notation used by Babbage to indicate that a variable containing the value , for example, kept its value, was , indicating that the variable was reduced to zero and later restored, without necessarily indicating the auxiliary address used.
Symbolically, the complete program written by Babbage would read as Table 4 shows. In steps 8, 9, and 10, there is no operation in the processor and only the memory is active, transferring numbers to recover the parameters from their complements.
Computation | Code | |
---|---|---|
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
10 | ||
11 | ||
12 | ||
13 |
Conclusion
Solving systems of linear equations is very useful in many areas of mathematics and engineering. It is natural that Babbage decided to use this as a kind of benchmark problem for the AE. It is known that Chinese mathematicians could solve linear systems of up to three variables and equations more than 2,000 years ago.
In his code sketches, Babbage did not write high-level code and then compile the program. The annotations in his program are more like comments and the actual code would be the strings of punched cards for the processor and the memory. In later programs, Babbage did not include a symbolic comment about the computations being performed. Babbage wrote his programs by listing the required operations and the required arguments. Both things immediately translate to the necessary punched cards. In modern parlance, Babbage wrote his programs in “assembler.” Also, since the operation cards are separate from the variable cards, they can synchronize in diverse ways, as explained in my paper.5 These complications are not present in the program discussed in this article. The AE was never finished, so the code shown here could never be run. It could only be tested following its execution on paper. The design of the AE was very ambitious, including also the possibility of programming loops. Babbage changed the design several times, and this was one of the problems that made it very difficult to finish the machine.3
It is important to point out, although it is obvious, that the first sketch ever written of a computer program is not one of those published in Menabrea.4 That publication appeared six years after Babbage had already sketched his program “number one” for solving simultaneous linear equations and other 25 coding examples.
Join the Discussion (0)
Become a Member or Sign In to Post a Comment