\section{Befunge} \definition{} \textit{Befunge} is another esoteric programming langauge, designed to be very difficult to compile. \par It consists of a \say{field} of instructions, a two-dimensional program counter, and a stack of values. \par \vspace{2mm} The program counter starts in the top-left corner of the field, moving right. \par It executes any instruction it encounters. The instructions \texttt{>}, \texttt{<}, \texttt{\^}, and \texttt{v} can be used to direct the program counter, and \texttt{\_} and \texttt{|} are used for control flow. \vfill An instruction reference is below: \begin{itemize}[itemsep=2mm] \item \texttt{+} Addition: Pop two values $a$ and $b$, then push the result of $a+b$ \item \texttt{-} Subtraction: Pop two values $a$ and $b$, then push the result of $b-a$ \item \texttt{*} Multiplication: Pop two values $a$ and $b$, then push the result of $a \times b$ \item \texttt{/} Integer division: Pop two values $a$ and $b$, then push the result of $b \div a$, rounded down. \item \texttt{\%} Modulo: Pop two values $a$ and $b$, then push the remainder of the integer division of $b \div a$. \item \texttt{!} Logical NOT: Pop a value. If the value is zero, push 1; otherwise, push zero. \item \texttt{ \`} Greater than: Pop two values $a$ and $b$, then push 1 if $b>a$, otherwise zero. \item \texttt{>} Program counter direction right \item \texttt{<} Program counter direction left \item \texttt{\textasciicircum} Program counter direction up \item \texttt{v} Program counter direction down \item \texttt{?} Random program counter direction \item \texttt{\_} Horizontal \texttt{if}: pop a value; set direction to right if value=0, set to left otherwise \item \texttt{|} Vertical \texttt{if}: pop a value; set direction to down if value=0, set to up otherwise \item \texttt{"} Toggle string mode (push each character's ASCII value all the way up to the next ") \item \texttt{:} Duplicate top stack value \item \texttt{\textbackslash} Swap top stack values \item \texttt{\$} Pop top of stack and discard \item \texttt{.} Pop top of stack and output as integer \item \texttt{,} Pop top of stack and output as ASCII character \item \texttt{\#} Bridge: jump over next command in the current direction of the current PC \item \texttt{g} A "get" call (a way to retrieve data in storage). Pop two values y and x, then push the ASCII value of the character at that position in the program. If (x,y) is out of bounds, push 0 \item \texttt{p} A "put" call (a way to store a value for later use). Pop three values y, x and v, then change the character at the position (x,y) in the program to the character with ASCII value v \item \texttt{\&} Get integer from user and push it \item \texttt{\~{}} Get character from user and push it \item \texttt{@} End program \item \texttt{0}-\texttt{9} Push corresponding number onto the stack \end{itemize} \vspace{2mm} Note that the \texttt{p} instruction allows us to write self-modifying code. \vfill \pagebreak \problem{} Write a program that prints \say{\texttt{Hello World}}. \problem{} Write a program that prints the alphabet \problem{} Write a program that generates a random sequence of numbers (\texttt{0}-\texttt{9}). \vfill \problem{} Write a program that does not contain the string \say{\texttt{Hello World}}, \par but writes that string somewhere inside its source. \problem{} Replace the \texttt{x}s in the following program so that the loop runs forever. \par Do not use any control-flow instructions (\texttt{>}, \texttt{<}, \texttt{\textasciicircum{}}, \texttt{v}, \texttt{\_}, \texttt{|}, \texttt{\#}, or \texttt{?}) \par \hint{ Start by replacing all the \texttt{x}s with spaces. \par You may not need all the \texttt{x}s, feel free to use a smaller rectangle. } \vspace{2mm} \begin{center} \texttt{>xxxxxxxxv \\ x ~ ~ ~ ~x \\ x ~ ~ ~ ~x \\ x ~ ~ ~ ~x \\ x ~ ~ ~ ~x \\ x ~ ~ ~ ~x \\ x ~ ~ ~ ~x \\ x ~ ~ ~ ~x \\ \textasciicircum{}xxxxxxxx@ } \end{center} \begin{solution} The intended solution is self-modifying code: \begin{center} \texttt{>69*6+97pv\\ p ~ ~ ~ ~8\\ 7 ~ ~ ~ ~8\\ 9 ~ ~ ~ ~*\\ * ~ ~ ~ ~0\\ 8 ~ ~ ~ ~0\\ 8 ~ ~ ~ ~p\\ \textasciicircum{}p00-2*88@ } \end{center} \end{solution} \vfill \problem{Bonus} Write a quine. (i.e, write a program that outputs itself) \begin{solution} \texttt{01->1\# +\# :\# 0\# g\# ,\# :\# 5\# 8\# *\# 4\# +\# -\# \_@} \end{solution} \vfill \pagebreak