Skip to content

Instantly share code, notes, and snippets.

@SomeoneWeird
Created April 5, 2014 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SomeoneWeird/9991311 to your computer and use it in GitHub Desktop.
Save SomeoneWeird/9991311 to your computer and use it in GitHub Desktop.

crappy cpu

This is a stupidly simple implementation of a "CPU" for my own benefit.

Registers

26 storage registers available, A through Z.

Other registers:

Register Name Description
EP Execution Pointer Stores currently executing memory address

OPCodes

Code Hex Value Example Description / Example
NOP 0x00 NOP No operation - does nothing.
SET 0x01 SET A, 20 Sets register A to 20 (dec)
ADD 0x02 ADD A, 30 Adds 30 to register A
SUB 0x03 SUB A, 10 Subtracts 10 from register A
MUL 0x04 MUL A, 5 Multiplies register A by 5
DV1 0x05 DV1 A, 5 Divides A by 5 and only stores the full number
DV2 0x06 DV2 A, 5 Divides A by 5 and only stores the remainder
EQL 0x07 EQL A, 10 Only executes next operation if A is 10
NQL 0x08 NQL A, 10 Only executes next operation if A is not 10
LBL 0x09 LBL string Allows you to set a label for code execution
JMP 0x10 JMP A Sets EP to A and continues execution
EXT 0xFF EXT Stops execution.

Example

SET A, 30    # SET A to 30
SET B, A     # SET B to A
ADD B, 30    # ADD 30 to B, B is now 60
MUL B, 2     # MULTIPLY B by 2, B is now 120

EQL B, 100   # Only run next statement if B is 100
  NOP        # This will never be run (i hope...)
NQL B, 100   # You can kind of use this as an if()else()
  DV1 B, 2   # Divides B by 2, back to 60
  
LBL exit  # JMP here to exit the program
  EXT   
  
LBL loop        # Set a label for our loop
  EQL B, 100    # If B is 100
    JMP exit    # Exit the program
  ADD B, 10     # Else, add 10 more
  JMP loop      # and restart the loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment