LMC Machine Level Programming


Machine level processing of instructions involves a systematic and almost mechanical repetition of actions.


Sample Program Execution

Initial Values and Program Dump

Step-By-Step Execution

  1. first instruction cycle
    1. the "little man" reads the Counter (00), goes over to Mailbox number 00, and reads the contents of that Mailbox (500)
    2. the "little man" pushes the increment button on the Counter (so it now reads 01)
    3. comparing the first digit (5) of the number he just read from the Mailbox, the little man discovers that he is to "Input", so he goes over to the Input basket, reads whatever number was left there by someone outside the computer room, and then goes to the Calculator and enters that number
  2. second instruction cycle
    1. the "little man" reads the Counter (01), goes over to Mailbox number 01, and reads the contents of that Mailbox (211)
    2. the "little man" pushes the increment button on the Counter (so it now reads 02)
    3. comparing the first digit (2) of the number he just read from the Mailbox, the little man discovers that he is to "Store", so he reads the value currently on the Calculator and then goes over to Mailbox 11 (as indicated by the last 2 digits of the instruction he just read) and replaces the number in Mailbox number 11 with whatever value he just read from the Calculator
  3. third instruction cycle
    1. the "little man" reads the Counter (02), goes over to Mailbox number 00, and reads the contents of that Mailbox (500)
    2. the "little man" pushes the increment button on the Counter (so it now reads 03)
    3. comparing the first digit (5) of the number he just read from the Mailbox, the little man discovers that he is to "Input", so he goes over to the Input basket, reads whatever number was left there by someone outside the computer room, and then goes to the Calculator and enters that number
  4. ......and so on


Encoding a Simple, Linear Program


LMC Flow Control Instructions

Flow control instructions enable machine level execution to perform the standard selection and repetition sequences.


Symbolic Labels

In the same way that mnemonic codes make it easier to write (and think about) instructions, "symbolic labels" make it easier to work with mailbox addresses. Instead of coding numeric addresses, it is usually easier to use label "names"; of course, for this to work, any label "name" which is used in an instruction must be defined as being the identifier ("symbolic label") for a mailbox containing either an instruction or a data value.


IF...THEN...ELSE...ENDIF (Selection)


WHILE...DO...ENDWHILE (Repetition)


Sample Program


Subroutine and Function Calls

All practical computer processors provide some method, at a low level, for using subroutines and functions. Unfortunately, the Little Man Computer is not a "practical computer processor". We could, however, imagine a more advanced model of the LMC, the "son of LMC" which would provide such support.

There are many different ways this support could be implemented even within a computer architecture as limited as the LMC. Note that a significant problem in implementing additional instructions to "call" and "return" from a subroutine involves the fact that almost all the numeric instruction code values have been used up. It is possible however to "squeeze" this subroutine handling ability in. The method described below is by no means "standard" but it will provide the necessary capabilities.

We will assume that our modified "son of LMC" has an additional instruction "CALL xy" with a numeric form "0xy". This CALL instruction will behave very much like the JMP instruction except that before changing the counter, it will save the contents of the counter plus "900" (that is a JMP to the counter contents address) in the mailbox immediately before "xy" (that is in the mailbox with address xy-1). Notice that at the time this "save" is done, the counter will already have been incremented and will contain the address of the instruction serially after the CALL.

Subroutines will always be coded to allow for a mailbox location immediately before the first instruction of the subroutine. When a subroutine has finished processing requirements, it will JMP to that "prefix" mailbox which will, in turn, contain the JMP instruction (provided by the CALL) back to the instruction immediately following the CALL.

Example:
MODULEBOXLABELOPCODE&OPERANDMACHINE
 Main
 program
00    IN 500
01    CALL RTN 006
02    IN 500
03    CALL RTN 006
04    HLT 700
 Sub-
  rou-
   tine
05    DAT 000
06RTN:   STO X 210
07    ADD X 310
08    OUT 600
09    JMP RTN-1905
10X:   DAT 000 000
 
Label Table
LABELMAILBOX
RTN:06
X:10

The CALL RTN (006) in mailbox 01 will change the contents of mailbox 05 (05 being the mailbox immediately before 06) to contain "902" (a JMP to the mailbox after this CALL), and then it will change the contents of the counter to contain "06". The next instruction to be performed will therefore be the one in mailbox 06.

When execution reached the JMP RTN-1 (905) in mailbox 09, execution of this instruction will cause the counter to change to 09. Therefore the next instruction to be performed will be the "902" placed in mailbox 05 by the earlier CALL RTN. The next instruction after this will then be the one in mailbox 02, the second IN.

After the second IN, a second CALL RTN is performed, but this time the contents of mailbox 05 are changed to "904", a JMP to the instruction following this second CALL RTN.