Hello World in Befunge

Published on 27 November 2018 (Updated: 27 November 2018)

Hello World in Befunge

This is Hello World in Befunge, a unique two-dimentional, stack based programming language.

How to Implement the Solution

There are many valid ways to set up a Befunge program. People tend to design for either artistic or compactness purposes. A typical example for a Hello World looks a little something like this:

"!dlroW ,olleH"> , v
               | : <
               @

As stated earlier, everything works off of a stack. A stack in programming is a list of values, set up sort of like a stack of papers. Only the top number can be retrieved, and new numbers are always placed on the very top. Because of this, strings have to be read in backwards, as the last letter in will be the first letter out.

The program starts out in string mode to read in the characters. At the end of the first line, our stack will look something like this: (with translated characters below)

33 100 108 114 111 87 32 44 111 108 108 101 72
!  d   l   r   o   W     ,  o   l   l   e   H

The following section is a literal loop, to print out each of the values off the stack:

> , v
| : <
@

In order, the arrow sends it right, , prints out the H, the arrows loop it back around, : copies the top value of the stack, and | takes that copy for an if statement.

With |, if the top value is 0 (or null), then the pointer starts to go downwards, otherwise it is sent up. _ works the same way, except it means right on 0 and left for anything else.

The pointer will continue to run through the loop until the stack is empty. When it finishes, the if bar drops it down to the bottom. The @ indicates end of program. 1

How to run the solution

Because of the particular design of the language, your best option is a Befunge interpreter. This is both for availability and because watching it run is the only way to really understand (or debug) it.

There are compilers as well, even through the technical challenges:

For a much more thorough look at Befunge, it’s list of operators, and its various derivatives, I recommend the Esolang Wiki.


References

  1. S. Irwin, “Hello World in Befunge,” The Renegade Coder, 26-Nov-2018. [Online]. Available: https://therenegadecoder.com/code/hello-world-in-befunge/. [Accessed: 27-Nov-2018].