This is the original source code NASA used for the 1969 Apollo 11 Moon landing
The United States became the first country to successfully land humans on the Moon on July 20, 1969. Although Russia and China have landed spacecraft on the Moon, to this day, the United States remains the only nation in the world to have successfully conducted crewed missions to the Moon.
On 16 July 1969, three NASA astronauts Commander Neil Armstrong and Lunar module pilot Buzz Aldrin, and Command module pilot Michael Collins were strapped into their Apollo spacecraft on top of the vast Saturn V rocket. They were propelled into orbit in just over 11 minutes. Four days later, Armstrong and Aldrin became the first humans to set foot on the lunar surface. Collins flew the Command Module Columbia alone in lunar orbit while they were on the Moon’s surface.
During the nine Apollo missions between 1969 and 1972, 24 astronauts went to the Moon, and 12 of them had the opportunity to walk on it. Even with new space innovations and historic milestones from companies like SpaceX, the last time a human visited the moon was in December 1972, during NASA’s Apollo 17 mission.
Over five decades later, the Apollo Moon mission remains one of NASA’s greatest achievements, if not humankind’s single greatest technological achievement. What makes it even so amazing is that NASA did not have access to the bleeding edge technology we have today. Given the technology of the time, one of the questions that get asked often is: How did NASA astronauts get to the Moon and back?
According to NASA archives, the technologies used for Apollo were developed by Project Gemini. You can read about the amazing handmade technology that powered Apollo 11’s Moon Voyage here. However, the focus of this piece is about the Apollo 11 onboard and guidance and the onboard computers that made it possible to fly the command module to the moon and back to earth and the source code behind them.
NASA also revealed that the “Apollo 11’s original source code was developed by computer programmers at the Massachusetts Institute of Technology in the ’60s. The code was the backbone of the Apollo Guidance Computer (AGC), which astronauts used to guide, navigate and control what became the first spacecraft to ever land on the moon.”
In 2016, NASA released the source code for the Apollo 11 Moon landing at github. According to NASA, Apollo computers evaluated the transcendental functions like sine, arctangent, and log. The code is part of the source code for Luminary 1A build 099. It is part of the source code for the Command Module (CM) and here for the Lunar Lander (LM) used for Apollo Guidance Computer (AGC), for Apollo 11.
The AGC is a digital computer produced for the Apollo program that was installed onboard each Apollo command module (CM) and Apollo Lunar Module (LM). The AGC provided computation and electronic interfaces for guidance, navigation, and control of the spacecraft. The AGC software was written in AGC assembly language and stored on rope memory.
The AGC was designed at the MIT Instrumentation Laboratory under Charles Stark Draper, with hardware design led by Eldon C. Hall. The flight hardware was fabricated by Raytheon, whose Herb Thaler was also on the architectural team.
The codes were used to implement sine and cosine functions: see here for the command module and here for the lunar lander (it looks like it is the same code).
# Page 1102
# SINGLE PRECISION SINE AND COSINE
SPCOS AD HALF # ARGUMENTS SCALED AT PI
SPSIN TS TEMK
ARG90 INDEX A
TC Q # RESULT SCALED AT 1.
Below is an explanation of the code. Hats off to Nathan Tuggy at stackexchange
# SINGLE PRECISION SINE AND COSINE
indicates, that the following is indeed an implementation of the sine and cosine functions.
Information about the type of assembler used, can be found on Wikipedia.
Partial explanation of the code:
SPSIN actually calculates , and
SPCOS calculates .
SPCOS first adds one half to the input, and then proceeds to calculate the sine (this is valid because of ). The argument is doubled at the beginning of the
SPT subroutine. That is why we now have to calculate for .
POLLEY calculates an almost Taylor polynomial approximation of . First, we store in the register SQ (where denotes the input). This is used to calculate the polynomial
The values for the constants can be found in the same GitHub repository and are
which look like the first Taylor coefficients for the function .
These values are not exact! So this is a polynomial approximation, which is very close to the Taylor approximation, but even better (see below, also thanks to @uhoh and @zch).
Finally, the result is doubled with the
DDOUBL command, and the subroutine
POLLEY returns an approximation to .
Note that for this polynomial approximation you only need four multiplications and two additions (
AD in the code). For the Apollo Guidance Computer, memory and CPU cycles were only available in small numbers.
There are some ways to increase accuracy and input range, which would have been available for them, but it would result in more code and more computation time. For example, exploiting symmetry and periodicity of sine and cosine, using the Taylor expansion for cosine, or simply adding more terms of the Taylor expansion would have improved the accuracy and would also have allowed for arbitrarily large input values.
Below is an old NASA video that shows how the DSKY worked, starting at about 6 minutes mark.
Below is an illustration of the Saturn V Rocket.