Page 276 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 276
9
Implementation of C Procedures
This chapter is perhaps the most important chapter in this book. We show how the 6812
assembly language implements C expressions and statements. We will use the HiWare
C++ compiler in our examples. While different compilers will generate different code for
the same C statement, studying one such implementation prepares you well to
understand other implementations.
We first discuss how C allocates and accesses global and local variables. Then we
consider how variables of different types are correctly coded in expressions and
assignment statements. Next we discuss the implementation of conditional statements.
We then describe how arrays and structs are accessed and then how loops are executed.
Finally we discuss procedure calls and arguments, and we present our conclusions.
After you study this chapter, you will be able to read the assembly-language output
of a C compiler with ease. One of the incidental benefits of this chapter is that you will
see how to implement many operations in assembly language, by reading a "definition"
of the problem to be solved in a C expression or statement and seeing the "solution" to
the problem in assembly language. You will also be able to write C code that produces
more efficient assembly-language code. As a further benefit, you will be able to fine-tune
a C procedure by replacing parts of it with assembly-language code that can be embedded
in the C procedure. Also, you will learn that you can write a C procedure that you can
debug on a personal computer and hand-compile it into an assembly-language program.
The C source program statements can be written in assembly-language comments to
document your assembly-language program. This is one way to quickly write complex
assembly-language programs.
This is therefore a very interesting chapter to complete the earlier chapters. You will
really understand how hardware, which we showed in Chapters 1 to 3 implemented the
6812 instruction set, becomes a powerful machine that executes C and C++ procedures,
in which you can express complex algorithms. You should be comfortable writing in a
high-level language like C or C++, knowing what really happens, right down to the
machine level, whenever you write an expression in your program.
We point out that the examples in this chapter are generated by a specific version
(5.0.8) of the HiWare C++ compiler, with selected optimization options. You can expect
to get slightly different code using different compilers, versions, or optimization options.
253