Sunday, October 23, 2011

Learning Assembly Language

Underlying Pedagogical Philosophy.
Recently in the New York Times, there was an article [nytimes.com] talking about the Waldorf School system which discourages the use of technology when teaching. I am sympathetic to this point of view, and believe (as a mathematician) that most subjects should be taught without technology.

What about computer science? Yes, to a degree, I believe so. What about learning assembly language...?

CARDIAC

The CARDboard Illustrative Aid to Computation (CARDIAC for short) was initially created at Bell labs to help understand programming in assembly language. It was also done "by hand": you are the computer!

It is done in base 10, and the computer has 100 memory cells. Each cell can hold signed numbers from ±0 to ±999.

You can download pdfs to print out and start computing here [kylem.net].

Each 3-digit number represents some instruction. It should be read as OAA where O is the digit referring to the instruction to carry out, and AA refers to the address of the memory cell involved.

Just some quick terminology, an "Accumulator" is a special memory cell (a CPU register, actually) where intermediate results of a calculation are held.

To keep track of which cell we are computing, one uses a "Program Counter". Originally it was a butterfly pin in the Bell Labs CARDIAC kit, but one could use anything.

Now, let us run quickly through the opcodes:

0 INP Input take a number from the input card and put it in a specified memory cell.
1 CLA Clear and add clear the accumulator and add the contents of a memory cell to the accumulator.
2 ADD Add add the contents of a memory cell to the accumulator.
3 TAC Test accumulator contents performs a sign test on the contents of the accumulator.
4 SFT Shift shifts the accumulator x places left, then y places right, where x is the upper address digit and y is the lower.
5 OUT Output take a number from the specified memory cell and write it on the output card.
6 STO Store copy the contents of the accumulator into a specified memory cell.
7 SUB Subtract subtract the contents of a specified memory cell from the accumulator.
8 JMP Jump jump to a specified memory cell. The current cell number is written in cell 99. This allows for one level of subroutines.
9xx HRS Halt and reset halt the machine and move the program counter to xx.

Note that cell 0 was "read only memory" (I guess one could write in pen to indicate that it won't ever change) and was 001.

Basic Algorithm

The basic algorithm to using the CARDIAC system is as follows:

  1. Begin with a clean output card, set the accumulator to 0.
  2. Set the program counter to memory cell 0.
  3. Read the opcode and the address from the cell.
  4. Look up the opcode, perform the operation using the address given.
  5. If the opcode is jump, set the current address to the address given; otherwise increment the program counter by 1 and go back to step 3.

Some Exercises

1. Consider the numbers 1, 1+2, 1+2+3, etc. These are triangular numbers. Write a program that prints out the first 100 triangular numbers.
2. How can we implement a loop in CARDIAC?
3. Implement Euclid's algorithm in CARDIAC.
4. Implement the Sieve of Eratosthenes in CARDIAC.

Final Remarks

This gives us some idea of how assembly language works, but it is also limited. Perhaps next time (if there is a next time!) we will look into the Littleman computer architecture.

No comments: