Relearning MSX #25: Printing on the screen in MSX-CPosted by Javi Lavandeira in How-to, MSX, Retro, Technology | September 14, 2015
The last two posts were very technical: we saw how to write assembler routines and use them in MSX-C and also the opposite, how to write MSX-C code and then use it in assembler programs.
Today we’re going to go back to where we left it in chapter 18 and continue learning the basics of MSX-C. First, we’re going to see how to display text and numbers on the screen.
Displaying characters one at a time: putchar()
We already saw the function putchar() in chapter 18, but we didn’t explain at the time what this function does.
putchar() outputs a single character to the screen. We can pass the character inside single quotes (this is known as a character constant), or we can pass a variable that contains the character we want to output.
Let’s see a very simple example:
This small program prints the a character to the screen and exits. This is an example of what happens when we run it:
It’s not obvious in this screenshot, but unlike the BASIC statement PRINT, putchar() doesn’t output a new line character after the character. This example will make it clear:
This program prints three characters to the screen and then exits. The characters are printed one after the other:
Make sure to remember that putchar() only prints one character. That’s exactly what the single quote characters mean: one and only one character. There is a different syntax and different functions to print text strings.
Displaying text strings: puts()
Now that we know about putchar() we can in theory output anything we want to the screen, one character at a time. However, this would not be practical if we’re going to be printing lots of text. There must be a better way.
The answer is the puts() function. With this function we can output whole strings of text at a time. puts() accepts text strings enclosed in double quotes (we call them string constants), or a variable that contains a character array. We’ll see variables and arrays in the next post.
Let’s see a simple example: a program that prints the string constant “This tomato is too loud.” on the screen:
Compiling and running this example gives us the expected result:
Remember that, like putchar(), this function doesn’t add a new line after printing to the screen. This example will make it easier to see:
If we compile and run the program above we get this result:
Displaying numbers: printf()
Next, let’s see how to display numbers. We use the printf() function for this. Like the two previous functions, printf() doesn’t print a new line character.
As an example, if we want to print the number “12345” on the screen we would write this:
After we compile and run the program:
You may be wondering why we need to write that weird “%d” thing, when all we want is just print a number on the screen. The reason is that printf() can do many, many things other than just printing numbers. It can print numbers in decimal, hexadecimal or octal. It can also print the character matching an ASCII code, justify text, etc. Because it can do so many things we have to tell printf() exactly what we want it to do, and the %d code is one of these instructions. printf() replaces %d with the decimal representation of the number we gave it in the second parameter.
We’ll see in another post what other codes we can use with printf().
Displaying a new line: the newline character
The funtions we’ve seen so far (putchar(), puts() and printf()) don’t output a new line character automatically. This means that everything we print on the screen will be on the same line.
In order to move to a new line we have to tell these functions to print the code \n (in Japanese computers it will be displayed as ¥n). This code is known as the newline character. In our programs we represent it with two symbols (backslash and n), but it is actually a single character (the newline character, with ASCII code 10).
Let’s try it with puts():
After compiling and running this program:
NOW we’re getting new lines! It really makes text much easier to read.
The \n code doesn’t have to be at the end of the string. It can be anywhere:
Again, compiling and running the program gives us:
It is very, very important to remember that \n is a single character, even if it’s represented as a two-character code in the program. Remember that putchar() accepts only one character as a parameter. This means that we can use putchar() to output a newline on the screen:
Using puts() vs printf()
We’ve seen that printf() can be used to print anything on the screen, not just numbers. If that’s the case, why would we even care about puts()?
The reason is that printf() is a relatively complex function because it includes code to recognize and substitute all these control codes and formats. puts() doesn’t have any of this baggage, and therefore is smaller and faster.
Let’s see an example. I wrote two programs printing the same string to the screen, one of them using printf() and the other using puts():
These programs do exactly the same thing: they print “Hello!” on the screen, and nothing else:
However, if we look at the files on disk we will see that there’s a big difference in size:
Both programs do the same, but PUTS.COM is 2.5KB and PRINTF.COM is 3.3KB. The difference between these is the difference in size between puts() and printf(). puts() is also much faster because it doesn’t have to do any processing. In other words:
- Use printf() when you’re printing strings that contain formatting (numbers, word aligning/justification, etc).
- Use puts() when you’re printing constant strings that don’t change.
In this chapter we’ve seen a few functions to print things on the screen. Let’s recap them:
- putchar() – Short for put character. Prints a single character on the screen. The character has to be enclosed in single quotes.
- puts() – Short for put string. Prints a string of text on the screen. The string has to be enclosed in double quotes.
- printf() – Short for print formatted. Prints text or numbers on the screen. The string has to be enclosed in double quotes, and it supports especial codes to control how the text will be printed.
We’ve also seen the concepts of character constant (for example, ‘x’), string constant (“This is one”) and newline character (\n or ¥n depending on your computer’s keyboard layout).
In the next chapter…
We’ll continue with our introduction to MSX-C. We will see how to use variables and arrays, and how to do arithmetic and logical operations.
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