Uxntal

Личный сайт Go-разработчика из Казани

Uxntal is a stack-machine assembly language targeting the Uxn virtual machine.

Stack machine programming might look at bit odd, as it uses a postfix notation, which means that operators are always found at the end of an operation. For instance, one would write 3 4 + instead of 3 + 4.

The expression written (5 + 10) * 3 in conventional notation would be written 10 5 + 3 * in reverse Polish notation.

1( This is a comment ) 2 3( All programming in Unxtal is done by manipulating the stack ) 4 5#12 ( push a byte ) 6#3456 ( push a short ) 7 8( Uxn has 32 opcodes, each opcode has 3 possible modes ) 9 10POP ( pop a byte ) 11POP2 ( pop a short ) 12 13( The modes are: 14 [2] The short mode consumes two bytes from the stack. 15 [k] The keep mode does not consume items from the stack. 16 [r] The return mode makes the operator operate on the return-stack. ) 17 18#12 #34 ADD ( 46 ) 19#12 #34 ADDk ( 12 34 46 ) 20 21( The modes can be combined ) 22 23#1234 #5678 ADD2k ( 12 34 56 78 68 ac ) 24 25( The arithmetic/bitwise opcodes are: 26 ADD SUB MUL DIV 27 AND ORA EOR SFT ) 28 29( New opcodes can be created using macros ) 30 31%MOD2 { DIV2k MUL2 SUB2 } 32 33#1234 #0421 MOD2 ( 01 b0 ) 34 35( ---------------------------------------------------------------------------- ) 36 37( A short is simply two bytes, each byte can be manipulated ) 38 39#1234 SWP ( 34 12 ) 40#1234 #5678 SWP2 ( 56 78 12 34 ) 41#1234 #5678 SWP ( 12 34 78 56 ) 42 43( Individual bytes of a short can be removed from the stack ) 44 45#1234 POP ( 12 ) 46#1234 NIP ( 34 ) 47 48( The stack opcodes are: 49 POP DUP NIP SWP OVR ROT ) 50 51( ---------------------------------------------------------------------------- ) 52 53( To compare values on the stack with each other ) 54 55#12 #34 EQU ( 00 ) 56#12 #12 EQU ( 01 ) 57 58( Logic opcodes will put a flag with a value of either 00 or 01 ) 59 60#12 #34 LTH 61#78 #56 GTH 62 #0101 EQU2 ( 01 ) 63 64( The logic opcodes are: 65 EQU NEQ GTH LTH ) 66 67( ---------------------------------------------------------------------------- ) 68 69( Uxn's accessible memory is as follows: 70 256 bytes of working stack 71 256 bytes of return stack 72 65536 bytes of memory 73 256 bytes of IO memory ) 74 75( The addressable memory is between 0000-ffff ) 76 77#12 #0200 STA ( stored 12 at 0200 in memory ) 78#3456 #0201 STA2 ( stored 3456 at 0201 in memory ) 79#0200 LDA2 ( 12 34 ) 80 81( The zero-page can be addressed with a single byte ) 82 83#1234 #80 STZ2 ( stored 12 at 0080, and 34 at 0081 ) 84#80 LDZ2 ( 12 34 ) 85 86( Devices are ways for Uxn to communicate with the outside world 87 There is a maximum of 16 devices connected to Uxn at once 88 Device bytes are called ports, the Console device uses the 10-1f ports 89 The console's port 18 is called /write ) 90 91%EMIT { #18 DEO } 92 93#31 EMIT ( print "1" to console ) 94 95( A label is equal to a position in the program ) 96@parent ( defines a label "parent" ) 97 &child ( defines a sublabel "parent/child" ) 98 99( Label positions can be pushed on stack ) 100;parent ( push the absolute position, 2 bytes ) 101,parent ( push the relative position, 1 byte ) 102.parent ( push the zero-page position, 1 byte ) 103 104( The memory opcodes are: 105 LDZ STZ LDR STR 106 LDA STA DEI DEO ) 107 108( ---------------------------------------------------------------------------- ) 109 110( Logic allows to create conditionals ) 111 112#12 #34 NEQ ,skip JCN 113 #31 EMIT 114 @skip 115 116( Logic also allows to create for-loops ) 117 118#3a #30 119@loop 120 DUP EMIT ( print "0123456789" to console ) 121 INC GTHk ,loop JCN 122POP2 123 124( Logic also allows to create while-loops ) 125 126;word 127@while 128 LDAk EMIT 129 INC2 LDAk ,while JCN 130POP2 131BRK 132 133@word "vermillion $1 134 135( Subroutines can be jumped to with JSR, and returned from with JMP2r ) 136 137;word ,print-word JSR 138BRK 139 140@print-word ( word* -- ) 141 @while 142 LDAk EMIT 143 INC2 LDAk ,while JCN 144 POP2 145JMP2r 146 147@word "cerulean 148 149( The jump opcodes are: 150 JMP JCN JSR )

Ready For More?