Page 305 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 305
282 Chapter 9 Implementation of C Procedures
If a function member is executed, as in Sptr ->pull () , the object's hidden pointer has
the address of a table of function members; the specific function member is jumped to by
using a predetermined offset from the hidden pointer. If we wish to call the pull member
function, which might be at location 2 in this jump table, we can execute:
LDX [ Sptr, PCR ]; get the address of the jump table for the object pointed to by Sptr
JSR [ 2, X ] ; call the procedure at location 2 in the jump table (pull)
It will go to the jump table for the class for which Q was blessed by the new operator, to
get the address of the function member to pull data from the queue.
Observe that different objects of the same class point to the same table, but each
class has its own separate table. Note that data members of different objects of a class are
different data items, but function members of different objects of a class are common to
all the objects of the same class via this table.
A final technique used in C-H- is the templated class such as Stack<T>. Such a
templated class can potentially generate many classes such as stack<char>,
Stack<unsigned char>, stack<int>, and so on. Rather than generate, and store in
the microcontroller, all possible classes that can be obtained with different types for the
template, a templated class generates real code only when it is declared or blessed.
9.6 Examples from Character String Procedures
In order to provide additional examples of C compiled into assembly language, we will
compile some common C procedures that are used to handle character strings.
We first show strlen which is used to determine the number of characters in a
null-terminated string. See Figure 9.17. Notice how the argument str, passed in
accumulator D, is saved as a local variable right after the local variable s is pushed, and
is then on top of the stack.
1: int strlen(char *str){ char *s = str;
0000088A 3B PSHD
0000088B 3B PSHD
0000088C 6C82 STD 2,SP
2: while(*str++);
0000088E EE80 LDX 0,SP
00000890 E630 LDAB 1,X+
00000892 6E80 STX 0,SP
00000894 0471F7 TBNE B,*-6 ;abs = 088E
3: return (str - s - 1);
00000897 B754 TFR X,D
00000899 A382 SUED 2,SP
0000089B 830001 SUBD #1
4: }
0000089E 1B84 LEAS 4 fSP
000008AO 3D RTS
Figure 9.17. The Strlen Procedure