D. Interfacing to Basic. Microware C Compiler
تبلیغات
تبلیغات
Appendix D. Interfacing to Basic09
The object code generated by the Microware C Compiler can be made callable from the BASIC09 "RUN" statement. Certain portions of a BASIC09 program written in C can have a drmatic effect on execution speed. To effectively utilize this feature, one must be familiar with both C and BASIC09 internal data representation and procedure calling protocol.
C type "int" and BASIC09 type "INTEGER" are identical; both are two byte two’s complement integers. C type "char" and BASIC09 type "BYTE" and "BOOLEAN" are also identical. Keep in mind that C will sign-extend characters for comparisons yielding the range -128 to 127.
BASIC09 strings are terminated by 0xff (255). C strings are terminated by 0x00 (0).
If the BASIC09 string is of maximum length, the terminator is not present. Therefore, string length as well as terminator checks must be performed on BASIC09 strings when processing them with C functions.
The floating point format used by C and BASIC09 are not directly compatible.
Since both use a binary floating point format it is possible to convert BASIC09 reals to C doubles and vice-versa.
Multi-dimensional arrays are stored by BASIC09 in a different manner than C.
Multi-dimensional arrays are stored by BASIC09 in a column-wise manner; C stores them row-wise. Consider the following example:
BASIC09 matrix: DIM array(5,3):INTEGER
The elements in consecutive memory locations (read left to right, line by line) are stored as:
(1,1),(2,1),(3,1),(4,1),(5,1)
(1,2),(2,2),(3,2),(4,2),(5,2)
(1,3),(2,3),(3,3),(4,3),(5,3)
C matrix: int array[5][3];
(1,1),(1,2),(1,3)
(2,1),(2,2),(2,3)
(3,1),(3,2),(3,3)
(4,1),(4,2),(4,3)
(5,1),(5,2),(5,3)
Therefore to access BASIC09 matrix elements in C, the subscripts must be transposed. To access element array(4,2) in BASIC09 use array[2][4] in C.
The details on interfacing BASIC09 to C are best described by example. The remainder of this appendix is a mini tutorial demonstrating the process starting with simple examples and working up to more complex ones.
D.1. Example 1 - Simple Integer Aritmetic Case
This first example illustrates a simple case. Write a C function to add an integer value to three integer variables.
build bt1.c
? addints(cnt,value,s1,arg1,s2,arg2,s2,arg3,s4)
? int *value,*arg1,*arg2,*arg3;
? {
?
?
?
? }
*arg1 += *value;
*arg2 += *value;
*arg3 += *value;
?
That’s the C function. The name of the function is "addints". The name is information for C and c.link; BASIC09 will not know anything about the name. Page 9-13 of the BASIC09 Reference manual describes how BASIC09 passes parameters to machine language modules. Since BASIC09 and C pass parameters in a similar fashion, it is easy to access BASIC09 values. The first parameter on the BASIC09
113
دانلود
تبلیغات