<-- 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
.. 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 little man puts the values of 5, 10 and 100 into address 99, 98 and 97 and labels them x, y and z.|
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'.