iBet uBet web content aggregator. Adding the entire web to your favor.
iBet uBet web content aggregator. Adding the entire web to your favor.



Link to original content: http://wikipedia.org/wiki/Little_man_computer
Little man computer - Wikipedia Jump to content

Little man computer

From Wikipedia, the free encyclopedia

This image is a screen capture of a Little Man Computer program. The background colour is a vibrant purple, in the top left the program title reads “Little Man Computer Simulation” in white text. Below it sits a table of data, with columns with names such as “line”, “label”, “operator” and “operand”. In it lie various computer commands, such as “start” “loop” “finish” “number” “zero”. The centre of the image is occupied by a representation of the different parts a computer, connected by blue lines on a pink background. The different parts of the computer have different shapes and names, such as the orange box labelled “PC” or the green “V” shape labelled “ALU”. To the right of that is a black and grey table titled “MEMORY”, filled with various digits, presumably collected from the operation of the machine. Finally, below this lies a panel where the user can enter “inputs” to receive an “output” - this is next to another box titled “console” which records the operation of the machine, e.g. “ASSEMBLY: SYMBOL TABLE BUILT”.
Little Man Computer simulator

The Little Man Computer (LMC) is an instructional model of a computer, created by Dr. Stuart Madnick in 1965.[1] The LMC is generally used to teach students, because it models a simple von Neumann architecture computer—which has all of the basic features of a modern computer. It can be programmed in machine code (albeit in decimal rather than binary) or assembly code.[2][3][4]

The LMC model is based on the concept of a little man shut in a closed mail room (analogous to a computer in this scenario). At one end of the room, there are 100 mailboxes (memory), numbered 0 to 99, that can each contain a 3 digit instruction or data (ranging from 000 to 999). Furthermore, there are two mailboxes at the other end labeled INBOX and OUTBOX which are used for receiving and outputting data. In the center of the room, there is a work area containing a simple two function (addition and subtraction) calculator known as the Accumulator and a resettable counter known as the Program Counter. The Program Counter holds the address of the next instruction the Little Man will carry out. This Program Counter is normally incremented by 1 after each instruction is executed, allowing the Little Man to work through a program sequentially. Branch instructions allow iteration (loops) and conditional programming structures to be incorporated into a program. The latter is achieved by setting the Program Counter to a non-sequential memory address if a particular condition is met (typically the value stored in the accumulator being zero or positive).

As specified by the von Neumann architecture, any mailbox (signifying a unique memory location) can contain either an instruction or data. Care therefore needs to be taken to stop the Program Counter from reaching a memory address containing data - or the Little Man will attempt to treat it as an instruction. One can take advantage of this by writing instructions into mailboxes that are meant to be interpreted as code, to create self-modifying code. To use the LMC, the user loads data into the mailboxes and then signals the Little Man to begin execution, starting with the instruction stored at memory address zero. Resetting the Program Counter to zero effectively restarts the program, albeit in a potentially different state.

Execution cycle

[edit]

To execute a program, the little man performs these steps:

  1. Check the Program Counter for the mailbox number that contains a program instruction (i.e. zero at the start of the program)
  2. Fetch the instruction from the mailbox with that number. Each instruction contains two fields: An opcode (indicating the operation to perform) and the address field (indicating where to find the data to perform the operation on).
  3. Increment the Program Counter (so that it contains the mailbox number of the next instruction)
  4. Decode the instruction. If the instruction uses data stored in another mailbox then use the address field to find the mailbox number for the data it will work on, e.g. "get data from mailbox 42")
  5. Fetch the data (from the input, accumulator, or mailbox with the address determined in step 4)
  6. Execute the instruction based on the opcode given
  7. Branch or store the result (in the output, accumulator, or mailbox with the address determined in step 4)
  8. Return to the Program Counter to repeat the cycle or halt

Commands

[edit]

While the LMC does reflect the actual workings of binary processors, the simplicity of decimal numbers was chosen to minimize the complexity for students who may not be comfortable working in binary/hexadecimal.

Instructions

[edit]

Some LMC simulators are programmed directly using 3-digit numeric instructions and some use 3-letter mnemonic codes and labels. In either case, the instruction set is deliberately very limited (typically about ten instructions) to simplify understanding. If the LMC uses mnemonic codes and labels then these are converted into 3-digit numeric instructions when the program is assembled.

The table below shows a typical numeric instruction set and the equivalent mnemonic codes.

Instructions
Numeric code Mnemonic code Instruction Description
1xx ADD ADD Add the value stored in mailbox xx to whatever value is currently on the accumulator (calculator).
Note: the contents of the mailbox are not changed, and the actions of the accumulator (calculator) are not defined for add instructions that cause sums larger than 3 digits. Similarly to SUBTRACT, one could set the negative flag on overflow.
2xx SUB SUBTRACT Subtract the value stored in mailbox xx from whatever value is currently on the accumulator (calculator).
Note: the contents of the mailbox are not changed, and the actions of the accumulator are not defined for subtract instructions that cause negative results - however, a negative flag will be set so that 7xx (BRZ) and 8xx (BRP) can be used properly.
3xx STA STORE Store the contents of the accumulator in mailbox xx (destructive).
Note: the contents of the accumulator (calculator) are not changed (non-destructive), but contents of mailbox are replaced regardless of what was in there (destructive)
5xx LDA LOAD Load the value from mailbox xx (non-destructive) and enter it in the accumulator (destructive).
6xx BRA BRANCH ALWAYS(unconditional) Set the program counter to the given address (value xx). That is, the value in mailbox xx will be the next instruction executed.
7xx BRZ BRANCH IF ZERO (conditional) If the accumulator (calculator) contains the value 000, set the program counter to the value xx. Otherwise, do nothing. Whether the negative flag is taken into account is undefined. When a SUBTRACT underflows the accumulator, this flag is set, after which the accumulator is undefined, potentially zero, causing behavior of BRZ to be undefined on underflow. Suggested behavior would be to branch if accumulator is zero and negative flag is not set.
Note: since the program is stored in memory, data and program instructions all have the same address/location format.
8xx BRP BRANCH IF POSITIVE (conditional) If the accumulator (calculator) is 0 or positive, set the program counter to the value xx. Otherwise, do nothing. As LMC memory cells can only hold values between 0 and 999, this instruction depends solely on the negative flag set by an underflow on SUBTRACT and potentially on an overflow on ADD (undefined).
Note: since the program is stored in memory, data and program instructions all have the same address/location format.
901 INP INPUT Go to the INBOX, fetch the value from the user, and put it in the accumulator (calculator)
Note: this will overwrite whatever value was in the accumulator (destructive)
902 OUT OUTPUT Copy the value from the accumulator (calculator) to the OUTBOX.
Note: the contents of the accumulator are not changed (non-destructive).
000 HLT/COB HALT/COFFEE BREAK Stop working/end the program.
DAT DATA This is an assembler instruction which simply loads the value into the next available mailbox. DAT can also be used in conjunction with labels to declare variables. For example, DAT 984 will store the value 984 into a mailbox at the address of the DAT instruction.

Examples

[edit]

Using numeric instruction codes

[edit]

This program (instruction 901 to instruction 000) is written just using numeric codes. The program takes two numbers as input and outputs the difference. Notice that execution starts at Mailbox 00 and finishes at Mailbox 07. The disadvantages of programming the LMC using numeric instruction codes are discussed below.

Mailbox Numeric code Operation Comments
00 901 Accumulator = Inbox INPUT the first number, enter into calculator (erasing whatever was there)
01 308 Mailbox 08 = Accumulator STORE the calculator's current value (to prepare for the next step…)
02 901 Accumulator = Inbox INPUT the second number, enter into calculator (erasing whatever was there)
03 309 Mailbox 09 = Accumulator STORE the calculator's current value (again, to prepare for the next step…)
04 508 Accumulator = Mailbox 08 (Now that both INPUT values are STORED in Mailboxes 08 and 09…)

LOAD the first value back into the calculator (erasing whatever was there)

05 209 Accumulator = Accumulator - Mailbox 09 SUBTRACT the second number from the calculator's current value (which was just set to the first number)
06 902 Outbox = Accumulator OUTPUT the calculator's result to the OUTBOX
07 000 Stop HALT the LMC

Using mnemonics and labels

[edit]

Assembly language is a low-level programming language that uses mnemonics and labels instead of numeric instruction codes. Although the LMC only uses a limited set of mnemonics, the convenience of using a mnemonic for each instruction is made apparent from the assembly language of the same program shown below - the programmer is no longer required to memorize a set of anonymous numeric codes and can now program with a set of more memorable mnemonic codes. If the mnemonic is an instruction that involves a memory address (either a branch instruction or loading/saving data) then a label is used to name the memory address.

INP
STA FIRST
INP
STA SECOND
LDA FIRST
SUB SECOND
OUT
HLT
FIRST DAT
SECOND DAT

Labels

[edit]

Without labels the programmer is required to manually calculate mailbox (memory) addresses. In the numeric code example, if a new instruction was to be inserted before the final HLT instruction then that HLT instruction would move from address 07 to address 08 (address labelling starts at address location 00). Suppose the user entered 600 as the first input. The instruction 308 would mean that this value would be stored at address location 08 and overwrite the 000 (HLT) instruction. Since 600 means "branch to mailbox address 00" the program, instead of halting, would get stuck in an endless loop.

To work around this difficulty, most assembly languages (including the LMC) combine the mnemonics with labels. A label is simply a word that is used to either name a memory address where an instruction or data is stored, or to refer to that address in an instruction.

When a program is assembled:

  • A label to the left of an instruction mnemonic is converted to the memory address the instruction or data is stored at. i.e. loopstart INP
  • A label to the right of an instruction mnemonic takes on the value of the memory address referred to above. i.e. BRA loopstart
  • A label combined with a DAT statement works as a variable, it labels the memory address that the data is stored at. i.e. one DAT 1 or number1 DAT

In the assembly language example which uses mnemonics and labels, if a new instruction was inserted before the final HLT instruction then the address location labelled FIRST would now be at memory location 09 rather than 08 and the STA FIRST instruction would be converted to 309 (STA 09) rather than 308 (STA 08) when the program was assembled.

Labels are therefore used to:

  • identify a particular instruction as a target for a BRANCH instruction.
  • identify a memory location as a named variable (using DAT) and optionally load data into the program at assembly time for use by the program (this use is not obvious until one considers that there is no way of adding 1 to a counter. One could ask the user to input 1 at the beginning, but it would be better to have this loaded at the time of assembly using one DAT 1)

Example

[edit]

The program below will take a user input, and count down to zero.

     INP
     OUT      // Initialize output 
LOOP BRZ QUIT // Label this memory address as LOOP. If the accumulator value is 0, jump to the memory address labeled
              // QUIT
     SUB ONE  // Subtract the value stored at address ONE from the accumulator
     OUT
     BRA LOOP // Jump (unconditionally) to the memory address labeled LOOP
QUIT HLT      // Label this memory address as QUIT
ONE  DAT 1    // Store the value 1 in this memory address, and label it ONE (variable declaration)

The program below will take a user input, square it, output the answer and then repeat. Entering a zero will end the program.
(Note: an input that results in a value greater than 999 will have undefined behaviour due to the 3 digit number limit of the LMC).

START   LDA ZERO     // Initialize for multiple program run
        STA RESULT
        STA COUNT
        INP          // User provided input
        BRZ END      // Branch to program END if input = 0
        STA VALUE    // Store input as VALUE
LOOP    LDA RESULT   // Load the RESULT
        ADD VALUE    // Add VALUE, the user provided input, to RESULT
        STA RESULT   // Store the new RESULT
        LDA COUNT    // Load the COUNT
        ADD ONE      // Add ONE to the COUNT
        STA COUNT    // Store the new COUNT
        SUB VALUE    // Subtract the user provided input VALUE from COUNT
        BRZ ENDLOOP  // If zero (VALUE has been added to RESULT by VALUE times), branch to ENDLOOP
        BRA LOOP     // Branch to LOOP to continue adding VALUE to RESULT
ENDLOOP LDA RESULT   // Load RESULT
        OUT          // Output RESULT
        BRA START    // Branch to the START to initialize and get another input VALUE
END     HLT          // HALT - a zero was entered so done!
RESULT  DAT          // Computed result (defaults to 0)
COUNT   DAT          // Counter (defaults to 0)
ONE     DAT 1        // Constant, value of 1
VALUE   DAT          // User provided input, the value to be squared (defaults to 0)
ZERO    DAT          // Constant, value of 0 (defaults to 0)

Note: If there is no data after a DAT statement then the default value 0 is stored in the memory address.

In the example above, [BRZ ENDLOOP] depends on undefined behaviour, as COUNT-VALUE can be negative, after which the ACCUMULATOR value is undefined, resulting in BRZ either branching or not (ACCUMULATOR may be zero, or wrapped around). To make the code compatible with the specification, replace:

        ...
        LDA COUNT    // Load the COUNT
        ADD ONE      // Add ONE to the COUNT
        STA COUNT    // Store the new COUNT
        SUB VALUE    // Subtract the user provided input VALUE from COUNT
        BRZ ENDLOOP  // If zero (VALUE has been added to RESULT by VALUE times), branch to ENDLOOP
        ...

with the following version, which evaluates VALUE-COUNT instead of COUNT-VALUE, making sure the accumulator never underflows:

        ...
        LDA COUNT    // Load the COUNT
        ADD ONE      // Add ONE to the COUNT
        STA COUNT    // Store the new COUNT
        LDA VALUE    // Load the VALUE
        SUB COUNT    // Subtract COUNT from the user provided input VALUE
        BRZ ENDLOOP  // If zero (VALUE has been added to RESULT by VALUE times), branch to ENDLOOP
        ...

Another example is a quine, printing its own machine code (printing source is impossible because letters cannot be output):

LOAD LDA 0     // Load position 0 into the accumulator. This line will be modified on each loop to load the next lines into the accumulator
     OUT       // Output the accumulator's value. The accumulator's value will be the line that was just loaded
     SUB ONE   // Subtract 1 from the value in the accumulator. This is so we can do the BRZ in the next step to see if we are on the last line in the program
     BRZ ONE   // If the previous subtraction has made the accumulator 0 (which means we had the value 001 in the accumulator), then branch to position ONE
     LDA LOAD  // Load the LOAD position into the accumulator, this is in preparation to increment the address digits for this position
     ADD ONE   // Increment the position digits for the LOAD line. The value currently in the accumulator would, if read as an instruction, load the next line into the accumulator, compared to the last line loaded
     STA LOAD  // Store the newly incremented LOAD line back in the LOAD position
     BRA LOAD  // Return to the beginning of the loop
ONE  DAT 1     // The variable ONE. If read as an instruction, this will be interpreted as HLT/COB and will end the program

This quine works using self-modifying code. Position 0 is incremented by one in each iteration, outputting that line's code, until the code it is outputting is 1, at which point it branches to the ONE position. The value at the ONE position has 0 as opcode, so it is interpreted as a HALT/COB instruction.

See also

[edit]

References

[edit]
  1. ^ "Little Man Computer". Illinois State University. May 1, 2000. Archived from the original on February 27, 2009. Retrieved March 8, 2009.
  2. ^ Yurcik, W.; Osborne, H. (2001). "A crowd of Little Man Computers: Visual computer simulator teaching tools". Proceedings of the 2001 Winter Simulation Conference (Cat. No.01CH37304). Vol. 2. p. 1632. doi:10.1109/WSC.2001.977496. ISBN 0-7803-7307-3. S2CID 18907923.
  3. ^ Yurcik, W.; Brumbaugh, L. (2001). "A web-based little man computer simulator". Proceedings of the thirty-second SIGCSE technical symposium on Computer Science Education - SIGCSE '01. p. 204. doi:10.1145/364447.364585. ISBN 1581133294. S2CID 14794750.
  4. ^ Osborne, H.; Yurcik, W. (2002). "The educational range of visual simulations of the Little Man Computer architecture paradigm". 32nd Annual Frontiers in Education. pp. S4G–S19. doi:10.1109/FIE.2002.1158742. ISBN 0-7803-7444-4. S2CID 10324295.
[edit]

Simulators

[edit]

Online

[edit]