Programming

Up a level : The ELIAC I
Previous page : The User Interface
Next page : ELIAC_I

Programs

A program is a set of instructions that (hopefully) do what you want the computer to do.  When the program is run, the instructions are executed one by one unless a jump instruction is encountered.

language: This is writing the program using the kind of code mentioned above, i.e. ADD a,b, and so on.  The code ADD is called a mnemonic (memory help) for the add instructions.

Machine code: The mnemonics can be translated to numbers that are the actual instructions read by the CPU. A program that turns mnemonic codes into machine code is called an assembler. This program contains an assembler. The instructions are coded as follows on this toy CPU. The values to the left will give us the first digit, and the top row value gives the second digit.

0 1 2 3 4 5 6 7 8 9
0 NOP LD A,B LD A,C LD A,D LD A,E ADD A,A ADD A,B ADD A,C ADD A,D ADD A,E
1 LD B,A LD B,C LD C,D LD E,E ADD B,A ADD B,B ADD B,C ADD C,D ADD E,E
2 LD C,A LD C,B LD C,D LD C,E ADD C,A ADD C,B ADD C,C ADD C,D ADD C,E
3 LD D,A LD D,B LD D,C SPC LD D,E ADD D,A ADD D,B ADD D,C ADD D,D ADD D,E
4 LD E,A LD E,B LD E,C LD E,D HLT ADD E,A ADD E,B ADD E,C ADD E,D ADD E,E
5 JP SUB A,B SUB A,C SUB A,D SUB A,E LDI A LDI B LDI C LDI D LDI E
6 SUB B,A JPZ SUB B,C SUB C,D SUB E,E LD A LD B LD C LD D LD E
7 SUB C,A SUB C,B JPNZ SUB C,D SUB C,E ST A ST B ST C ST D ST E
8 SUB D,A SUB D,B SUB D,C JPC SUB D,E IN A IN B IN C IN D IN E
9 SUB E,A SUB E,B SUB E,C SUB E,D JPNC OUT A OUT B OUT C OUT D OUT E

We have, for example, that the instruction LDI B, 17, i.e. load (immediately) the register B with the value 17 is thus coded as 56, 17 in consecutive cells.

The part of an instruction that actually says what should be done is called an opcode or op-code for operation code. In the LDI B, 17, the number 56 is the op-code. The number 17 is called an operand.

As you can see, two machine codes do not mean anything, 11 and 22, so there is space for an upgrade (^^,).

An example

Say, for example, you want to add the numbers in cell 0 and in cell 1 and put the result in cell 2. A program that does that could look something like this:

Let’s go through the different elements of this.

Case sensitivity: As you can see, the letters in the above are all in small letters.  This particular assembly language is not case sensitive, so “add a,b”, “ADD a,b” and “aDd A,b” would all mean the same, even though the later looks very ugly.

One could refer to addresses by numbers, but it is often more convenient to use a so-called label.

Labels: Names given to positions in a program or cells of a memory. Here we write labels as the label name followed by a colon, in cell 00 we have, for example, “n1:”. That means that we may refer to cell 00 by using the label n1. When we use a label, we don’t put a colon after the name.

A label may be followed by nothing, a number, a label reference or an instruction.

In this case, we have “n1: 17”. That means that the cell 00 can be referred to as the cell n1, and that it contains the value 17.

In JS, the labels correspond to variable names. In this case, we can see n1 as the name of a variable that has the value 17.

Start: This is a particular label that tells the assembler what to put in the PC initially, i.e. what the starting point of the program will be. This is often referred to as the origin in various assembly languages, and so the label is then instead “org”.

Spaces and commas: The program does not care about spaces and commas. We may include them freely. So “addab” means the same as “add a,b”, but the latter is much easier to read. I suggest you put a couple of spaces before a label reference for readability.

This program will start at cell 04.

At that point, we have the text “ld a,”, and the next line has a label reference “n1”.  In most assembly languages, this would be written in one line, but here I have kept it so that one line of code will correspond to one memory cell. The assembler has turned the mnemonics “ld a,” to the number 65, which is the machine code for “ld a,” for this particular machine.

The next cell, 05, contains the number 00, since n1 refers to the cell 00.

When the machine runs the instruction in cells 04 and 05 it will load register A with the contents of cell 00, i.e. the number 17.

Next, the same is repeated, but now for the cell n1.


Exercise 1: Why does the cell 07 contain the number 01? (Key at the end of the page)


The next instruction, “add a,b” instructs the CPU to add the contents of register B to register A.

Then we have “st a,” and “sum”. These two lines instruct the CPU to store the value of register A in the cell labelled sum.

Finally, we have the instruction “hlt” that instructs the CPU to halt the program execution.


Exercise 2: Enter the above program into The ELIAC I. Correct mistakes that the program warns you about, if any. Then step through the program using the STEP key, or by pressing Ctrl-S. Check what result you get in the cell labelled sum.

Exercise 3: Check what happens to the PC (program counter) as you run the program.

Exercise 4: Change line 06 to “ld b, n2”, and press Enter. What happens and why?  Change it back to “ld b,”. What happens and why? You may have to press Enter a couple of times.

Exercise 5: Change the values of the cells n1 and n2 to other values. Do you see any problem? How about if you enter 50 and 50 and run the program? What is the result?


You can load the program we have looked at by pressing Ctrl-1.

Copy and Paste

The copy button will save the current program to the clipboard. You can then paste it into Word, the notebook, or any other text editor.

You may then select the program in the editor, copy it, and then use the paste key to write it to ELIAC’s memory.

The Carry flag

As you hopefully have found, the result of the last addition is not 100 as expected. This is due to the simple fact that a cell can only contain values between 00 and 99. The result of 50+50 is 100, but the CPU will only keep the last two digits. This is where the carry flag will help us. The carry flag will be set if the result exceeds 100. What we can do is to check if the carry flag is above 100 and then add 1 to say register C.

To do this, we need to set ta register to 1, so that we can add one to C, and we need to check the result of the addition, “add a,b”.

We start by inserting a couple of new lines at address 08. To do this, we select line eight and press Ctrl-I a couple of times. We may now write “ldi e,”, “   1” on line eight and nine.

This will result in register E getting the value 1 as the program runs the lines of code.

Next, we need to check the result of the addition “add a,b”. To do this, we insert three empty lines below the add instruction. (If you need to remove something, you may press Ctrl-D.) On the lines just below the add instruction, we insert “jpnc” and “  noc”. We will now get an error because the label “noc” is not defined. We will fix that in a moment.

The “jpnc” instruction makes the program jump to the address specified after the instruction if the carry flag is set, else it will continue with the next instruction. In this case, we want the next instruction to add one to C. We will thus add the instruction “add c,e” at the next position. This will store any possible carry in the register c.  Finally, we add the label “noc” (no carry) in front of the “st a,” instruction. Observe that the label should be on the same line as the instruction.

(To get rid of the error, we need to go to the red line and press Enter.)

Finally, we need to store the carry in some cell. To do this, we may change the label sum to sum 1, add the label sum 2 to the next line, change st a, sum to st a, sum2 and add a couple of lines in the end to store c in the cell named sum 1. Finally, add the hlt instruction at the end of the program.

(You may also load the whole program by pressing Ctrl-2.)

Change 17 to 50 and 12 to 50 in the first two cells, and then run the program.  Try with some other values.


Key:

  1. Because it contains the address to the cell with the label n2.
  2. You get that register A and cell 02 will have the value 17+12=29.
  3. It steps up from 4 to 11 (the address of the cell with the hlt instruction).
  4. You will get an error message:After changing it back, the error message disappears.
  5. What happens if the sum exceeds 99? 50+50=100. In the 50+50 case the register A=00 and the CY flag is set.
Up a level : The ELIAC I
Previous page : The User Interface
Next page : ELIAC_ILast modified: Jul 7, 2025 @ 20:38