Character input

The programmer desiring to handle input on a character basis should consider using another language.

o:

a:

t:

Character Output

inside

backwards



Head position Required output Required binary Reverse binary Required head position Move head by

0 H 0100 1000 0001 0010 18 238 18 e 0110 0101 1010 0110 166 108 166 l 0110 1100 0011 0110 54 112 54 l 0110 1100 0011 0110 54 0

Hello, world

For those few of you who read the first part of this tutorial series, " Part Isa - Introduction, Manners & 'Hello, world' ", you would know that we are roughly 8% of our way to completing the classic program 'Hello, world' in INTERCAL. In short, we had managed to write the character 'h' to our standard output but we were not entirely sure how we had achieved this. In this tutorial we will boldly attempt to finish writing our first INTERCAL program and possibly try to understand how the program actually works.INTERCAL has very good support for the input and output of numbers. In accepting numeric input the following would be allowed:EIGHT OH NINEEIGHT ZERO NINERWALO WALA SIYAM:where all of these inputs mean '809', (the last being written in the Tagalog language). The 'WRITE IN' statement accepts numbers written in English, Sanskrit, Basque, Tagalog, Classical Nahuatl, Georgian, Kwakiutl, and Volapuk. INTERCAL will output numbers in Roman numerals, so in the example above we could use 'READ OUT' to output the value 809 as DCCCIX.To write our 'Hello, world' program, we are much more interested in character output rather than Roman numeric output. To understand character output in INTERCAL, you must first understand character input. When speaking of character input, the INTERCAL manual says:Indeed character input in INTERCAL is handled in a fashion that is significantly different from all other programming languages and as we will see soon character output is even more unique. To understand text input and output in INTERCAL we need to understand the Turing Text Model . It is possibly best described using the following diagram which I drew on the back of an envelope:We can imagine that INTERCAL has a circular input tape with all of the 256 available characters printed on it. INTERCAL also has an 'input head' which is positioned at the location of the last character entered by the user. The input head starts at position 0 (ASCII 0) when your INTERCAL program starts. If the user types 'g', as in the diagram, the input head will be moved to the 'g' on the input tape and the decimal value 103 (g is ASCII 103) will be stored in the first position of your array.So far, so good. It is when the user keeps on typing that things get tricky. If the user finishes typing the word 'goat', by typing 'oat', the following will result:The input head will move to the right by 8 positions to reach the 'o' from its initial position of 'g', so the decimal value 8 will be stored in the second position of your array.The input head can only travel to the right. So to reach the letter 'a', it must travel past the end of the 256 available characters and keep traveling until it reaches 'a'. To do this, it must travel 242 positions. So the decimal value 242 will be stored in the third position of the array.As with the simple case of 'o', the input head travels from 'a' to 't', storing the decimal value 19 in the fourth position of the array.Simple.When it comes to character output, there is some good news and some bad news. The good news is that the input tape and output tape (and their corresponding heads) are independent, which is much simpler than if they were connected. The bad news is that the previous sentence is the only good news.As with input in INTERCAL, described in the previous section, there is an output tape with all 256 ASCII characters printed on it and an output head. The tape travels in the same direction as the input tape, but the output tape is on theof the tape. This results in two subtle differences:1. The numbers required to move the head from one position to another are different because the output head is effectively moving in the opposite direction to the corresponding input head.2. Because the output head is on the inside of the tape, it sees the binary representation of the ASCII characters printed on it. For example, to print the ASCII character 'b', binary 0110 0010, you would need to move the output head to the ASCII 'F', binary 0100 0110.As with the input head, the output head starts at position zero. We can calculate the required head moves for the string 'Hello, world' in the following table::and so on. Continuing on these calculations you end up with the program:DO ,1 PLEASE ,1SUB#1 DO ,1SUB#2 DO ,1SUB#3 DO ,1SUB#4 DO ,1SUB#5 PLEASE ,1SUB#6 PLEASE ,1SUB#7 DO ,1SUB#8 DO ,1SUB#9 DO ,1SUB#10 DO ,1SUB#11 DO ,1SUB#12 DO ,1SUB#13 DO READ OUT ,1PLEASE GIVE UP:which when compiled and run gives the enormously satisfying output:I have written 'Hello, world' in many different programming languages over the past many years but I have never felt the sense of achievement that writing 'Hello, world' in INTERCAL has given me. Imagine the pride I would I feel if I built a small operating system using INTERCAL?'Hello, world' has only scratched the surface of the power and flexibility of INTERCAL. The astute reader will note that our program is quite linear, running from start to finish without branching or looping. In the next tutorial in my INTERCAL series we will explore some of the options that INTERCAL provides us with to create more complex programs.

Labels: character input, character output, goat, Reverse Binary, Turing Text Model