Sunday, March 11, 2007

Part Dalawa - Still trying to write '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:


: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.

Character input

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:

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

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:

o: 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.

a: 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.

t: 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.


Character Output

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 the inside of 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 backwards. 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:

Head positionRequired outputRequired binaryReverse binaryRequired head positionMove head by
0H0100 10000001 001018238
18e0110 01011010 0110166108
166l0110 11000011 011054112
54l0110 11000011 0110540

:and so on. Continuing on these calculations you end up with the program:

DO ,1 <- #13
PLEASE ,1SUB#1 <- #238
DO ,1SUB#2 <- #108
DO ,1SUB#3 <- #112
DO ,1SUB#4 <- #0
DO ,1SUB#5 <- #64
PLEASE ,1SUB#6 <- #194
PLEASE ,1SUB#7 <- #48
DO ,1SUB#8 <- #22
DO ,1SUB#9 <- #248
DO ,1SUB#10 <- #168
DO ,1SUB#11 <- #24
DO ,1SUB#12 <- #16
DO ,1SUB#13 <- #214

:which when compiled and run gives the enormously satisfying output:

Hello, world

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: , , , ,


Blogger Chris Rathman said...

Don't know if it helps, but I put together an INTERCAL Cheatsheet a while back.

March 14, 2007 at 4:36 AM  
Anonymous Anonymous said...

This series is classic. :D

March 14, 2007 at 10:57 AM  
Blogger Aaron Davies said...

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.

it could be worse, it took genetic programming to breed a working "Hello, world" program for malbolge. it was the first program written in malbolge, and it came out two years after the language was released.

October 1, 2011 at 10:24 AM  
Blogger Patrick said...

To create a birthday greeting for a friend, I wrote a Google spreadsheet that simplifies the process of determining the values to store in the array:

DO ,1 <- #6
PLEASE ,1SUB#1 <- #94
DO ,1SUB#2 <- #156
DO ,1SUB#3 <- #32
DO ,1SUB#4 <- #96
DO ,1SUB#5 <- #88
PLEASE ,1SUB#6 <- #52

July 21, 2015 at 7:10 AM  

Post a Comment

<< Home