Advertisement
NovaYoshi

dumb stack machine

Nov 20th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. 16-bit words
  2. 8 registers of any size
  3. two stacks, one is active at a time
  4.  
  5. 0000 - nop - do nothing
  6. 0001 - add - pop two numbers, push their sum
  7. 0010 - sub - pop two numbers, push their difference
  8. 0011 - pop - pop a number, and do nothing
  9. 0100 - dup - pop a number, push it twice
  10. 0101 - swap - pop two numbers, push them in the opposite order
  11. 0110 - ld - pop an address and a value, then store the value to that address
  12. 0111 - st - pop an address, and then push the contents of that address
  13. 1000 nnnn - ld #num - push 4-bit sign extended integer
  14. 1001 0nnn - ld reg - push contents of register n
  15. 1001 1nnn - st reg - pop, store to register n
  16. 1010 0nnn - add reg - pop, add register n's contents, push new value
  17. 1010 1nnn - sub reg - pop, subtract register n's contents, push new value
  18. 1011 0000 - mul - pop two numbers, push their product
  19. 1011 0001 - div - pop two numbers, push their quotient
  20. 1011 0010 - mod - pop two numbers, push their remainder
  21. 1011 0011 - and - pop two numbers, push their bitwise AND
  22. 1011 0100 - or - pop two numbers, push their bitwise OR
  23. 1011 0101 - xor - pop two numbers, push their bitwise XOR
  24. 1011 0110 - lsr - pop a number, push number>>1
  25. 1011 0111 - asr - pop a number, push number>>1 with top bit the same
  26. 1011 1000 - call - pop a number, jump to that location, push the old program counter to other stack
  27. 1011 1001 - return - pop a number from the other stack, and jump to it
  28. 1011 1010 - neg - pop a number, push -number
  29. 1011 1011 - cpl - pop a number, push ~number
  30. 1011 1100 - inc - pop a number, push number+1
  31. 1011 1101 - dec - pop a number, push number-1
  32. 1011 1110 - if - pop a number, pop an address. call address if number is nonzero
  33. 1011 1111 - ifelse - pop a number, pop two addresses, pop a number. call address if number is nonzero
  34. 1100 0000 - loop - pop two addresses. call the first address, if it produces a nonzero number call the second one, repeat
  35. 1100 0001 - eq? - pop two numbers, push -1 if ==, 0 if not
  36. 1100 0010 - ne? - pop two numbers, push -1 if !=, 0 if not
  37. 1100 0011 - gt? - pop two numbers, push -1 if >, 0 if not
  38. 1100 0100 - lt? - pop two numbers, push -1 if <, 0 if not
  39. 1100 0101 - le? - pop two numbers, push -1 if >=, 0 if not
  40. 1100 0110 - ge? - pop two numbers, push -1 if <=, 0 if not
  41. 1100 0111 0000 - pick - pops a number, pushes the number from N items down
  42. 1100 0111 0001 - pickout - pops a number, pushes the number from N items down and removes it from the stack
  43. 1100 0111 0010 - rotate - rotates the top three stack items
  44. 1100 0111 0011 - altstack - use the other stack from now on
  45. 1100 0111 0100 - -
  46. 1100 0111 0101 - -
  47. 1100 0111 0110 - -
  48. 1100 0111 0111 - -
  49. 1100 0111 1000 - -
  50. 1100 0111 1001 - -
  51. 1100 0111 1010 - -
  52. 1100 0111 1011 - -
  53. 1100 0111 1100 - -
  54. 1100 0111 1101 - -
  55. 1100 0111 1110 - -
  56. 1100 0111 1111 - -
  57. 1100 1nnn nnnn nnnn -(see note)- pushes address of next opcode, jumps n bytes ahead (up to 2048)
  58. 1101 0nnn - ld *reg - push the word pointed to by register
  59. 1101 1nnn - st *reg - pop a word, and store it in the memory location pointed to by register
  60. 1110 0nnn - inc reg - increment register
  61. 1110 1nnn - dec reg - decrement register
  62. 1111 nnnn nnnn nnnn - ld #num - push a full sized word
  63.  
  64. 1100 1nnn nnnn nnnn is specified with a [ starting the area to be skipped over, and a ] ending it
  65.  
  66.  
  67.  
  68. loop from 0 to 10:
  69. ld #0
  70. [
  71. dup
  72. ld #10
  73. ne?
  74. ]
  75. [
  76. inc
  77. dup
  78. ld #PrintDecimal
  79. call
  80. ]
  81. loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement