Original document September, 1993
Updated December, 1996
The PC in Befunge-93, however, is subject to different rules. It may go forward, backward, left or right. A Befunge-93 program is treated as an 80x25 torus (a page which wraps around on the edges) of ASCII text. Certain commands change the direction of the progress of the PC. By default, the PC points to the upper-left corner of the program, and is oriented to travel left-to-right.
Each command in Befunge-93 is a single character, as is the largest data unit; Befunge-93 programs have a maximum size of 80x25 total commands and data bytes. There are no run-time variables and a single run-timestack. Befunge-93 programs allow for self-modification. Due to the 2-dimensional nature of the PC, they allow for some extremely quirky code.
There are a few basic calculation commands:
In order to push a number greater than 9 on the stack, calculations must be done with numbers less than or equal to 9. In any other language this would be a pain. In Befunge-93 it is a joy. For example, to push '123' onto the stack, one might push 9, then 9, then multiply (leaving 81), then push 7, then 6, then multiply (leaving 81 and 42,) then add (leaving 123.) In Befunge, this would look something like :
NB. If the stack is be empty when you pop something off, be warned that this will not generate an underflow! It will simply return '0' to you. Hope you can live with it!
Should the PC encounter the 'edge' of the program, such as if you were to try to execute:
'_' acts like '<' if it is true, and '>' if it is false.
'|' acts like '^' if it is true, and 'v' if it is false.
'While' loops can be made by sticking an 'if' in an infinite loop. For example,
':' is the duplicating command. It makes a copy of the top element of the stack. This is useful, as demonstrated in the program below.
'$' pops a value off the stack, but does nothing with it. So,
'\' swaps the top two elements of the stack. So,
'`' (back-quote) is the 'greater' command. It compares the top two values on the stack, and returns '1' if the first is greater than the second.
COMMAND INITIAL STACK (bot->top)RESULT (STACK) ------- ------------- ----------------- + (add) <value1> <value2> <value1 + value2> - (subtract) <value1> <value2> <value1 - value2> * (multiply) <value1> <value2> <value1 * value2> / (divide) <value1> <value2> <value1 / value2> (nb. integer) % (modulo) <value1> <value2> <value1 mod value2> ! (not) <value> <0 if value non-zero, 1 otherwise> ` (greater) <value1> <value2> <1 if value1 > value2, 0 otherwise> > (right) PC -> right < (left) PC -> left ^ (up) PC -> up v (down) PC -> down ? (random) PC -> right? left? up? down? ??? _ (horizontal if) <boolean value> PC->left if <value>, else PC->right | (vertical if) <boolean value> PC->up if <value>, else PC->down " (stringmode) Toggles 'stringmode' : (dup) <value> <value> <value> \ (swap) <value1> <value2> <value2> <value1> $ (pop) <value> pops <value> but does nothing . (pop) <value> outputs <value> as integer , (pop) <value> outputs <value> as ASCII # (bridge) 'jumps' PC one farther; skips over next command g (get) <x> <y> <value at (x,y)> p (put) <value> <x> <y> puts <value> at (x,y) & (input value) <value user entered> ~ (input character) <character user entered> @ (end) ends program
Special thanks to Curtis Coleman, Jason Goga, Kalyna Zazelenchuk,Shawn Vincent, Mike Veroukis, Urban Mueller, and Wouter van Oortmerssen.