annotate docs/ccguide/basic09.appendix @ 554:afff0087c27f

findstr and signal finished.
author roug
date Tue, 22 Oct 2002 19:09:04 +0000
parents 60b821f18853
children 7d803625ead8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
1 <appendix>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
2 <title>Interfacing to Basic09</title>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
3 <para>
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
4 The object code generated by the Microware C Compiler can be made
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
5 callable from the BASIC09 "RUN" statement. Certain portions of a
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
6 BASIC09 program written in C can have a drmatic effect on execution
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
7 speed. To effectively utilize this feature, one must be familiar
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
8 with both C and BASIC09 internal data representation and procedure
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
9 calling protocol.
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
10 </para>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
11
554
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
12 <section>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
13 <title>Example 1 - Simple Integer Aritmetic Case</title>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
14 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
15 This first example illustrates a simple case. Write a C function to
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
16 add an integer value to three integer variables.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
17 <screen>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
18 build bt1.c
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
19 ? addints(cnt,value,s1,arg1,s2,arg2,s2,arg3,s4)
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
20 ? int *value,*arg1,*arg2,*arg3;
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
21 ? {
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
22 ? *arg1 += *value;
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
23 ? *arg2 += *value;
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
24 ? *arg3 += *value;
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
25 ? }
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
26 ?
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
27 </screen>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
28 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
29 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
30 That's the C function. The name of the function is "addints". The
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
31 name is information for C and c.link; BASIC09 will not know anything
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
32 about the name. Page 9-13 of the BASIC09 Reference manual describes
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
33 how BASIC09 passes parameters to machine language modules. Since
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
34 BASIC09 and C pass parameters in a similar fashion, it is easy to
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
35 access BASIC09 values. The first parameter on the BASIC09 stack is
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
36 a two-byte count of the number of following parameter pairs. Each
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
37 pair consists of an address and size of value. For most C
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
38 functions, the parameter count and pair size is not used. The
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
39 address, however, is the useful piece of information. The address
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
40 is declared in the C function to always be a "pointer to..." type.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
41 BASIC09 always passes addresses to procedures, even for constant
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
42 values. The arguments cnt, s1, s2, s3 and s4 are just place holders
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
43 to indicate the presence of the parameter count and argument sizes
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
44 on the stack. These can be used to check validity of the passed
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
45 arguments if desired.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
46 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
47 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
48 The line "int *value,*arg1,*arg2,*arg3" declares the parameters (in
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
49 this case all "pointers to int"), so the compiler will generate the
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
50 correct code to access the BASIC09 values. The remaining lines
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
51 increment each arg by the passed value. Notice that a simple
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
52 arithmetic operation is performed here (addition), so C will not
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
53 have to call a library function to do the operation.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
54 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
55 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
56 To compile this function, the following C compiler command line is
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
57 used:
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
58 <informalexample>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
59 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
60 cc2 bt1.c -rs
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
61 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
62 </informalexample>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
63 CC2 uses the Level-Two compiler. Replace cc2 with cc1 if you are
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
64 using the Level-One compiler. The -r option causes the compiler to
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
65 leave bt1.r as output, ready to be linked. The -s option suppresses
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
66 the call to the stack-checking function. Since we will be making a
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
67 module for BASIC09, cstart.r will not be used. Therefore, no
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
68 initialized data, static data, or stack checking is allowed. More
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
69 on this later.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
70 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
71 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
72 The bt1.r file must now be converted to a loadable module that
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
73 BASIC09 can link to by using a special linking technique as follows:
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
74 <informalexample>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
75 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
76 c.link bt1.r -b=addints -o=addints
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
77 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
78 </informalexample>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
79 This command tells the linker to read bt1.r as input. The option
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
80 "-b=addints" tells the linker to make the output file a module that
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
81 BASIC09 can link to and that the function "addints" is to be the
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
82 entrypoint in the module. You may give many input files to c.link
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
83 in this mode. It resolves references in the normal fashion. The
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
84 name given to the "-b=" option indicates which of the functions is
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
85 to be entered directly by the BASIC09 RUN command. The option
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
86 "-o=addints" says what the name of the output file is to be, in this
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
87 case "addints". This name should be the name used in the BASIC09
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
88 RUN command to call the C procedure. The name given in "-o="
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
89 option is the name of the procedure to RUN. The "-b=" option is
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
90 merely information to the linker so it can fill in the correct
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
91 module entrypoint offset.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
92 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
93
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
94 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
95 Enter the following BASIC09 program:
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
96 <programlisting>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
97 PROCEDURE btest
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
98 DIM i,j,k:INTEGER
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
99 i=1
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
100 j=132
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
101 k=-1033
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
102 RUN addints(4,i,j,k)
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
103 PRINT i,j,k
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
104 END
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
105 </programlisting>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
106 When this procedure is RUN, it should print:
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
107 <screen>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
108 5 136 -1029
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
109 </screen>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
110 indicating that our C function worked!
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
111 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
112
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
113 </appendix>