Research and Advances
Computer History

The First Computer Program

Mathematician and inventor Charles Babbage's code sketches were the first attempt to specify how to mechanize complex algorithms with a computer.

Posted
Credit: Andrij Borys Associates, Shutterstock Charles Babbage, gears, sketch of the Analytical Engine, and the date 1837, illustration

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 v3. 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

a x + b y + c = 0 a x + b y + c = 0 .

We have six parameters, the six coefficients a,b,c,a,b,c, in the two equations. The solution for x is

x = b c b c b a b a .

Given x, the solution for y is

y = ( a x c ) / b

In the first expression, we assume that the denominator baba is non-zero, while in the second we assume that b is non-zero, so that the solutions exist. Babbage did not check these two conditions in his program.

First, Babbage assigns the six coefficients a,b,c,a,b,c, to the six variables v1 to v6 in the memory of the AE. He then computes successively the intermediate results ba,bc,ba,bc,bcbc,baba, and finally the quotient for finding x. The complete computation for x 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 x is found.

Table 1. Computation of x.
MillStore 
Numbers ofNature ofin variables + a x + b y + c   + a x + b y + c    
Operationsoperationsvariables in store v 1 v 2 v 3   v 4 v 5 v 6   v 7  
1 × b a 0    0   b a v 7 = v 5 · v 1
2 × b c b c  0       v 1 = v 5 · v 3
3 × b a  0 b a  0     v 3 = v 2 · v 4
4 × b c   b c     0   v 2 = v 2 · v 6
5 b c b c 00   b c b c      v 4 = v 2 v 1
6 b a b a b a b a  0     0 v 1 = v 7 v 3
7* ÷ b c b c b a b a 0  = x  0     x = v 3 = v 4 v 1

The six memory addresses v1,,v6 contain, at the beginning, the coefficients of the six terms ax,by,c,ax,by,c. In the first multiplication, the processor uses the variables v1 and v5 for computing ba. The table shows that after the first operation, both variables are reduced to zero and the result ba is stored in v7. The last column is a comment about the operation which has been performed, that is, v7=v5·v1.

The second multiplication computes bc and stores it in v1. Symbolically, the computation is v1=v5·v3. The quote means that the original content of v1 has been overwritten once. However, there is a problem.

Variable v5 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 ba and bc, the first argument b can be kept in the processor. After the multiplication with a, we only need to load argument c 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 b is reused for a multiplication table with b.

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 v1 and v3.

Table 2. The seven arithmetic operations and their description, line by line.
 ComputationCode
1 b a v 7 = v 5 · v 1
2 b c v 1 = v 5 · v 3
3 b a v 3 = v 2 · v 4
4 b c v 2 = v 2 · v 6
5 b c b c v 4 = v 2 v 1
6 b a b a v 1 = v 7 v 3
7 b c b c b a b a v 3 = v 4 v 1

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 x with the first seven lines of the program, we can now compute y using the value of x. The computation for x is the same as before, but y is then computed as y=(cax)/b, since the first linear equation is ax+by+c=0. The program is shown below.

Table 3. Computation of x and y.
MillStore 
Numbers ofNature ofin variables + a x + b y + c   + a x + b y + c      
Operationsoperationsvariables in store v 1 v 2 v 3   v 4 v 5 v 6   v 7 v 8 v 9  
1 × b a 0    0   b a C a   v 7 = v 5 · v 1
2 × b c b c  0   C c       v 1 = v 5 · v 3
3 × b a  0 b a  0      C b v 3 = v 2 · v 4
4 × b c   b c     0     v 2 = v 2 · v 6
5 b c b c 00     b c b c      v 6 = v 2 v 1
6 b a b a   b a b a 0     0   v 2 = v 7 v 3
7* ÷ b c b c b a b a  0   = x  0  = x    x = v 4 = v 6 v 2
8   a         0  v 1 = v 1 = a
9    b         0 v 2 = v 2 = b
10     c   0      v 3 = v 3 = c
11 × a x 0   0 a x       v 5 = v 1 · v 4
12 c a x c a x  0  0      v 1 = v 3 v 5
13* ÷ c a x b 00    = y       y = v 5 = v 1 v 2

There is something new in the rows 1, 2, and 3. Now, Babbage has made explicit that the coefficients a,b,c need to be refreshed, storing their complements Ca,Cb,Cc in auxiliary variables. The complement of a is stored in v8, the complement of c in v5, and the complement of b in v9. We need a, b, and c for the computation of y.

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 v5, and its complement was saved temporarily in v8, we would have 765 in the variable v8. Reading back from v8 to v5, we would complement the number again, digit by digit, and v5 would be restored to the original 345.

In the program, the stored complements are transferred back (complementing again) to the variables v1, v2, and v3 in the auxiliary steps 8, 9, and 10. The value of y 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 a, for example, kept its value, was 0/a, 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 a,b,c from their complements.

Table 4. Final set of arithmetic operations for computing x and y and their description, line by line.
 ComputationCode
1 b a v 7 = v 5 · v 1
2 b c v 1 = v 5 · v 3
3 b a v 3 = v 2 · v 4
4 b c v 2 = v 2 · v 6
5 b c b c v 6 = v 2 v 1
6 b a b a v 2 = v 7 v 3
7* b c b c b a b a v 4 = v 6 v 2
8  v 1 = v 1 = a
9  v 2 = v 2 = b
10  v 3 = v 3 = c
11 a x v 5 = v 1 · v 4
12 c a x v 1 = v 3 v 5
13* c a x b v 5 = v 1 v 2

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.

    • 1. Babbage, C. Passages from the Life of a Philosopher. Longman, London, (1864).
    • 2. Babbage, C. On the mathematical powers of the calculating engine, unpublished manuscript, 1837. The Origins of Digital Computers. B. Randell (eds.), Springer-Verlag, Berlin, (1982).
    • 3. Bromley, A.  Charles Babbage's Analytical Engine, 1838. Annals of the History of Computing 4, 3 (July 1982).
    • 4. Menabrea, L.F. Sketch of the Analytical Engine Invented by Charles Babbage, translation with notes from Ada Augusta Lovelace, (1843).
    • 5. Rojas, R. The computer programs of Charles Babbage. IEEE Annals of the History of Computing 43, 1 (2021), 618.

Join the Discussion (0)

Become a Member or Sign In to Post a Comment

The Latest from CACM

Shape the Future of Computing

ACM encourages its members to take a direct hand in shaping the future of the association. There are more ways than ever to get involved.

Get Involved

Communications of the ACM (CACM) is now a fully Open Access publication.

By opening CACM to the world, we hope to increase engagement among the broader computer science community and encourage non-members to discover the rich resources ACM has to offer.

Learn More