The problem with printing text
In many languages, printing a message is one line. In Befunge, there is no built-in way to print a whole string at once. Instead, text has to be put onto the stack one character at a time, and then each character is taken off the stack and printed. Because a stack hands values back in the opposite order from how they went in, a little planning is needed to get the words out in the right order.
Pushing a string
Befunge has a special mode for text. When the pointer meets a double quote, it enters what is usually called string mode. Every cell the pointer passes over after that does not run as an instruction. Instead, the character’s code is pushed onto the stack. The next double quote switches the mode off again.
So the cells "abc" push the codes for a, then b, then c. The top of the stack is now c, the last one pushed. If you printed straight away, you would get c, b, a. This is the reversal that surprises most beginners, and it is closely related to the classic idea of string reversal with a stack.
The practical answer is to write the string backward in the source. If you want to print Hi, you push i first and H last, so H is on top and comes out first.
A small Hello World
Here is a compact version:
>25*"!dlroW olleH">:#,_@
Read it left to right, the way the pointer does.
>points the pointer to the right (it is already moving right, so this is just a marker).2and5push 2 and 5, and*multiplies them. The stack now holds 10, which is the code for a newline character. Placing it first means it ends up at the bottom, so it prints last."!dlroW olleH"is the text written backward. The!goes in first andHgoes in last, soHsits on top.- The second
>begins the print loop.
The print loop
The loop is :#,_ and it is worth going through slowly, because it packs several ideas into four cells.
:duplicates the top value. We need a spare copy because the test in a moment will use one up.#is a skip instruction. It makes the pointer jump over the very next cell, which is the,._pops a value and checks it. If the value is zero, the pointer goes right. If it is anything else, the pointer goes left.,pops a value and prints it as a character.
On the first pass, the copy of H is nonzero, so _ sends the pointer left. Moving left, the pointer lands on the , that was skipped earlier, and now it runs: it prints the original H. The pointer keeps moving left and meets # again. This time the skip jumps over the : and lands on the >, which turns the pointer right once more. Then : runs again, and the cycle repeats with the next character on the stack.
The skip instruction used in this way is often called the trampoline instruction, and it is a handy way to make a pointer bounce back into the middle of a loop.
How the loop ends
Each time around, one character is printed and removed. Eventually only the newline, 10, is left. It is nonzero, so it is duplicated, tested and printed like the rest. Now the stack is empty. The next : duplicates a value from an empty stack. In Befunge-93, taking a value from an empty stack gives zero, so the copy is zero. The _ sees zero and sends the pointer to the right, where it hits @ and the program ends.
That quiet behavior of reading zero from an empty stack is what lets the loop stop without a counter. It is also the sort of detail that can cause confusion elsewhere, which is why the topic of stack underflow gets its own treatment.
The finished output is the text Hello World! followed by a newline.
Changing the message
To print something else, write your new message backward between the quotes. For example, to print Go! you would put !oG inside the quotes. You can leave the newline setup and the loop exactly as they are. Everything from >:#,_@ stays the same, because the loop prints whatever is on the stack until it reaches the zero.
A few things to keep in mind:
- The text is typed in reverse, so check it by reading the quoted section from right to left.
- Spaces inside the quotes are pushed like any other character.
- Do not put a double quote inside the text, since that would end string mode early.
- The whole program should fit on the playfield, which is a limit in Befunge-93.
A different layout
Some people prefer a longer, stacked layout in which each row handles part of the message, using arrows to snake the path down the grid. That layout is easy to read but takes more room. The single-row version above is short, though a little dense. Choosing between the two is a question of code layout, and neither is more correct than the other.
What this example teaches
Even this short program touches most of the basic ideas of Befunge: the pointer moving across cells, the stack holding values, a string pushed in reverse, a test that decides which way to go, and a way to end. If you can explain each cell without looking at the notes, you understand the foundations of the language. The next steps are to learn the rest of the instructions and to practice following the pointer yourself, which makes later debugging much easier.