📜 ⬆️ ⬇️

Computer little man

We all know the Turing machine and the Post machine. These are abstract computing machines invented by mathematicians for the theory of algorithms. The computer of the little man ( Little man computer ) is a computer model designed to learn how a computer works and works. This model was proposed by Professor Stewart Madnik in 1965 and has been successfully used to teach students of elementary courses in both programming and computer design.


Let's look at the computer as a small post office.

There are two windows in the compartment for communicating with the outside world: INP comes in and out of the OUT window there are leaves with numbers. The numbers can be from 0 to 999. There are 100 mailboxes in the compartment, numbered from 0 to 99, of which the little man take leaves with three-digit numbers, and can also replace these leaves with new ones. He also has a simple calculator (battery) that allows you to add and subtract three-digit numbers. When processing numbers on a calculator, flags for numbers equal to 0 and greater than 0 are automatically raised.

The computer operator writes the program on the sheets and puts them in the mailboxes, then sets the command counter (PC) to 0 and presses the "Start job" button. Inside the post office, the little man is processing the numbers of the letters in the mailboxes. We describe the processing cycle in steps:
')
  1. Select a number from the command counter. This number is the mailbox number.
  2. Remember the number from the mailbox, the number of which was obtained in step 1.
  3. Increase the command counter by 1.
  4. Parse the command (the number received in step 2).
  5. Fetch data from the mailbox, if required.
  6. Run the command itself.
  7. Save data to your inbox if required.
  8. Return to step 1 or stop if the command is 0.

Commands - three-digit decimal numbers, the number of hundreds determines the type of command, and the numbers of tens and ones - the number of the mailbox.
Command codeCommand typeActions
1xxADDAdd a number from the box xx to the battery. Raise the flag ZERO or POSITIVE NUMBER. If the result is greater than 999, then finish the job with an error.
2xxSUBSubtract the number from the box xx from the battery. Raise the flag ZERO or POSITIVE NUMBER. If the result is less than 0, the battery does not change and the flags go down.
3xxStaSave the contents of the battery in the box xx.
5xxLdaLoad the number from box xx into the battery. Raise the flag ZERO or POSITIVE NUMBER.
6xxBraSet the command counter to xx.
7xxBRZIf the flag is ZERO, then set the command counter to xx.
8xxBRPIf the flag is ZERO or POSITIVE NUMBER, then set the command counter to xx.
901INPSelect the number from the INP window and write to the battery. Raise the flag ZERO or POSITIVE NUMBER. If the number does not fall in the range from 0 to 999, then finish the work with an error.
902OutWrite the number of the battery on a piece of paper and put it in the OUT window.
000HltFinish the job.

So, we described a computer with von Neumann architecture with a small set of commands. The model does not use binary coding of commands, and this simplifies the writing of programs for this computer — the computer device can be drawn on the board, and the programs executed on a piece of paper. I think that in the 60s of the twentieth century, many students performed the programs in this way, but now we can write an emulator of such a computer, which I did at AWK.

Emulator source code and sample programs: GitHub .

The emulator is started with the command:
  awk -f lmc.awk 

Emulator Commands:
 LOAD ### [### ...] - load the program in codes
 DUMP - show memory contents 
 RUN - run the program
 ASM <file name> - compile the program in assembler and download

Some short programs:
 LOAD 901 902 000 - copy the number from INP to OUT, stop
 LOAD 901 104 902 000 1 - add 1 to the number of INP, write to OUT, stop
 LOAD 901 902 704 600 000 - we copy numbers from INP to OUT, and stop working after printing 0

An example of running the program:
 LOAD 901 902 0
 DUMP
 00: 901 902 0 0 0 0 0 0 0 0
 10: 0 0 0 0 0 0 0 0 0 0
 20: 0 0 0 0 0 0 0 0 0 0
 30: 0 0 0 0 0 0 0 0 0 0
 40: 0 0 0 0 0 0 0 0 0 0
 50: 0 0 0 0 0 0 0 0 0 0
 60: 0 0 0 0 0 0 0 0 0 0
 70: 0 0 0 0 0 0 0 0 0 0
 80: 0 0 0 0 0 0 0 0 0 0
 90: 0 0 0 0 0 0 0 0 0 0
 RUN
 INP: 100
 OUT: 100


The same length and simple format of commands allows you to quickly write an assembler for this computer. After that, you can write long programs without manually calculating addresses.
Fibonacci numbers:
awk -f lmc.awk asm fib.lma 00: #Print fibonacci numbers 00: LDA ONE #Load init values 01: STA FIB1 #First number 02: STA FIB2 #Second number 03: OUT #Print first 04: OUT #Print second 05: LOOP LDA MAX #ACC=MAX 06: SUB FIB1 #ACC=ACC-FIB1 07: SUB FIB2 #ACC=ACC-FIB2 08: BRP CONT #ACC positive ? Continue 09: BRA END #Negative : goto end of program 10: CONT LDA FIB1 #ACC=FIB1 11: ADD FIB2 #ACC=ACC+FIB2 12: STA FIBN #Store FIBN - next number 13: OUT #Print it 14: LDA FIB2 #FIB1=FIB2 15: STA FIB1 16: LDA FIBN #FIB2=FIBN 17: STA FIB2 18: BRA LOOP #Next LOOP 19: END HLT 20: ONE DAT 1 #Init value 21: FIB1 DAT #First fib number 22: FIB2 DAT #Second fib number 23: FIBN DAT #Next fib number 24: MAX DAT 999 #Max computer number Labels: LOOP 05 MAX 24 FIB1 21 FIB2 22 FIBN 23 ONE 20 END 19 CONT 10 Xrefs: LOOP 18 MAX 5 FIB1 1 6 10 15 FIB2 2 7 11 14 17 FIBN 12 16 ONE 0 END 9 CONT 8 LOAD 520 321 322 902 902 524 221 222 810 619 521 122 323 902 522 321 523 322 605 0 1 0 0 0 999 

Source: https://habr.com/ru/post/257331/


All Articles