Category Archives: How-to
We’ve seen already how to read from the keyboard, perform some basic operations, and print results on the screen. This may be enough for a few very especific situations, but even the simplest programs will at some point need to take decisions based on the value of a certain variable. This is where conditional statements come in.
The smallest component of a program: statements
Before I explain conditional statements we need to understand what a statement is. To put it simply, a statement in C is a piece of code that performs an operation. So far we’ve seen two types of statements:
- Assignment statements (assigning a value to a variable):
i = 1234;
c = 'a';
c = getch();
- Calls to functions (performing an operation by calling a single function):
Note something very important: all of these end in a semicolon (;). While not technically correct, we could think of the semicolon as part of the statement.
These two types of statements are essential. We can’t write useful programs without them. However, they are not enough. We can’t code useful programs with just these two either. We need something else: conditional statements and loops.Read more ›
In the previous two posts we learnt how to operate with values stored inside variables in our program.
This time we’re going to see how to read characters and text strings from the keyboard in MSX-C.
Reading a single key press: getch()
MSX-C comes with a few functions to read a single character from the keyboard, but probably the easiest to understand is getch(). Let’s see an example:Read more ›
In the previous post we saw a couple of variable types and the basic arithmetic operators. Today we’ll complete this part by taking a look at the bitwise operators and seeing a couple of characteristics of the char type.
Bitwise arithmetic operators
You may be aware already that computers store all data internally in binary. This is also the case with all the data types in MSX-C. As an example, the table below shows how some integer values are represented internally in the MSX computer memory:
The computer always works in binary, but in most cases we don’t care how MSX-C stores data internally because the compiler handles these details for us. However, there are situations when working with binary data makes sense, as in the case of working with graphics or programming hardware devices.Read more ›
In the previous post we saw how to print text and numbers on the screen. This time we’ll learn how to use variables and how to operate with them.
You probably know already what a variable is. It’s just some place in the computer memory that we use to store a piece of data. In C, a variable can contain data of one of several basic types. Let’s take a look at them.
Character variables – the char type
We have already seen this type in some of the examples in previous posts. The program below assigns the character ‘X’ to the character variable c and prints it to the screen using putchar():Read more ›
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:Read more ›
We saw in the previous post how to run assembler routines from MSX-C. I’m sure you’re about to ask whether it’s possible to do the opposite: Is it possible to write programs in assembler and use the functions from the MSX-C Standard Library or the MSX-C Library disk?
I’m glad you asked. Yes, it’s possible, and it’s actually very easy to do.
Calling MSX-C library functions from assembler programs
The rules that we saw for how MSX-C passes parameters to assembler functions also apply to MSX-C’s own library functions. This means that we can leave arguments in the CPU registers or in the stack as appropriate and call the function just like that.
As always, let’s see an example:Read more ›
In the previous post we learnt a couple things about how MSX-DOS manages files and runs programs.
This post is more technical: we’ll see how to use assembler routines from MSX-C.
This subject is a bit more advanced that what we’ve seen so far. Feel free to skip this chapter for the time being if you don’t need to mix C and assembler yet. The reason why we’re going to see this now is that there are advanced users playing around with MSX-C who have asked how to do this.
Standard types in MSX-C
First of all we need to understand how the standard MSX-C data types are stored in memory.
16-bit values are always stored in memory with the low order byte first (little endian), and the high order byte in the memory address after it.
There’s also a couple things we have to keep in mind:
- In MSX-C pointers are always 16-bit values, the same as integer values
- The numeric types short and int are exactly the same thing
|char||8||0 to 255|
|short||16||-32768 to 32767|
|int||16||-32768 to 32767|
|unsigned||16||0 to 65535|
The previous post ended with the description of the first of the three MSX-DOS functions: I/O. This time we will see an introduction to the other two: file management and program execution.
Without doubt, the most useful peripheral in a computer system is the storage device, whether it’s a floppy disk drive, a hard disk, optical media, or some kind of flash storage attached via a generic interface.
Early MSX computers supported only cassette tape as storage media. It was slow and somewhat unreliable, but it was very cheap.
Besides the low speed, the big drawback of tapes was that the only way to access the data on them was sequentially: in order to access a program in a tape, you had to either read all other data stored before it, or manually rewind the tape to the location of the program you wanted to load. Needless to say, this wasn’t very fun.Read more ›
In the previous post we learnt about mnemonics and pseudoinstructions, symbols and labels. This is a good start, but learning assembler won’t be any use at all if we don’t also learn the environment where our programs will run. In our case this environment is going to be MSX-DOS, the MSX Disk Operating System. Let’s see what this means.
Functions of the operating system
The most basic functions of any operating system are:
- Input and output of data from/to peripherals (keyboard, screen, printer, etc)
- File management
- Running programs
MSX-DOS is able to handle all these, plus a few other tasks. let’s look at them in more detail:Read more ›
In the previous post we learnt what an assembler does and also the differences between assembly language and two high-level programming languages: BASIC and C.
This time we’ll see an actual assembly language program and compare it with a BASIC program that does the same function. We’ll learn about pseudoinstructions and labels, and we’ll see an example of an assemble list for our first assembly language program.
Note: so far I’ve been using the terms assembly language and assembler, the first one when talking about the programming language we use to write programs, and the second one to refer to the program that handles the task of converting the source code into a file we can run. However, many people often use the term assembler to refer to the assembly language as well. I’m one of these people. Because of this, keep the following in mind:
- Assembly language (or just assembly) always refers to the programming language
- Assembler may refer to either the programming language, or to the program used to assemble the source code
The context in the sentence should always make it clear to see which one we’re talking about.
Let’s start:Read more ›