annotate docs/ccguide/basic09.appendix @ 1669:eca9a9b181cd

current releasedefs
author boisy
date Fri, 23 Jul 2004 04:08:02 +0000
parents 7fb3d02e04b0
children
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>
631
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
11 <para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
12 C type "int" and BASIC09 type "INTEGER" are identical; both are two
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
13 byte two's complement integers. C type "char" and BASIC09 type
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
14 "BYTE" and "BOOLEAN" are also identical. Keep in mind that C will
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
15 sign-extend characters for comparisons yielding the range -128 to
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
16 127.
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
17 </para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
18 <para>
652
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
19 BASIC09 strings are terminated by 0xff (255). C strings are
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
20 terminated by 0x00 (0). If the BASIC09 string is of maximum length,
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
21 the terminator is not present. Therefore, string length as well as
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
22 terminator checks must be performed on BASIC09 strings when
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
23 processing them with C functions.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
24 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
25 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
26 The floating point format used by C and BASIC09 are not directly
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
27 compatible. Since both use a binary floating point format it is
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
28 possible to convert BASIC09 reals to C doubles and vice-versa.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
29 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
30 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
31 Multi-dimensional arrays are stored by BASIC09 in a different manner
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
32 than C. Multi-dimensional arrays are stored by BASIC09 in a column-wise
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
33 manner; C stores them row-wise. Consider the following example:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
34 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
35 <screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
36 BASIC09 matrix: DIM array(5,3):INTEGER
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
37 The elements in consecutive memory locations (read left to
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
38 right, line by line) are stored as:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
39 (1,1),(2,1),(3,1),(4,1),(5,1)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
40 (1,2),(2,2),(3,2),(4,2),(5,2)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
41 (1,3),(2,3),(3,3),(4,3),(5,3)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
42
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
43 C matrix: int array[5][3];
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
44 (1,1),(1,2),(1,3)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
45 (2,1),(2,2),(2,3)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
46 (3,1),(3,2),(3,3)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
47 (4,1),(4,2),(4,3)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
48 (5,1),(5,2),(5,3)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
49 </screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
50 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
51 Therefore to access BASIC09 matrix elements in C, the subscripts
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
52 must be transposed. To access element array(4,2) in BASIC09 use
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
53 array[2][4] in C.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
54 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
55 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
56 The details on interfacing BASIC09 to C are best described by
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
57 example. The remainder of this appendix is a mini tutorial
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
58 demonstrating the process starting with simple examples and working
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
59 up to more complex ones.
631
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
60 </para>
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
61
554
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
62 <section>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
63 <title>Example 1 - Simple Integer Aritmetic Case</title>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
64 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
65 This first example illustrates a simple case. Write a C function to
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
66 add an integer value to three integer variables.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
67 <screen>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
68 build bt1.c
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
69 ? addints(cnt,value,s1,arg1,s2,arg2,s2,arg3,s4)
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
70 ? int *value,*arg1,*arg2,*arg3;
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
71 ? {
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
72 ? *arg1 += *value;
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
73 ? *arg2 += *value;
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
74 ? *arg3 += *value;
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
75 ? }
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
76 ?
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
77 </screen>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
78 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
79 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
80 That's the C function. The name of the function is "addints". The
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
81 name is information for C and c.link; BASIC09 will not know anything
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
82 about the name. Page 9-13 of the BASIC09 Reference manual describes
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
83 how BASIC09 passes parameters to machine language modules. Since
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
84 BASIC09 and C pass parameters in a similar fashion, it is easy to
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
85 access BASIC09 values. The first parameter on the BASIC09 stack is
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
86 a two-byte count of the number of following parameter pairs. Each
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
87 pair consists of an address and size of value. For most C
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
88 functions, the parameter count and pair size is not used. The
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
89 address, however, is the useful piece of information. The address
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
90 is declared in the C function to always be a "pointer to..." type.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
91 BASIC09 always passes addresses to procedures, even for constant
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
92 values. The arguments cnt, s1, s2, s3 and s4 are just place holders
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
93 to indicate the presence of the parameter count and argument sizes
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
94 on the stack. These can be used to check validity of the passed
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
95 arguments if desired.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
96 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
97 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
98 The line "int *value,*arg1,*arg2,*arg3" declares the parameters (in
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
99 this case all "pointers to int"), so the compiler will generate the
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
100 correct code to access the BASIC09 values. The remaining lines
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
101 increment each arg by the passed value. Notice that a simple
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
102 arithmetic operation is performed here (addition), so C will not
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
103 have to call a library function to do the operation.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
104 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
105 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
106 To compile this function, the following C compiler command line is
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
107 used:
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
108 <informalexample>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
109 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
110 cc2 bt1.c -rs
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
111 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
112 </informalexample>
652
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
113 Cc2 uses the Level-Two compiler. Replace cc2 with cc1 if you are
554
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
114 using the Level-One compiler. The -r option causes the compiler to
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
115 leave bt1.r as output, ready to be linked. The -s option suppresses
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
116 the call to the stack-checking function. Since we will be making a
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
117 module for BASIC09, cstart.r will not be used. Therefore, no
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
118 initialized data, static data, or stack checking is allowed. More
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
119 on this later.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
120 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
121 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
122 The bt1.r file must now be converted to a loadable module that
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
123 BASIC09 can link to by using a special linking technique as follows:
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
124 <informalexample>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
125 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
126 c.link bt1.r -b=addints -o=addints
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
127 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
128 </informalexample>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
129 This command tells the linker to read bt1.r as input. The option
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
130 "-b=addints" tells the linker to make the output file a module that
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
131 BASIC09 can link to and that the function "addints" is to be the
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
132 entrypoint in the module. You may give many input files to c.link
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
133 in this mode. It resolves references in the normal fashion. The
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
134 name given to the "-b=" option indicates which of the functions is
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
135 to be entered directly by the BASIC09 RUN command. The option
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
136 "-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
137 case "addints". This name should be the name used in the BASIC09
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
138 RUN command to call the C procedure. The name given in "-o="
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
139 option is the name of the procedure to RUN. The "-b=" option is
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
140 merely information to the linker so it can fill in the correct
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
141 module entrypoint offset.
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
142 </para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
143
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
144 <para>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
145 Enter the following BASIC09 program:
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
146 <programlisting>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
147 PROCEDURE btest
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
148 DIM i,j,k:INTEGER
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
149 i=1
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
150 j=132
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
151 k=-1033
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
152 RUN addints(4,i,j,k)
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
153 PRINT i,j,k
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
154 END
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
155 </programlisting>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
156 When this procedure is RUN, it should print:
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
157 <screen>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
158 5 136 -1029
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
159 </screen>
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
160 indicating that our C function worked!
afff0087c27f findstr and signal finished.
roug
parents: 468
diff changeset
161 </para>
557
7d803625ead8 Signal is not finished at all.
roug
parents: 554
diff changeset
162 </section>
565
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
163
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
164 <section>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
165 <title>Example 2 - More Complex Integer Aritmetic Case</title>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
166 <para>
652
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
167 The next example shows how static memeory can be used. Take the
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
168 C function from the previous example and modify it to add the number
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
169 of times it has been entered to the increment:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
170 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
171 <screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
172 buld bt2.c
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
173 ? static int entcnt;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
174 ?
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
175 ? addints(cnt,cmem,cmemsiz,value,s1,arg1,s2,arg2,s2,arg3,s4)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
176 ? char *cmem;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
177 ? int *value,*arg1,*arg2,*arg3;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
178 ? {
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
179 ? #asm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
180 ? ldy 6,s base of static area
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
181 ? #endasm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
182 ? int j = *value + entcnt++;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
183 ?
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
184 ? *arg1 += j;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
185 ? *arg2 += j;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
186 ? *arg3 += j;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
187 ? }
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
188 ?
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
189 </screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
190 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
191 This example differs from the first in a number of ways. The line
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
192 "static in entcnt" defines an integer value name entcnt global to
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
193 bt2.c. The parameter cmem and the line "char *cmen" indicate a
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
194 character array. The array will be used in the C function for
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
195 global/static storage. C accesses non-auto and non-register
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
196 variables indexed off the Y register. cstart.r normally takes care
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
197 of setting this up. Since cstart.r will not be used for this
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
198 BASIC09-callable function, we have to take measures to make sure the
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
199 Y register points to a valid and sufficiently large area of memory.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
200 The line "ldy 6,s" is assembly language code embedded in C source
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
201 that loads the Y register with the first parameter passed by
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
202 BASIC09. If the first parameter in the BASIC09 RUN statement is an
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
203 array, and the "ldy 6,s" is placed <emphasis>immediately</emphasis> after the
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
204 "{" opening the function body, the offset will always be "6,s". Note the line
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
205 beginning "int j = ...". This line uses an initializer which, in
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
206 this case, is allowed because <varname>j</varname> is of class "auto".
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
207 No classes but "auto" and "register" can be initialized in BASIC09-callable C
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
208 functions.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
209 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
210 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
211 To compile this function, the following C compiler command line is
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
212 used:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
213 <screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
214 cc2 bt2.c -rs
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
215 </screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
216 Again, the -r option leaves bt2.r as output and the -s option
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
217 suppresses stack checking.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
218 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
219 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
220 Normally, the linker considers it to be an error if the "-b=" option
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
221 appears and the final linked module requires a data memory
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
222 allocation. In our case here, we require a data memory allocation
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
223 and we will provide the code to make sure everything is set up
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
224 correctly. The "-t" linker option causes the linker to print the
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
225 total data memory requirement so we can allow for it rather than
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
226 complaining about it. Our linker command line is:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
227 <screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
228 c.link bt2.r -o=addints -b=addints -r
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
229 </screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
230 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
231 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
232 The linker will respond with "BASIC09 static data size is 2 bytes".
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
233 We must make sure <varname>cmem</varname> points to at least 2 bytes of memory. The
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
234 memory should be zeroed to conform to C specifications.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
235 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
236 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
237 Enter the following BASIC09 program:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
238 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
239 <screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
240 PROCEDURE btest
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
241 DIM i,j,k,n;INTEGER
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
242 DIM cmem(10):INTEGER
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
243 FOR i=1 TO 10
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
244 cmem(i)=0
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
245 NEXT i
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
246 FOR n=1 TO 5
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
247 i=1
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
248 j=132
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
249 k=-1033
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
250 RUN addints(cmem,4,i,j,k)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
251 PRINT i,j,k
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
252 NEXT n
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
253 END
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
254 </screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
255 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
256 This program is similar to the previous example. Our area for data
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
257 memory is a 10-integer array (20 bytes) which is way more than the 2
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
258 bytes for this example. It is better to err on the generous side.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
259 Cmem is an integer array for convenience in initializing it to zero
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
260 (per C data memory specifications). When the program is run, it
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
261 calls addints 5 times with the same data values. Because addints
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
262 add the number of times it was called to the value, the i,j,k
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
263 values should be 4+number of times called. When run, the program prints:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
264 <screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
265 5 136 -1029
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
266 6 137 -1028
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
267 7 138 -1027
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
268 8 139 -1026
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
269 9 140 -1025
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
270 </screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
271 Works again!
565
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
272 </para>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
273 </section>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
274
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
275 <section>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
276 <title>Example 3 - Simple String Manipulation</title>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
277 <para>
652
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
278 This example shows how to access BASIC09 strings through C
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
279 functions. For this example, write the C version of SUBSTR.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
280 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
281 <screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
282 build bt3.c
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
283 ? /* Find substring from BASIC09 string:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
284 ? RUN findstr(A$,B$,findpos)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
285 ? returns in fndpos the position in A$ that B$ was found or
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
286 ? 0 if not found. A$ and B$ must be strings, fndpos must be
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
287 ? INTEGER.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
288 ? */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
289 ? findstr(cnt,string,strcnt,srchstr,srchcnt,result);
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
290 ? char *string,*srchstr;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
291 ? int strcnt, srchcnt, *result;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
292 ? {
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
293 ? *result = finder(string,strcnt,srchstr,srchcnt);
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
294 ? }
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
295 ?
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
296 ? static finder(str,strlen,pat,patlen)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
297 ? char *str,*pat;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
298 ? int strlen,patlen;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
299 ? {
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
300 ? int i;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
301 ? for(i=1;strlen-- &gt; 0 &amp;&amp; *str!=0xff; ++i)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
302 ? if(smatch(str++,pat,patlen))
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
303 ? return i;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
304 ? }
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
305 ?
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
306 ? static smatch(str,pat,patlen)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
307 ? register char *str,*pat;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
308 ? int patlen;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
309 ? {
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
310 ? while(patlen-- &gt; 0 &amp;&amp; *pat != 0xff)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
311 ? if(*str++ != *pat++)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
312 ? return 0;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
313 ? return 1;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
314 ? }
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
315 ?
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
316 </screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
317 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
318 Compile this program:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
319 <screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
320 cc2 bt3.c -rs
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
321 </screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
322 And link it:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
323 <screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
324 c.link bt3.r -o=findstr -b=findstr
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
325 </screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
326 The BASIC09 test program is:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
327 <screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
328 PROCEDURE btest
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
329 DIM a,b:STRING[20]
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
330 DIM matchpos:INTEGER
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
331 LOOP
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
332 INPUT "String ",a
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
333 INPUT "Match ",b
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
334 RUN findstr(a,b,matchpos)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
335 PRINT "Matched at position ",matchpos
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
336 ENDLOOP
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
337 </screen>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
338 When this program is run, it should print the position where the
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
339 matched string was found in the source string.
565
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
340 </para>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
341 </section>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
342
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
343 <section>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
344 <title>Example 4 - Quicksort</title>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
345 <para>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
346 The next example programs demonstrate how one might implement a
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
347 quicksort written in C to sort some BASIC09 data.
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
348 </para>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
349 <para>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
350 C integer quicksort program:
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
351 </para>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
352 <programlisting>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
353 #define swap(a,b) { int t; t=a; a=b; b=t; }
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
354
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
355 /* qsort to be called by BASIC09:
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
356 dim d(100):INTEGER any size INTEGER array
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
357 run cqsort(d,100) calling qsort.
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
358 */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
359
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
360 qsort(argcnt,iarray,iasize,icount,icsiz)
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
361 int argcnt, /* BASIC09 argument count */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
362 iarrary[], /* Pointer to BASIC09 integer array */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
363 iasize, /* and it's size */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
364 *icount, /* Pointer to BASIC09 (sort count) */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
365 icsiz; /* Size of integer */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
366 {
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
367 sort(iarray,0,*icount); /* initial qsort partition */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
368 }
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
369
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
370 /* standard quicksort algorithm from Horowitz-Sahni */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
371 static sort(a,m,n)
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
372 register int *a,m,n;
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
373 {
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
374 register i,j,x;
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
375
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
376 if(m &lt; n) {
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
377 i = m;
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
378 j = n + 1;
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
379 x = a[m];
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
380 for(;;) {
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
381 do i += 1; while(a[i] &lt; x); /* left partition */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
382 do j -= 1; while(a[j] &gt; x); /* right partition */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
383 if(i &lt; j)
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
384 swap(a[i],a[j]) /* swap */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
385 else break;
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
386 }
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
387 swap(a[m],a[j]);
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
388 sort(a,m,j-1); /* sort left */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
389 sort(a,j+1,n); /* sort right */
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
390 }
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
391 }
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
392 </programlisting>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
393 <para>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
394 The BASIC09 program is:
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
395 </para>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
396 <programlisting>
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
397 PROCEDURE sorter
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
398 DIM i,n,d(1000):INTEGER
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
399 n=1000
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
400 i=RND(-(PI))
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
401 FOR i=1 to n
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
402 d(i):=INT(RND(1000))
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
403 NEXT i
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
404 PRINT "Before:"
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
405 RUN prin(1,n,d)
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
406 RUN qsortb(d,n)
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
407 PRINT "After:"
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
408 RUN prin(1,n,d)
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
409 END
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
410
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
411 PROCEDURE prin
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
412 PARAM n,m,d(1000):INTEGER
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
413 DIM i:INTEGER
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
414 FOR i=n TO m
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
415 PRINT d(i); " ";
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
416 NEXT i
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
417 PRINT
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
418 END
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
419 </programlisting>
631
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
420 <para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
421 C string quicksort program:
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
422 </para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
423 <programlisting>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
424 /* qsort to be called by BASIC09:
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
425 dim cmemory:STRING[10] This should be at least as large as
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
426 the linker says the data size should
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
427 be.
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
428 dim d(100):INTERGER Any size INTEGER array.
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
429
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
430 run cqsort(cmemory,d,100) calling qsort. Note that the pro-
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
431 cedure name run in the linked OS-9
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
432 subroutine module. The module name
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
433 need not be the name of the C func-
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
434 tion.
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
435 */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
436
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
437 int maxstr; /* string maximum length */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
438
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
439 static strbcmp(str1,str2) /* basic09 string compare */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
440 register char *str1,*str2;
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
441 {
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
442 int maxlen;
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
443
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
444 for (maxlen = maxstr; *str1 == *str2 ;++str1)
652
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
445 if (maxlen-- &gt;0 || *str2++ == 0xff)
631
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
446 return 0;
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
447 return (*str1 - *str2);
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
448 }
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
449
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
450 cssort(argcnt,stor,storsiz,iaarray,iasize,elemlen,elsiz,
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
451 icount,icsiz)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
452 int argcnt; /* BASIC09 argument count */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
453 char *stor; /* Pointer to string (C data storage) */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
454 char iarray[]; /* Pointer to BASIC09 integer array */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
455 int iasize, /* and it's size */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
456 *elemlen, /* Pointer integer value (string length) */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
457 elsiz, /* Size of integer */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
458 *icount, /* Pointer to integer (sort count) */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
459 icsiz; /* Size of integer */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
460 {
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
461 /* The following assembly code loads Y with the first
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
462 arg provided by BASIC09. This code MUST be the first code
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
463 in the function after the declarations. This code assumes the
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
464 address of the data area is the first parameter in the BASIC09
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
465 RUN command. */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
466 #asm
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
467 ldy 6,s get addr for C storage
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
468 #endasm
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
469
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
470 /* Use the C library qsort function to do the sort. Our
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
471 own BASIC09 string compare function will compare the strings.
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
472 */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
473
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
474 qsort(iarray,*icount,maxstr=*elemlen,strbcmp);
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
475 }
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
476
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
477 /* define stuff cstart.r normally defines */
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
478 #asm
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
479 _stkcheck:
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
480 rts dummy stack check function
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
481
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
482 vsect
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
483 errno: rmb 2 C function system error number
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
484 _flacc: rmb 8 C library float/long accumulator
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
485 endsect
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
486 #endasm
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
487 </programlisting>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
488 <para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
489 The BASIC09 calling programs: (words file contains strings to sort)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
490 </para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
491 <programlisting>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
492 PROCEDURE ssorter
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
493 DIM a(200):STRING[20]
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
494 DIM cmemory:STRING[20]
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
495 DIM i,n:INTEGER
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
496 DIM path:INTEGER
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
497 OPEN #path,"words":READ
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
498
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
499 n=100
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
500 FOR i=1 to n
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
501 INPUT #path,a(i)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
502 NEXT i
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
503 CLOSE #path
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
504 RUN prin(a,n)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
505 RUN cssort(cmemory,a,20,n)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
506 RUN prin(a,n)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
507 END
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
508
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
509 PROCEDURE prin
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
510 PARAM a(100):STRING[20]; n:INTEGER
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
511 DIM i:INTEGER
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
512 FOR i=1 TO n
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
513 PRINT i; " "; a(i)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
514 NEXT i
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
515 PRINT i
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
516 END
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
517 </programlisting>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
518 </section>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
519
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
520 <section>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
521 <title>Example 5 - Floating Point</title>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
522 <para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
523 The next example shows how to access BASIC09 reals from C functions:
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
524 </para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
525 <programlisting>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
526 flmult(cnt,cmemory,cmemsiz,realarg,realsize)
652
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
527 int cnt; /* number of arguments */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
528 char *cmemory; /* pointer to some memory for C use */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
529 double *realarg; /* pointer to real */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
530 {
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
531 #asm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
532 ldy 6,s get static memory address
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
533 #endasm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
534
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
535 double number;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
536
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
537 getbreal(&amp;number,realarg); /* get the BASIC09 real */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
538 number *= 2.; /* number times two*/
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
539 putbreal(realarg,&amp;number); /* give back to BASIC09 */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
540
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
541 }
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
542
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
543 /* getreal(creal,breal)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
544 get a 5-byte real from BASIC09 format to C format */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
545
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
546 getbreal(creal,breal)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
547 double *creal,*breal;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
548 {
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
549 register char *cr,*br; /* setup some char pointers */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
550
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
551 cr = creal;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
552 br = breal;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
553 #asm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
554 * At this point U reg contains address of C double
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
555 * 0,s contains address of BASIC09 real
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
556 ldx 0,s get address of B real
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
557
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
558 clra clear the C double
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
559 clrb
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
560 std 0,u
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
561 std 2,u
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
562 std 4,u
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
563 stb 6,u
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
564 ldd 0,x
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
565 beq g3 BASIC09 real is zero
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
566
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
567 ldd 1,x get hi B mantissa
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
568 and a #$7f clear place for sign
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
569 std 0,u put hi C matissa
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
570 ldd 3,x get lo B mantissa
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
571 andb #$fe mask off sign
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
572 std 2,u put lo C mantissa
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
573 lda 4,x get B sign byte
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
574 lsra shift out sign
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
575 bcc g1
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
576 lda 0,u get C sign byte
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
577 ora #$80 tun on sign
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
578 sta 0,u put C sign byte
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
579 g1 lda 0,x get B exponent
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
580 suba #128 excess 128
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
581 sta 7,u put C exponent
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
582 g3 clra clear carry
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
583 #endasm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
584
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
585 }
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
586
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
587 /* putbreal(breal,creal)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
588 put C format double into a 5-byte real from BASIC09 */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
589
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
590 putbreal(breal,creal)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
591 double *breal,*creal;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
592 {
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
593 register char *cr,*br; /* setup some pointers */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
594
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
595 cr = creal;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
596 br = breal;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
597 #asm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
598 * At this point U reg contains address of C double
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
599 * 0,s contains address of BASIC09 real
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
600 ldx 0,s get address of B real
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
601
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
602 lda 7,u get C exponent
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
603 bne p0 not zero?
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
604 clra clear the BASIC09
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
605 clrb real
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
606 std 0,x
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
607 std 2,x
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
608 std 4,x
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
609 bra p3 and exit
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
610
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
611 p0 ldd 0,u get hi C mantissa
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
612 ora #$80 this bit always on for normalized real
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
613 std 1,x put hi B mantissa
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
614 ldd 2,u get lo C mantissa
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
615 std 3,x put lo B mantissa
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
616 incb round mantissa
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
617 bne p1
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
618 inc 3,x
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
619 bne p1
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
620 inc 2,x
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
621 bne p1
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
622 inc 1,x
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
623 p1 andb #$fe turn off sign
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
624 stb 4,x put B sign byte
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
625 lda 0,u get C sign byte
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
626 lsla shift out sign
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
627 bcc p2 bra if positive
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
628 orb #$01 turn on sign
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
629 stb 4,x put B sign byte
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
630 p2 lda 7,u get C exponent
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
631 adda #128 less 128
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
632 sta 0,x put B exponent
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
633 p3 clra clear carry
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
634 #endasm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
635 }
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
636
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
637
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
638 /* replace cstart.r definitions for BASIC09 */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
639 #asm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
640 _stkcheck:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
641 _stkchec:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
642 rts
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
643
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
644 vsect
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
645 _flacc: rmb 8
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
646 errno: rmb 2
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
647 endsect
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
648 #endasm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
649 </programlisting>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
650 <para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
651 BASIC09 calling program:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
652 </para>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
653 <programlisting>
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
654 PROCEDURE btest
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
655 DIM a:REAL
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
656 DIM i:INTEGER
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
657 DIM cmemory:STRING[32]
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
658 a=1.
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
659 FOR i=1 TO 10
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
660 RUN flmult(cmemory,a)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
661 PRINT a
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
662 NEXT i
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
663 END
631
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
664 </programlisting>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
665 </section>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
666
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
667 <section>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
668 <title>Example 6 - Matrix Elements</title>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
669 <para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
670 The last program is an example of accessing BASIC09 matrix elements.
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
671 The C program:
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
672 </para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
673 <programlisting>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
674 matmult(cnt,cmemory,cmemsiz,matxaddr,matxsize,scalar,scalsize)
652
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
675 char *cmemory; /* pointer to some memory for C use */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
676 int matxaddr[5][3]; /* pointer a double dim integer array */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
677 int *scalar; /* pointer to integer */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
678 {
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
679 #asm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
680 ldy 6,s get static memory address
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
681 #endasm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
682
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
683 int i,j;
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
684
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
685 for(i=0; i&lt;5; ++i)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
686 for(j=1; j&lt;3; ++j)
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
687 matxaddr[j][i] *= *scalar; /* multiply by value */
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
688 }
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
689 #asm
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
690 _stkcheck:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
691 _stkchec:
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
692 rts
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
693
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
694 vsect
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
695 _flacc: rmb 8
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
696 errno: rmb 2
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
697 endsect
7fb3d02e04b0 Appendix C is finished
roug
parents: 631
diff changeset
698 #endasm
631
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
699 </programlisting>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
700 <para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
701 BASIC09 calling program:
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
702 </para>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
703 <programlisting>
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
704 PROCEDURE btest
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
705 DIM im(5,3):INTEGER
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
706 DIM i,j:INTEGER
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
707 DIM cmem:STRING[32]
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
708 FOR i=1 TO 5
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
709 FOR j=1 TO 3
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
710 READ im(i,j)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
711 NEXT j
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
712 NEXT i
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
713 DATA 11,13,7,3,4,0,5,7,2,8,15,0,0,14,4
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
714 FOR i=1 TO 5
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
715 PRINT im(i,1),im(i,2),im(i,3)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
716 NEXT i
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
717 PRINT
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
718 RUN matmult(cmem,im,64)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
719 FOR i=1 TO 5
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
720 PRINT im(i,1),im(i,2),im(i,3)
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
721 NEXT i
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
722 END
594a34d027f8 Some progres on the BASIC09 examples
roug
parents: 565
diff changeset
723 </programlisting>
565
0e84dcd81835 Several functions ready.
roug
parents: 557
diff changeset
724 </section>
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
725 </appendix>