Skip to content

Instantly share code, notes, and snippets.

@castleberrysam
Last active December 17, 2016 04:29
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 castleberrysam/0f93ff5450011cfceebab4ab0c960133 to your computer and use it in GitHub Desktop.
Save castleberrysam/0f93ff5450011cfceebab4ab0c960133 to your computer and use it in GitHub Desktop.
Instruction Set:
PSH C push a constant onto the stack
POP N remove N values from the top of the stack
POP pop value N, POP N
STO N pop value, store into Nth slot down in the stack
STO pop value N, STO N
RCL N push value in Nth slot down in the stack
RCL pop value N, RCL N
AND C logical AND C with the top value
AND logical AND the top two values
NOT complement the top value
JMP A execute at address A
JMP execute at address in the top two values
EXE A push address of next instruction, JMP A
EXE push address of next instruction, JMP
CND skip following instruction if top value is greater than zero
ADD C add C to top value
ADD add top two values
SUB C subtract C from top value
SUB subtract top two values
ADR push address of next instruction
Following is an example program for the ADDN function, which adds N numbers together and returns their sum.
PSH #x01
PSH #x02
PSH #x03 ; arguments to ADDN, (1 2 3)
PSH #x03 ; number of args, 3
PSH #x10
PSH #x00 ; push address of ADDN, call it
EXE
JMP #x0e ; infinite loop, end of program
RCL #x02 ; start of ADDN, recall # of args
ADD #x04
STO #x03 ; add 4 and restore to make arg index
PSH #x00 ; addition accumulator
RCL #x03 ; start of loop, recall arg index (used to be # of args)
RCL #x00 ; duplicate it
SUB #x04
CND
JMP #x2e ; goto END if arg index <= 4
RCL
ADD
RCL #x03 ; get num at arg index, add to accumulator (prev 2 lines), recall arg index again
SUB #x01
STO #x04 ; decrement and restore
JMP #x18 ; goto start of loop
RCL #x01 ; END of loop, recall accumulator
STO #x05 ; store underneath return address
POP #x02 ; get rid of remaining locals, jump to return address
JMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment