Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Simple Functions with the 5-Instruction Set Paper Computer
- The 5-Instruction Set Computer is basically a Turing-complete
- programming langauge that has only 5 instructions:
- +--+-------+
- |1.| isz |
- +--+-------+
- |2.| jmp |
- +--+-------+
- |3.| inc |
- +--+-------+
- |4.| dec |
- +--+-------+
- |5.| stp |
- +--+-------+
- With just these three instructions, it is possible to emulate any algorithm this universe has to offer. Its up to you to be creative and have an unlimited
- paper tape supply!
- Before we go into what these instructions mean, we need to know what a PROGRAM COUNTER is.
- Simply put, the PROGRAM COUNTER is the line of the program you are on. Because (most) programs start lists at 0, the program counter starts out at 0.
- 0 some_instruction <- If you are HERE, the program counter is 0
- 1 some_instruction <- If you are HERE, the program counter is 1
- 2 some_instruction <- If you are HERE, the program counter is 2
- 3 some_instruction <- If you are HERE, the program counter is 3
- ...
- You execute your program by increasing your program counter by +1 every instruction unless otherwise specified (You execute line by line and follow
- each instruction as it presented).
- Now we can see what these instructions do!
- These instructions are extremely simple. Here they are broken down:
- 1. ISZ
- (IS Zero) ISZ [R]
- This function checks to see if the specified [R]egister is Zero.
- If it is: Add +2 to the program counter (Skip 2 lines)
- If it isn't: Add +1 to the program counter (Skip 1 line)
- 2. JMP
- (JuMP) JMP [L]
- This function jumps the program to the specified [L]ine.
- 3. INC
- (INCrement) INC [R]
- This function increments (adds +1) to the specified [R]egister
- 4. DEC
- (DECrement) DEC [R]
- This function decrements (subtracts -1) from the specified [R]egister
- 5. STP
- (SToP) STP
- This function terminates the program.
- Now that we know what the instructions do, let's make a paper computer!
- If you have a whiteboard or chalkboard, this will be much easier.
- Draw something that looks like this:
- +---------------+ +---------------+
- | 0| | |A| |
- +---------------+ +---------------+
- | 1| | |B| |
- +---------------+---+---------------+
- | 2| | |C| |
- +---------------+---+---------------+
- | 3| | |D| |
- +---------------+---+---------------+
- | 4| | |E| |
- +---------------+---+---------------+
- | 5| | |F| |
- +---------------+---+---------------+
- | 6| | |G| |
- +---------------+---+---------------+
- | 7| | |H| |
- +---------------+---+---------------+
- | 8| | |I| |
- +---------------+---+---------------+
- | 9| | |J| |
- +---------------+---+---------------+
- |10| | |K| |
- +---------------+---+---------------+
- The LEFT column is your PROGRAM. These are the spaces that you will write and execute your program.
- The RIGHT colum are your REGISTERS. These are the blocks of data that you will manipulate with your program.
- By default, they are blank (zero). You can use tally marks, coins, match sticks, anything, to represent the number.
- Say I make an adding program, and I want to add Register A to Register B and have the result in Register C.
- I will first write the program, and then put in a number in Register A (say, 5) and a number in Register C (say, 2).
- After I run the program, Register C will have my answer!
- Ex:
- +---------------+ +---------------+ +---------------+ +---------------+
- | 0| isz A | |A| 5 | | 0| isz A | |A| 0 |
- +---------------+ +---------------+ +---------------+ +---------------+
- | 1| jmp 3 | |B| 5 | | 1| jmp 3 | |B| 0 |
- +---------------+---+---------------+ +---------------+ +---------------+
- | 2| jmp 6 | |C| | | 2| jmp 6 | |C| 10 |
- +---------------+---+---------------+ +---------------+ +---------------+
- | 3| dec A | |D| | | 3| dec A | |D| |
- +---------------+---+---------------+ +---------------+ +---------------+
- | 4| inc C | |E| | | 4| inc C | |E| |
- +---------------+---+---------------+ +---------------+ +---------------+
- | 5| jmp 0 | |F| | | 5| jmp 0 | |F| |
- +---------------+---+---------------+ +---------------+ +---------------+
- | 6| isz B | |G| | --> | 6| isz B | |G| |
- +---------------+---+---------------+ +---------------+ +---------------+
- | 7| jmp 9 | |H| | | 7| jmp 9 | |H| |
- +---------------+---+---------------+ +---------------+ +---------------+
- | 8| stp | |I| | | 8| stp | |I| |
- +---------------+---+---------------+ +---------------+ +---------------+
- | 9| dec B | |J| | | 9| dec B | |J| |
- +---------------+---+---------------+ +---------------+ +---------------+
- |10| inc C | |K| | |10| inc C | |K| |
- +---------------+---+---------------+ +---------------+ +---------------+
- |11| jmp 6 | |L| | |11| jmp 6 | |L| |
- +---------------+---+---------------+ +---------------+ +---------------+
- Here's some sample programs to help get you started.
- Samples:
- +------------------------+
- |Hello World (1/0 Toggle)|
- +------------------------+
- Infinitely toggles Register A from 0 to 1 and back again.
- 0 inc A
- 1 dec A
- 2 jmp 0
- +----------+
- |Simple Add|
- +----------+
- Adds Register B to Register A.
- Solution is in Register A
- 0 isz B
- 1 jmp 3
- 2 stp
- 3 dec B
- 4 inc A
- 5 jmp 0
- +---------------+
- |Simple Subtract|
- +---------------+
- Subtracts Register B from Register A. Stops if either Register A
- or Register B reach 0.
- 0 isz A
- 1 jmp 3
- 2 stp
- 3 isz B
- 4 jmp 6
- 5 stp
- 6 dec B
- 7 dec A
- 8 jmp 0
- +-----------------+
- |Boolean Functions|
- +-----------------+
- Register A: Input A
- Register B: Input B
- Register X: Output
- You can change the register names as you need
- AND OR XOR NAND NOR XNOR NOT
- 0 isz A isz A isz A 0 isz A isz A isz A isz A 0
- 1 jmp 3 jmp 5 jmp 7 1 jmp 4 stp jmp 5 stp 1
- 2 stp isz B isz B 2 inc X isz B isz B inc X 2
- 3 isz B jmp 5 jmp 5 3 stp stp stp stp 3
- 4 jmp 6 stp stp 4 isz B inc X jmp 8 4
- 5 stp inc X inc X 5 stp stp isz B 5
- 6 inc X stp stp 6 jmp 2 jmp 8 6
- 7 stp isz B 7 stp 7
- 8 stp 8 inc X 8
- 9 jmp 5 9 stp 9
- IMPLY NIMPLY
- 0 isz A isz A
- 1 jmp 4 jmp 3
- 2 inc X stp
- 3 stp isz B
- 4 isz B stp
- 5 jmp 2 inc X
- 6 stp stp
- In order to chain these gates, be sure to change the internal STP's to JMP to the
- last line of that function to allow the program to continue.
- +---------------+
- |Signed Subtract|
- +---------------+
- Subtracts Register B from Register A. Signed. Negative flag is in X.
- May be able to optimize this.
- 0 isz A
- 1 jmp 5
- 2 isz B
- 3 jmp 11
- 4 stp
- 5 isz B
- 6 jmp 8
- 7 stp
- 8 dec B
- 9 dec A
- 10 jmp 0
- 11 inc X
- 12 dec B
- 13 inc A
- 14 isz B
- 15 jmp 12
- 16 stp
- +-----------------+
- |Unsigned Multiply|
- +-----------------+
- Multiplies Register A by Register B. Solution is in Register C.
- Unsigned (wiill ignore negative flag).
- One-way multipication: Two-way multipication:
- 0 isz B 0 isz B
- 1 jmp 3 1 jmp 3
- 2 stp 2 stp
- 3 dec B 3 dec B
- 4 isz A 4 isz A
- 5 jmp 7 5 jmp 7
- 6 jmp 11 6 jmp 11
- 7 dec A 7 dec A
- 8 inc C 8 inc C
- 9 inc D 9 inc D
- 10 jmp 4 10 jmp 4
- 11 isz B 11 isz B
- 12 jmp 14 12 jmp 14
- 13 stp 13 stp
- 14 isz D 14 isz D
- 15 jmp 17 15 jmp 17
- 16 jmp 0 16 jmp 0
- 17 dec D 17 dec B
- 18 inc A 18 inc C
- 19 jmp 14 19 dec D
- 20 inc A
- 21 jmp 14
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement