Getting started with the Little Man Computer (post #02)

Continuing our series on The Little Man Computer.  This time - adding.

<-- previous post.  

The little man has an instruction for adding - simply "ADD". An instruction for adding might look like "ADD X", which means "add the value at the address labelled 'X' onto the accumulator." 

Example program number four.

Add three numbers together.

...adds three numbers together

lda x
add y
add z
out
.
.. the values of x, y, z
.. are coded here
x dat 5
y dat 10

z dat 100

When the LMC compiles this program it translates the first four commands into the codes, 599 (lda x), 198 (add y), 197 (add z) and 902 (output) as shown in the image below.

The LMC memory showing how the first four commands have been translated into codes.
The values of 'x', 'y' and 'z' are placed at locations 99, 98 and 97 (the little man decides that this is the best place to put them).  In the image below you can see the values 5, 10 and 100 in these locations.
The little man puts the values of 5, 10 and 100 into address 99, 98 and 97 and labels them x, y and z.


What is happening?
First the little man reads code 00: 599 and translates this as "Load the value in address at 99 into the accumulator.  This is our value 5.
Now that we have a value in the accumulator, we can add the other two values on, so...
The code 01: 198 means 'Add the value at address 98 onto the accumulator.'  This means that 10 is added to 5 and stored in the accumulator.  The accumulator should now contain the value 15.  The next code, 02:197 means, 'Add the value at address 97 onto the accumulator', so the value 100 is added to the current value in the accumulator - 15, giving an answer of 115.  Opening the output report should prove this:


The output instruction puts the current value of the accumulator into a file for easy reading.


Mnemonics used in this post:


add  -  adds a value at an address onto the accumulator.  For example 'add 99' adds the value at the address '99' onto the accumulator.  CAREFUL!  This is NOT the same as adding the value 99 to the accumulator.


lda  -  loads a value at an address into the accumulator.  For example 'lda 99' puts the value at address '99' into the accumulator (replacing any other value currently there.

out  -  writes the current value in the accumulator into the output file. 

dat  -  puts a value into the computer memory.  for example, 'one dat 001' puts the value '1' into an address and labels it 'one'.

#littleManComputer