Relearning MSX #37: Control characters and C escape sequencesPosted by Javi Lavandeira in How-to, MSX, Retro, Technology | November 28, 2015
The MSX supports several control characters. Those are special characters that when printed on the screen don’t display any letter, number or symbol, but instead perform some special function such as clearing the screen or moving the cursor.
Let’s go take a look.
Control characters and C escape sequences
In C it is possible to print control characters to the screen just like any other number or letter. However, control characters don’t appear on the screen and because of this it is not possible to type them in our program. What we type instead is a pair of characters known as a C escape sequence that we use to represent each control character.
For example, we’ve seen already that the escape sequence \n (¥n if your MSX has a Japanese keyboard) represents the newline character, or what’s the same, the character with code 10 in the ASCII table.
MSX-C supports seven of these escape sequences:
Remember that if your MSX has a Japanese keyboard these will show with a yen sign (¥) instead of a backslash (\).
Printing these characters to the screen will perform the action shown in the Function column on the table above. Also, it doesn’t matter what method we use to output the character: as long as it goes to the output, it will work:
/* These three lines do exactly the same: clear the screen */ putchar('¥f'); puts("¥f"); printf("%c", 12);
Control characters without C escape sequence
Besides the 7 control characters above, the MSX supports a few more that don’t have a corresponding escape sequence in C:
Note the first two: control character 0x01 marks the beginning of a graphic character. We’ll learn about these in another post soon. The other one (0x02) marks the beginning of a terminal escape sequence. We’ll see those in the next post.
These control characters don’t have a corresponding C escape sequence, but that doesn’t mean that we can’t use them. In fact, we can print any character by typing “\x” followed by the character code in hexadecimal. For example, in our programs we can use the control character that moves the cursor one line down (code 0x1F) by referring to it as \x1f. The program below shows how this works: first we clear the screen by printing the control character \f, then we use control character \x1F to move the cursor one line down in the middle of printing a word:
Running this program results in this:
In the example above we enter the character code in hexadecimal, but we can also enter it in octal. In this case, instead of using the \x prefix, we just type a backslash followed by the character code in octal:
/* This is exactly the same as the puts() in ESCAPE1.C */ puts("DI \37 AG \37 ON \37 AL");
In this post we’ve learnt about control characters and their character codes. We’ve seen that some of them can be represented in C programs using especial combinations of characters that we call C escape sequences. There are some control codes that don’t have a corresponding escape sequence, but we can print those too by entering a backslash followed by the character’s code in hexadecimal or octal.
In the next post…
Next we’ll learn about a different kind of escape sequences: terminal escape sequences. Those are used to control the console (the screen) in a more complex way than using just control characters.
This series of articles is supported by your donations. If you’re willing and able to donate, please visit the link below to register a small pledge. Every little amount helps.
Javi Lavandeira’s Patreon page