Page 246 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 246
8.2 Operators and Assignment Statements 223
it again in an interpreter without having to go through the lengthy process of compiling
the code. On the internet, programs can be sent to different servers to be executed.
Servers on the internet can immediately interpret a JAVA program regardless of which
server gets the code or from where it was sent. This interpretive language has proven to
be a very powerful tool for the internet.
Some interpreters are almost compilers. The high-level language is stored in
memory almost as written, but some words are replaced by tokens. For example, in the
preceding program, the word for could be replaced by a token $81. All the bytes in the
program would be ASCII characters, whose value would be below $7F. The original
high-level language can be regenerated from the information in memory, because the
tokens can be replaced by their (ASCII) character string equivalents. But as the program
is executed, the interpreter can essentially use the token $81 as a command, in this case
to set up a loop. It does not have to puzzle over what the (ASCII) characters f, o, and
r are before it can decide what the line means. These interpreters have the convenience
of a pure interpreter, with respect to the ease of changing the program, but they have
speeds approaching those of compilers. They are really partly compiler, to get the
tokens, and partly interpreter, to interpret the tokens and the remaining characters and
have some of the better features of both.
The state-of-the-art 6812 clearly illustrates the need for programming
microcontrollers in a high-level language and in object-oriented languages. Further, the
32K-byte flash memory of the MC68HC912B32 or the 4K-byte EEPROM memory of
the MC68HC812A4 is large enough to support high-level language programs. Also,
object-oriented features like modularity, information hiding, and inheritance will further
simplify the task of controlling 6812-based systems.
This chapter illustrates C and C++ programming techniques. C programming is
introduced first. The use of classes in C++ will be introduced at the end of this chapter.
While this introduction is very elementary and rather incomplete, it is adequate for the
discussion of how high-level languages generate machine code in the next chapter.
8.2 Operators and Assignment Statements
We first explain the basic form of a C procedure, the simple and the special numeric
operators, conditional expression operators, conditional statements, loop statements, and
functions. However, we do not intend to give all the rules of C you need to write good
programs. A C program has one or more procedures, of which the first to be executed is
called main, and the others are "subroutines" or "functions" if they return a value.
All the procedures, including main, are written as follows. Carriage returns and
spaces (except in names and numbers) are not significant in C programs and can be used
to improve readability. The periods (.) in the example below do not appear in C
programs but are meant here to denote that one or more declaration or statement may
appear. Each declaration of a parameter or a variable and each statement ends in a
semicolon (;), and more than one of these can be put on the same line. Parameters and
variables used in the 6812 are usually 8-bit (char), 16-bit (int), or 32-bit (long)
signed integer types. They can be declared unsigned by putting the word unsigned in
front of char, int, or long. In this and the next chapter we will not discuss long