Welcome to part 2 of my Funge-98 tutorial. This part will cover the use of ascii characters within Funge and expand a bit more on numbers.Character Data
The first thing to understand about character data in Funge is that it is treated exactly like numbers! Inside of the Funge engine all data is treated as numbers. There is however a few commands that allow us to convert characters to numbers and vice versa.
Lets start with this program:
All except one command, the , command you already understand. When you run this program, see what you get! The , is similar to the . command in that it takes a value off of the stack and prints it, the difference is that the , interprets the value as an ascii character and prints the character instead of the number like the , command does, so this program will print the letter A rather than the number 65 that the computation comes out to. The , command is the only command that allows for output of ascii character data.
If you want to place ascii data onto the stack, there is actually a much easier way to do it. Try out this program:
The new command ' will take the next funge cell in front of the IP and interpret it as ascii data to be loaded onto the stack. This is the command you use whenever you want to place a single ascii value onto the stack.
There is also a method if you wish to place multiple characters on the stack at the same time, try this program:
The " command changes the mode that the IP is in. When the first " is encountered, the mode of the IP is changed to string mode. While in this mode, each cell the IP runs across will be loaded directly to the stack as ascii data. The IP will stay in string mode until it encounters another " command. Now run this program, do you understand why you got the result you did?
The Hello was printed backwards because the H was the first character pushed onto the stack, and therefore the last one to come, the o was the last character pushed onto the stack and therefore the first one to come off when the first , command is executed
To print the Hello in the correct direction, you need to reverse it on the stack, try this program:
This is the basic method for printing messages in Funge. The main problem with this method is that you need a , command for each character you want to print, with longer messages you could end up needing a lot of , commands, here is a better way to print messages, try this program:
This program will also print out the Hello message but no longer needing so many , commands. The new command here is the k command which will take a number off of the stack and then execute the instruction following the k command as many times as specified. So in the above example, 5 was pushed onto the stack, 5 letters in the word Hello, then the k command took the 5 off of the stack and then moved the IP forward to the next command, the , command which was then executed 5 times.
This method is better than just using , commands but still requires you to count how many characters are in the message and providing that count to the k command. This is fine for short messages, but what about longer ones? We will answer this question in a later lesson since some more advanced Funge techniques would be needed.More with Numbers
Lets use what we just learned by working with characters to make working with numbers a little bit simpler. Remember that the Funge engine deals with all data as numbers, even ascii data. This simplifies how we can produce some numbers within funge. Lets start by looking at this program:
You should be able to determine what this program will print before you even run it, right? If you do not understand why it printed what it did then revisit part 1 of the tutorial and reread the section on generating numbers.
Now, knowing what we now know about ascii character data, we can make it easier to get the 65 onto the stack, try this program now:
The method of using ascii characters can be used to create numbers from 32, by using a space after the ' command, up to 127, by using the ~ character after the ' command.
In addition to the 0 through 9 instructions, there are actually 6 more instructions that will put numbers onto the stack, these are the commands a, b, c, d, e, and f. For those who know hexidecimal numbers, these will look familiar, to those who do not, these characters represent the numbers 10 through 15, a=10, b=11 on up to f=15.
Here is an example of using these commands:
By this time you should be able to easily predict that this program will print the number 21.
Now take a look back at our original "Hello World" program:
Are there any mysteries left in this program now? If so, then rearead the tutorial up to this point and then look again.
Before ending this part of the tutorial, lets look at one more command. Lets start by creating this program:
This program will print out the number 625. Its method is 5 times 5 equals 25, keep that on the stack, then again 5 times 5 is 25, which is left on the stack for the final multiplication which will multiply the two 25s together to get the final 625.
Now lets shorten this just a bit, and try this program:
The new command here is the : command. This command will take the top value on the stack and duplicate it and put both of them back onto the stack. So after the first 25 is obtained with the sequence 55* the : command will duplicate the 25, so now there are 2 of them on the stack for the final * command to multiply. The : command is very useful when you need a duplicate of what is on top of the stack. Whenever you need to square numbers, this command is helpful. We will revisit the : command in a later lesson.
This concludes part 2 of this tutorial. Using what you have learned try experimenting with creating numbers using the ascii method as well as playing around with ascii characters.
|Command||What it does|
|a||Push 10 onto the stack|
|b||Push 11 onto the stack|
|c||Push 12 onto the stack|
|d||Push 13 onto the stack|
|e||Push 14 onto the stack|
|f||Push 15 onto the stack|
|k||Repeat the following command|
|,||Print the top value on the stack as a character|
|'||Read next funge cell as character and place on stack|
|"||Toggle string input mode|
|:||Duplicate top of stack|