PiDP-8/I SoftwareCheck-in [96ed492d9f]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merged the cc8-octal-output branch down to trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:96ed492d9f62d33100d4e7489509f4989b5b4c3d6a27a6a994bc757559ce6f15
User & Date: tangent 2019-03-17 04:55:56
Context
2019-03-17
04:57
Clarity fix check-in: ffce738e78 user: tangent tags: trunk
04:55
Merged the cc8-octal-output branch down to trunk. check-in: 96ed492d9f user: tangent tags: trunk
04:51
Merged in trunk improvements Closed-Leaf check-in: 9492282084 user: tangent tags: cc8-octal-output
04:41
Updated ChangeLog for upcoming release to March, February having passed. check-in: 617783a44d user: tangent tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ChangeLog.md.

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

























133
134
135
136
137
138
139
...
218
219
220
221
222
223
224
225



226
227
228
229
230
231
232
233
...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
        use a static binary image shipped in the Fossil code repository.
        The primary practical upshot of this is that you can now change
        the native OS/8 CC8 source code on the host side and just say
        “`make`” to get a new RK05 disk pack with the new code running
        in it. If you don’t get how cool this is, you don’t understand
        it properly. :)</p>

    *   <p>Warren Young greatly expanded the [CC8 user manual][cc8m]. It
        now answers many more questions, reveals many previously hidden
        details, fully documents LIBC’s behaviors, and documents the CC8
        memory model.</p>

    *   <p>Warren and Ian collaborated on fixes to the native compiler
        and its LIBC to fix a bunch of bugs and improve its conformance
        to Standard C. It’s still miles from passing any ISO C
        conformance suite, but it should violate fewer expectations now.
        This work does change the API and ABI of CC8’s LIBC somewhat, so
        if you have existing code, you might want to read the new manual
        to work out what’s needed to port that code.</p>


























*   Since the beginning of this project, we've called our modified
    version of the SIMH PDP-8 simulator `pidp8i-sim`.  With this
    release, we hard link that program to `pdp8`, the simulator's name
    in the upstream distribution of SIMH.  When called by that name, our
    simulator suppresses all of the PiDP-8/I extensions.

................................................................................
*   Fixed a bug going clear back to the epochal v20151215 release which
    can cause an OSR instruction to incorrectly set the Link bit if the
    next GPIO pin up from those used by the SR lines happens to be set
    when you issue that instruction.

*   The `tools/mkbootscript` program which translates palbart assembly
    listing files into SIMH boot scripts was only writing a SIMH "dep"
    command for the first word.  This affected some of the tty output



    from `hello.script` and `pep001.script`.

    While in there, made several other improvements to the script.

*   The `examples/hello.pal` program was badly broken in prior releases.

    *   <p>It was skipping the first character ("H") in its output
        message.</p>
................................................................................

*   The software now builds and runs on FreeBSD.  This just a step
    toward support for FreeBSD for the Raspberry Pi: we haven’t tried to
    make the GPIO stuff work at all yet.  For now, it just lets this
    software be used on your FreeBSD desktop or server machine.  It may
    allow building on other BSDs, but that is untested.

*   A year of maintenance and polishing, much of it resulting in
    documentation and build system improvements.

[cc8m]: https://tangentsoft.com/pidp8i/doc/trunk/doc/cc8-manual.md
[pv]:   https://tangentsoft.com/pidp8i/doc/trunk/README.md#systemd
[sc85]: https://github.com/ncb85/SmallC-85
[tctd]: https://tangentsoft.com/pidp8i/wiki?name=TD8E+vs+TC08
[v3df]: https://tangentsoft.com/pidp8i/wiki?name=OS/8+V3D+vs+V3F







|
|
|
|








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
>
>
>
|







 







|







114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
...
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
        use a static binary image shipped in the Fossil code repository.
        The primary practical upshot of this is that you can now change
        the native OS/8 CC8 source code on the host side and just say
        “`make`” to get a new RK05 disk pack with the new code running
        in it. If you don’t get how cool this is, you don’t understand
        it properly. :)</p>

    *   <p>Warren Young quadrupled the size of the [CC8 user manual][cc8m].
        It now answers many more questions, reveals many previously
        hidden details, fully documents LIBC’s interfaces and internal
        behaviors, and documents the CC8 memory model.</p>

    *   <p>Warren and Ian collaborated on fixes to the native compiler
        and its LIBC to fix a bunch of bugs and improve its conformance
        to Standard C. It’s still miles from passing any ISO C
        conformance suite, but it should violate fewer expectations now.
        This work does change the API and ABI of CC8’s LIBC somewhat, so
        if you have existing code, you might want to read the new manual
        to work out what’s needed to port that code.</p>

        <p>Notable improvements are that <tt>itoa()</tt> now takes a
        radix parameter to match its implementation in other C
        libraries; <tt>sprintf()</tt> returns an error code when the
        format string contains an unsupported format specifier; and the
        <tt>printf()</tt> family of functions now handle <tt>%x</tt> and
        <tt>%X</tt> properly. In the prior release, only <tt>%x</tt>
        was supported, and it gave uppercase output, not lowercase as
        the Standard requires.</p>

    *   <p>Warren changed CC8 to use octal when generating constants in
        SABR output, that being SABR’s default radix. Since this means
        SABR is always in octal mode, the primary user benefit of this
        is that inline assembly now behaves the same in CC8 as in OS/8
        FORTRAN II, which is also built atop SABR. That is to say, your
        inline assembly code can safely assume that the assembler is in
        octal mode when it processes your code.</p>

        <p>This does mean that if you had C programs built with CC8 that
        had inline assembly and that code had integer constants within
        it, it will have to be changed to work with the new
        compilers.</p>

        <p>The default radix for C code remains 10, so if you were not
        using inline assembly, this change does not affect you.</p>

*   Since the beginning of this project, we've called our modified
    version of the SIMH PDP-8 simulator `pidp8i-sim`.  With this
    release, we hard link that program to `pdp8`, the simulator's name
    in the upstream distribution of SIMH.  When called by that name, our
    simulator suppresses all of the PiDP-8/I extensions.

................................................................................
*   Fixed a bug going clear back to the epochal v20151215 release which
    can cause an OSR instruction to incorrectly set the Link bit if the
    next GPIO pin up from those used by the SR lines happens to be set
    when you issue that instruction.

*   The `tools/mkbootscript` program which translates palbart assembly
    listing files into SIMH boot scripts was only writing a SIMH "dep"
    command for the first word on a line of listing output.  It’s legal
    in PAL format listings to have multiple words on a line, as is
    common with data arrays and such. Any program that makes use of that
    feature is affected, including the tty output from `hello.script`
    and `pep001.script`.

    While in there, made several other improvements to the script.

*   The `examples/hello.pal` program was badly broken in prior releases.

    *   <p>It was skipping the first character ("H") in its output
        message.</p>
................................................................................

*   The software now builds and runs on FreeBSD.  This just a step
    toward support for FreeBSD for the Raspberry Pi: we haven’t tried to
    make the GPIO stuff work at all yet.  For now, it just lets this
    software be used on your FreeBSD desktop or server machine.  It may
    allow building on other BSDs, but that is untested.

*   Over a year of maintenance and polishing, much of it resulting in
    documentation and build system improvements.

[cc8m]: https://tangentsoft.com/pidp8i/doc/trunk/doc/cc8-manual.md
[pv]:   https://tangentsoft.com/pidp8i/doc/trunk/README.md#systemd
[sc85]: https://github.com/ncb85/SmallC-85
[tctd]: https://tangentsoft.com/pidp8i/wiki?name=TD8E+vs+TC08
[v3df]: https://tangentsoft.com/pidp8i/wiki?name=OS/8+V3D+vs+V3F

Changes to doc/cc8-manual.md.

1
2
3
4
5
6
7
8
















9
10
11
12
13
14
15
..
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

35
36


37
38
39
40
41
42
43
44
45


46
47

48
49
50
51




52
53
54
55
56
57
58
...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

128

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163


164
165
166
167
168
169

170


171


172


173

174
175
176
177
178
179
180
181
182







183
184
185
186
187
188
189
190
191

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

232
233
234
235
236
237
238
239
240
241
242
243
244
245



246

247
248
249
250
251
252









253
254
255

256
257
258
259
260
261
262
263
264



265
266
267
268

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
...
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
...
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352

353
354
355
356
357
358
359
360
361
362


363
364
365
366
367
368
369
370
371
372
373


374
375
376
377
378
379
380
...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
...
459
460
461
462
463
464
465
466
467
468
469
470
471




472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
....
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
....
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
....
1495
1496
1497
1498
1499
1500
1501
































































1502
1503
1504
1505
1506
1507
1508
....
1722
1723
1724
1725
1726
1727
1728






















1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
....
1761
1762
1763
1764
1765
1766
1767
1768

1769
1770

1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799

1800
1801
1802
1803
1804
1805
1806
# CC8 Manual


## A Bit of Grounding History

The C language and its derivatives are now the industry standard for the
development of operating systems and utilities. The language has evolved
significantly since its initial specification in 1972.

















The first implementation of C was on the PDP-11 as part of the early
work on the Unix operating system, and it was initially used to write
system utilities that otherwise would have been written in assembly. A C
language compiler first appeared publicly in Version 2 Unix, released
later in 1972. Much of PDP-11 Unix remained written in assembly until
its developers decided to rewrite the operating system in C, for Version
................................................................................
4 Unix, released in 1973. That decision allowed Unix to be relatively
easily ported to a wholly different platform — the Interdata 8/32 — in
1978 by writing a new code generator for the C compiler, then
cross-compiling everything. That success in porting Unix lead to C’s own
success first as a systems programming language, and then later as a
general-purpose programming language.

The PDP-8 was introduced by DEC in 1965 with the intention of being a
small and cheap processor that could be used in a variety of
environments. From this simple machine, the modern desktop computer
evolved, some of which were used in the writing of this document. The
PDP-8 is also arguably the ancestor of the Raspberry Pi you may be using
our CC8 compiler on, as part of the PiDP-8/I project.

The PiDP-8/I project is part of an effort to prevent the PDP-8 from
sliding into undeserved obscurity. Whether you consider it the ancestor
of the desktop computer or the embedded processor, it is a machine worth
understanding.



## CC8’s Developmental Sparks



CC8 is a C subset implementation for the DEC PDP-8 processor.

The CC8 project’s creator (Ian Schofield) thought it was time to have a
modern language compiler running on the PDP-8. Ian actually wrote *two*
C compilers:

1.  A [cross-compiler](#cross) that builds and runs on any host computer
    with a C compiler that still understands K&R C.



2.  A [native OS/8 compiler](#native), compiled to assembly by the

    cross-compiler.

Ian also collected and wrote the [LIBC implementation](#libc) common to
both compilers.





The last high-level language compiler to be attempted for the PDP-8, as
far as this document’s authors are aware, was Pascal in 1979 by Heinz
Stegbauer.

In more recent times, Vince Slyngstad and Paolo Maffei wrote a C
cross-compiler based on Ron Cain’s Small-C using a VM approach. [This
................................................................................
independently of OS/8, but the bounds on that class of programs is not
currently clear to us.


<a id="cross" name="posix"></a>
## The Cross-Compiler

The features of the cross-compiler are basically that of Small-C itself,
being a good approximation of K&R C (1978) minus:

*   most of the standard library (see [below](#libc) for what we *do*
    have)

*   function pointers


*   `float` and `long`


The code for this is in the `src/cc8/cross` subdirectory of the PiDP-8/I
source tree, and it is built along with the top-level PiDP-8/I software.
When installed, this compiler is in your `PATH` as `cc8`.

Ian Schofield wrote the CC8 cross-compiler as a [SABR][sabr] code
generator for Ron Cain’s famous Small-C compiler, originally published
in [Dr Dobb’s Journal][ddj], with later versions published elsewhere.
William Cattey later ported this code base to Small-C 85, a living
project currently [available on GitHub][sc85].

This means the cross compiler more or less adheres to the *language*
dialect of C as published in "The C Programming Language," by Kernighan
and Ritchie, first edition, 1978. The reader is directed to the extensive
documentation of Small-C available on the web for further details. You
may also find references for K&R C 1978 helpful.

We stress *language* above because we have not attempted to clone the C
Standard Library as of K&R 1978.  CC8 has a [very limited standard
library](#libc), and it has many weaknesses relative to even early
versions of C. See that section of this manual for details about known
limitations, exclusions, and bugs.

The CC8 cross-compiler can successfully compile itself, but it produces
a SABR assembly file that is too large (28K) to be assembled on the
PDP-8.  Thus [the separate native compiler](#native).

The key module for targeting Small-C to the PDP-8 is `code8.c`. It
does the code generation to emit SABR assembly code. However, the
targeting is not confined to that one file. There is code in various
of the other modules that is specific to the PDP-8 port that should be
abstracted out and cleaned up in the fullness of time.

SABR is normally used as the second pass of the OS/8 FORTRAN II
system.



When you use the cross-compiler on a POSIX type system such as the
Raspbian PiDP-8/I environment, the resulting `*.sb` files will have
LF-only line endings, but OS/8 expects CR+LF line endings. The `txt2ptp`
utility program included with the PiDP-8/I distribution will
automatically do that conversion for you when making a SIMH paper tape

image file, which you can then read into the OS/8 environment.





[ddj]:  https://en.wikipedia.org/wiki/Dr._Dobb%27s_Journal


[sabr]: /wiki?name=A+Field+Guide+to+PDP-8+Assemblers#sabr

[sc85]: https://github.com/ncb85/SmallC-85


<a id="cpp"></a>
### The Cross-Compiler’s Preprocessor Features

The cross-compiler has rudimentary C preprocessor features:

*   Literal `#define` only.  You cannot define parameterized macros.







    There are no `-D` or `-U` flags to define and undefine macros from
    the command line.

*   `#undef` to remove a symbol previously defined with `#define`

*   `#include`, but only for files in the current directory.  There is
    no include path, either hard-coded within the compiler or modifiable
    via the traditional `-I` compiler flag. #include can appear within
    an included file but limited to 3 levels deep.


*   [Inline assembly](#asm) via `#asm`.

*   Simple `#ifdef` and `#ifndef` with direct match of symbol or macro
    names but not expressions. `#endif` is requried. `#else` is allowed.
    There is no support for `#if`.

*   **TBD:** Token pasting?

*   **TBD:** Stringization?


### <a id="nhead"></a>Necessary Headers

There are two header files shipped with CC8, intended to be used only by
users of the cross-compiler:

*   `libc.h` — Declares the entry points used by [LIBC](#libc) using
    CC8 [library linkage directives](#linkage). If your program makes
    use of any library functions, you must `#include` this at the top of
    your program.

*   `init.h` — Inserts a block of [inline assembly](#asm) startup code
    into your program, which initializes the program environment, sets
    up LIBC, and defines a few low-level routines. Certain programs may
    get away without this code, but the rules for which programs and why
    are not currently clear to us.  **TODO:** Find out the rules that
    govern whether this is necessary.

As a rule, all cross-compiler users should include both of these at the
top of every program.

Because the cross-compiler lacks an include path feature, you generally
want to symlink these files to the directory where your source files
are. This is already done for the CC8 examples and such.

If you compare the examples in the source tree (`src/cc8/examples`) to
those with uppercased versions of those same names on the OS/8 `DSK:`
volume, you’ll notice that these `#include` statements were stripped
out. This is [necessary](#os8pp); the linked documentation tells you why

and how the OS/8 version of CC8 gets away without a `#include` feature.

The tool that strips these `#includes` out for us is called
`bin/cc8-to-os8`, which you might find useful if you’re frequently
working with programs that need to work under both compilers.

Alternatively a simple `sed` script could be used:

    sed '/^#include/d' <yourfile>

<a id="native" name="os8"></a>
## The Native OS/8 Compiler

This compiler’s source code is in the `src/cc8/os8` subdirectory of the



PiDP-8/I software distribution.


Unlike in the original CC8 distribution or in past distributions of the
PiDP-8/I software, we no longer need to ship binaries for the compiler
to bootstrap the system. Due to the power of [`os8-run`][os8r] and the
PiDP-8/I software build system, we now bootstrap CC8 environment
entirely from source code unless the user passes the `--disable-os8-cc8`









option to the `configure` script.  This process is controlled by the
[`cc8-tu56.os8`][cctu] script, which you may want to examine along with
the `os8-run` documentation to understand this process better.


If you change the OS/8 CC8 source code, saying `make` at the PiDP-8/I
build root will update `bin/v3d.rk05` with new binaries automatically.

Because the CC8 native compiler is compiled by the CC8 *cross*-compiler,
the [standard memory layout](#memory) applies to both.  Among other
things, this means each pass of the native compiler requires
approximately 20&nbsp;kWords of core.




<a id="ncpass"></a>The compiler passes are:

1.  `c8.c` &rarr; `c8.sb` &rarr; `CC.SV`: The compiler driver: accepts
    the input file name from the user, and calls the first proper

    compiler pass, `CC1`.

2.  `n8.c` &rarr; `n8.sb` &rarr; `CC1.SV`: The parser/tokeniser section
    of the compiler.

3.  `p8.c` &rarr; `p8.sb` &rarr; `CC2.SV`: The token to SABR code
    converter section of the compiler.

`CC.SV` contains extremely rudimentary preprocessor features
documented [below](#os8pp).

There is also `libc.c` &rarr; `libc.sb` &rarr; `LIBC.RL`, the [C
library](#libc) linked to any program built with CC8, including the
passes above, but also to your own programs.

All of these binaries end up on the automatically-built OS/8 boot disk:
`CC?.SV` on `SYS:`, and everything else on `DSK:`, based on the defaults
our OS/8 distribution is configured to use when seeking out files.
................................................................................
1.  **Recursion:** See [`FIB.C`][fib] for an example of this.

1.  **Simple arithmetic operators:** `+`, `-`, `*`, `/`, etc.

1.  **Bitwise operators:** `&`, &brvbar;, `~` and `!`

1.  **Simple comparison operators:** False expressions evaluate as 0 and
    true as -1 in twos complement form, meaning all 1's in binary form.
    See the list of limitations below for the operators excluded by our
    "simple" qualifier.

1.  **A few 2-character operators:** `++`, `--` (postfix only) and `==`.

1.  **Limited library:** See [below](#libc) for a list of library
    functions provided, including their known limitations relative to
................................................................................

[fib]: /doc/trunk/src/cc8/examples/fib.c


<a id="nlim" name="limitations"></a>
### Known Limitations of the OS/8 CC8 Compiler

The OS/8 version of CC8 is missing many language features relative to
[the cross-compiler](#cross), and much more compared to modern C.

1.  <a id="typeless"></a>The language is typeless in that everything is
    a 12 bit integer and any variable/array can interpreted as `int`, `char`
    or pointer.  All variables and arrays must be declared as `int`.
    The return type may be left off of a function's definition; it is
    implicitly `int` in all cases, since `void` is not supported.

    **TBD** A recent update may have added void.


    Further to this point, in the OS/8 version of CC8, it is optional
    to declare the types of the arguments to a function. For example,
    the following is likely to be rejected by a strictly conforming
    K&R C compiler, but it is legal in OS/8 CC8 because the types
    are already known, there being only one data tyype in OS/8 CC8:

        myfn(n) { /* do something with n; optionally return something */ }

    This declares a function taking an `int` called `n` and returning


    an `int`. Contrast the CC8 cross-compiler, which requires the
    function's argument type to be declared, if not the return type:

        myfn(n)
        int n;
        {
            /* do something with n; optionally return something */
        }

    (The return type cannot be `void` since there is no `void` in
    K&R C as published in 1978, thus not in CC8, either.)



2.  There must be an `int main()`, and it must be the last function
    in the single input C file.

    Since OS/8 has no way to pass command line arguments to a program
    — at least, not in a way that is compatible with the Unix style
    command lines expected by C — the `main()` function is never
................................................................................
    support for preprocessor directives.

    *   Literal `#define` only: no parameterized macros, and no `#undef`.

    *   `#include` is not supported and must not appear in the C source
        code fed to the Native OS/8 Compiler.

        You cannot use `#include` directives to string multiple C modules
        into a single program.

        It also means that if you take a program that the cross-compiler
        handles correctly and just copy it straight into OS/8 and try to
        compile it, it probably still has the `#include <libc.h>` line and
        possibly one for `init.h` as well. *Such code will fail to compile.*
        You must strip such lines out when copying C files into OS/8.

        (The native compiler emits startup code automatically, and it
        hard-codes the LIBC call table in the [final compiler
        pass](#ncpass), implemented in `p8.c`, so it doesn’t need
        `#include` to make these things work.)

    *   [Broken](#os8asm) handling of [inline assmembly](#asm) via `#asm`.

    *   No support for `#if`, `#ifdef`, etc.

5.  Variables are implicitly `static`, even when local.

6.  Arrays may only be single indexed. See `PS.C` for an example.

7.  The compiler does not yet understand how to assign a variable's
    initial value as part of its declaration. This:
................................................................................
    expression to a form that is now legal in our limited C dialect. All
    of this comes from the Laws section of the linked Wikipedia article;
    if you learn nothing else about Boolean algebra, you would be well
    served to memorize those rules.

9. Dereferencing parenthesized expressions does not work: `*(<expr>)`

10. The stack, which includes all globals and literals, is only 4&nbsp;kWords.
    Stack overflow is not detected.  Literals are inlcuded in this due
    to a limitation in the way `COMMN` is implemented in SABR.

11. There is no argument list checking, not even for standard library
    functions.





12. `do/while` loops are parsed, but the code is not properly generated.
    Regular `while` loops work, as does `break`, so one workaround for a
    lack of `do/while` is:

        while (1) { /* do something useful */; if (cond) break; }

    We have no intention to fix this.

13. `switch` doesn't work.

The provided [LIBC library functions](#libc) is also quite limited and
nonstandard compared to Standard C.  See the documentation for each
individual library function for details.


<a id="warning"></a>
#### GOVERNMENT HEALTH WARNING

**You are hereby warned**: The native OS/8 compiler does not contain any
error checking whatsoever. If the source files contain an error or you
................................................................................

Another set of examples not preinstalled on the OS/8 disk are
`examples/pep001-*.c`, which are described [elsewhere][pce].

[pce]: /wiki?name=PEP001.C


## Making Executables 

Executing `CCR.BI` loads, links, and runs your C program without
producing an executable file on disk.  You need only a small variation
on this BATCH file's contents to get an executable core image that
you can run with the OS/8 `R` command:

    .R CC                   ⇠ kinda like Unix cc(1)
................................................................................
data field](#udf). That is to say, the code generated by CC8 does not
use 15-bit extended addresses; it just flips between pages depending on
what type of data or code it’s trying to access.

This means it is possible to iterate a pointer past the end of a 4096
word core memory field, causing it to wrap around to 0 and continue
blithely along.  Since the last page of the user data field [is reserved
for use by OS/8](#os8res) and the first page of each field is [reserved
by the LOADER run time system](#ldrts), programs that do this will most
likely crash and may even destroy data. Our [LIBC implementation](#libc)
generally does not try to check for such wraparound problems, much less
signal errors when it happens. The programmer is expected to avoid doing
this.

Code that operates on pointers will generally only do its work within
the user data field. You will likely need to resort to [inline
assembly](#asm) and `CIF`/`CDF` instructions to escape that field.
Getting our [LIBC](#libc) to operate on other fields may be tricky or
even more difficult than it’s worth.

................................................................................
There is no `malloc()` in this C library and no space reserved for its
heap in [the user data field](#udf). Everything in a CC8 program is
statically-allocated, if you’re using stock C-level mechanisms. If your
program needs additional dynamically-allocated memory, you’ll need to
arrange access to it some other way, such as [via inline
assembly](#asm).


































































### <a id="flayout"></a>Field Layout, Concrete Example

The field layout given [at the start of this section](#memory) is not
fixed. The linking loader is free to use any layout it likes, consistent
with any constraints in the input binaries. You can use the `/M` option
with `LOADER.SV` to get a core memory map for a given output. Let’s work
................................................................................
That example is based on real code, the implementation of
[`itoa()`](#itoa) for radices beyond 10: we tried it both ways and ended
up doing it the obscure way to save code space in LIBC.

For the most part, CC8 currently leaves the task of optimization to the
end user.
























### <a id="linkage" name="varargs"></a>Library Linkage and Varargs

CC8 has some non-standard features to enable the interface between the
main program and the C library. This constitutes a compile time linkage
system to allow for standard and vararg functions to be called in the
library.

**TODO:** Explain this.


### <a id="os8asm"></a>Inline Assembly in the Native CC8 Compiler

#### Limitations

The native compiler has some significant limitations in the way it
handles inline assembly.

The primary one is that snippets of inline assembly are gathered by the
[first pass](#ncpass) of the compiler in a core memory buffer that’s
only 1024 characters in size. If the total amount of inline assembly in
................................................................................
[`fopen()`](#fopen) is limited to a [single output file at a
time](#fiolim) and it cannot append to an existing file, it’s got one
shot to write everything it collected.

This is one reason the CC8 LIBC has to be cross-compiled: its inline
assembly is over 6&times; the size of this buffer.



#### Incompatibilities


The only known incompatibility between the compilers in the way they
handle inline assembly is that the native compiler inserts a `DECIM`
directive early in its SABR output, so all constants in inline assembly
that aren’t specifically given a radix are treated as decimal numbers:

    #asm
        TAD (42
    #endasm

That instruction adds 42 decimal to AC when compiled with the native
compiler, but it adds 34 decimal (42₈) with the cross-compiler because
the cross-compiler leaves SABR in its default octal mode!

If you want code to work with both, use the SABR-specific `D` and `K`
prefix feature on constants:

    #asm
        TAD (D42      / add 42 *decimal* to AC
    #endasm

We cannot recommend using the `DECIM` and `OCTAL` SABR pseudo-ops in
code that has to work with both compilers because there’s no way to tell
what directive to give at the end of the `#asm` block to restore prior
behavior. If you switch the mode without switching it back properly,
SABR code emitted by the compiler itself will be misinterpreted.

There’s a `DECIM` directive high up in the implementation of LIBC, but
that’s fine since it knows it will be compiled by the cross-compiler
only.



### <a id="opdef"></a>Predefined OPDEFs

In addition to the op-codes predefined for SABR — which you can find in
[Appendix C of the OS/8 Handbook, 1974 edition][os8hac] — the following
`OPDEF` directives are inserted at the top of every SABR file output





|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
|
|
<
<
<

<
<
<
<

>

<
>
>

<
<
<
<
<
<

|
>
>

|
>
|

<
<
>
>
>
>







 







|
|
<
<
<
<
<
<
>
|
>





|
|
|
|
|
|
|
|
|
|
<
<
<
<
<
<
<











|
|
>
>

<
<
|
|
<
>
|
>
>

>
>

>
>

>









>
>
>
>
>
>
>
|


<
<


|
<
>



|
|
|
|
|
|
<




|
<








|
|
|
|
<
<
<







|
|
>
|

<
|
|

|

<




|
>
>
>
|
>

<
<
<
<
|
>
>
>
>
>
>
>
>
>
|
|
|
>









>
>
>



|
>
|







<
<
<







 







|







 







|
|


|
|
|
|

|
>

<
<
<
<
<
<
|


>
>
|
|




|


|
|
>
>







 







|
|












|

|







 







<
<
<
<
|
<
>
>
>
>

|







|
<
<
<
<







 







|







 







|
|
|
|
|
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>











|
<
<







 







<
>
|
<
>
|
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
..
32
33
34
35
36
37
38
39
40
41



42




43
44
45

46
47
48






49
50
51
52
53
54
55
56
57


58
59
60
61
62
63
64
65
66
67
68
...
123
124
125
126
127
128
129
130
131






132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149







150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165


166
167

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198


199
200
201

202
203
204
205
206
207
208
209
210
211

212
213
214
215
216

217
218
219
220
221
222
223
224
225
226
227
228



229
230
231
232
233
234
235
236
237
238
239
240

241
242
243
244
245

246
247
248
249
250
251
252
253
254
255
256




257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295



296
297
298
299
300
301
302
...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370






371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
...
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
...
474
475
476
477
478
479
480




481

482
483
484
485
486
487
488
489
490
491
492
493
494
495




496
497
498
499
500
501
502
....
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
....
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488

1489
1490
1491
1492
1493
1494
1495
....
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
....
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835


1836
1837
1838
1839
1840
1841
1842
....
1854
1855
1856
1857
1858
1859
1860

1861
1862

1863
1864
1865
1866
1867

























1868
1869
1870
1871
1872
1873
1874
1875
# CC8 Manual


## A Bit of Grounding History

The PDP-8 was introduced by DEC in 1965 with the intention of being a
small and cheap processor that could be used in a variety of use cases
that were, at the time, considered low end, compared to where the rest
of the minicomputer world was at the time. It filled niches at the time
that today we’d fill with either desktop computers or embedded
processors. That makes the PDP-8 the spiritual ancestor of the iMac I’m
typing this on and of the Raspberry Pi this software is intended to run
on.

The PiDP-8/I project is part of an effort to prevent the PDP-8 from
sliding into undeserved obscurity. Whether you consider it the ancestor
of the desktop computer or the embedded processor, it is a machine worth
understanding.

The PDP-8 was roughly contemporaneous with a much more famous machine,
the PDP-11, upon which the C programming language was created. Although
a low-end PDP-11 is more powerful than even a high-end PDP-8, the fact
that their commercial lifetimes overlapped by so many years made one of
us (Ian Schofield) wonder if the PDP-8 could also support a C compiler.

The first implementation of C was on the PDP-11 as part of the early
work on the Unix operating system, and it was initially used to write
system utilities that otherwise would have been written in assembly. A C
language compiler first appeared publicly in Version 2 Unix, released
later in 1972. Much of PDP-11 Unix remained written in assembly until
its developers decided to rewrite the operating system in C, for Version
................................................................................
4 Unix, released in 1973. That decision allowed Unix to be relatively
easily ported to a wholly different platform — the Interdata 8/32 — in
1978 by writing a new code generator for the C compiler, then
cross-compiling everything. That success in porting Unix lead to C’s own
success first as a systems programming language, and then later as a
general-purpose programming language.

Although we are not likely to use CC8 to write a portable operating
system for the PDP-8, it is powerful enough to fill C’s original niche
in writing system utilities for a preexisting OS written in assembly.









## What Is CC8?


The CC8 system includes two different compilers, each of which
understands a different dialect of C:







1.  A [cross-compiler](#cross) that builds and runs on any host computer
    with a C compiler that still understands K&R C. This compiler
    understands most of K&R C itself, with the exceptions documented
    below.

2.  A [native OS/8 compiler](#native), cross-compiled on the host
    machine to PDP-8 assembly code by the cross-compiler. This compiler
    is [quite limited](#nlim) compared to the cross-compiler.



CC8 also includes [a small C library](#libc) shared by both compilers.


## CC8’s Developmental Sparks

The last high-level language compiler to be attempted for the PDP-8, as
far as this document’s authors are aware, was Pascal in 1979 by Heinz
Stegbauer.

In more recent times, Vince Slyngstad and Paolo Maffei wrote a C
cross-compiler based on Ron Cain’s Small-C using a VM approach. [This
................................................................................
independently of OS/8, but the bounds on that class of programs is not
currently clear to us.


<a id="cross" name="posix"></a>
## The Cross-Compiler

The CC8 cross-compiler is the [SmallC-85 C compiler][sc85] with a PDP-8
[SABR][sabr] code generator strapped to its back end. That means the C






language dialect understood by the CC8 cross-compiler is [K&R C
(1978)][krc] minus function pointers and the `float` and `long` data
types.

The code for this is in the `src/cc8/cross` subdirectory of the PiDP-8/I
source tree, and it is built along with the top-level PiDP-8/I software.
When installed, this compiler is in your `PATH` as `cc8`.

CC8 also includes a [small C library](#libc) in the files
`src/cc8/os8/libc.[ch]`, which is shared with the [native OS/8
compiler](#native). This library covers only a small fraction of what
the K&R C library does, in part due to system resource constraints.

Ian Schofield originally wrote the SABR code generator atop a version of
Ron Cain’s famous [Small-C compiler][sc80], originally published in [Dr
Dobb’s Journal][ddj], with later versions published elsewhere.  William
Cattey later ported this code base to SmallC-85, a living project
currently [available on GitHub][sc85].








The CC8 cross-compiler can successfully compile itself, but it produces
a SABR assembly file that is too large (28K) to be assembled on the
PDP-8.  Thus [the separate native compiler](#native).

The key module for targeting Small-C to the PDP-8 is `code8.c`. It
does the code generation to emit SABR assembly code. However, the
targeting is not confined to that one file. There is code in various
of the other modules that is specific to the PDP-8 port that should be
abstracted out and cleaned up in the fullness of time.

[Currently](/tktview?name=e1f6a5e4fe), the simplest way to get SABR
outputs from the CC8 cross-compiler into the PiDP-8/I simulator is to
use our `os8-cp` program in ASCII mode to copy SABR outputs from the
cross-compiler onto the simulator’s disk image:



    $ os8-cp -a -rk0s /opt/pidp8i/share/media/os8/v3d.rk05 \
      src/cc8/examples/ps.sb dsk:


That results in a file `DSK:PS.SB` with the POSIX LF-only line endings
translated to the CRLF line endings OS/8 wants. You can then assemble,
link, and run within the simulator, as described [below](#exes).

For related ideas, see the PiDP-8/I wiki article “[Getting Text In][gti].”

[ddj]:  https://en.wikipedia.org/wiki/Dr._Dobb%27s_Journal
[gti]:  http://localhost:8080/wiki?name=Getting+Text+In
[krc]:  https://en.wikipedia.org/wiki/The_C_Programming_Language
[sabr]: /wiki?name=A+Field+Guide+to+PDP-8+Assemblers#sabr
[sc80]: https://en.wikipedia.org/wiki/Small-C
[sc85]: https://github.com/ncb85/SmallC-85


<a id="cpp"></a>
### The Cross-Compiler’s Preprocessor Features

The cross-compiler has rudimentary C preprocessor features:

*   Literal `#define` only.  You cannot define parameterized macros.

*   There are no token pasting (`##`), stringization (`#`), or
    charization (`#@`) features, there being little point to these
    featuers of the C preprocessor without parameterized macros.

*   `#undef` removes a symbol previously defined with `#define`

*   There are no `-D` or `-U` flags to define and undefine macros from
    the command line.



*   `#include`, but only for files in the current directory.  There is
    no include path, either hard-coded within the compiler or modifiable
    via the traditional `-I` compiler flag. It is legal to nest `#include`

    statements, but the depth is currently limited to 3 levels, maximum.

*   [Inline assembly](#asm) via `#asm`.

*   `#ifdef`, `#ifndef`, `#else` and `#endif` work as expected, within
    the limitations on macros given above.

*   There is no support for `#if`, not even for simple things like `#if
    0`, much less for expressions such as `#if defined(XXX) &&
    !defined(YYY)`



### <a id="nhead"></a>Necessary Headers

There are two header files, for use with the cross-compiler only:


*   `libc.h` — Declares the entry points used by [LIBC](#libc) using
    CC8 [library linkage directives](#linkage). If your program makes
    use of any library functions, you must `#include` this at the top of
    your program.

*   `init.h` — Inserts a block of [inline assembly](#asm) startup code
    into your program, which initializes the program environment, sets
    up LIBC, and defines a few low-level routines. Unless you know this
    file’s contents and have determined that you do not need any of what
    it does for you, you probably cannot write a valid CC8 program that
    does not `#include` this header.




Because the cross-compiler lacks an include path feature, you generally
want to symlink these files to the directory where your source files
are. This is already done for the CC8 examples and such.

If you compare the examples in the source tree (`src/cc8/examples`) to
those with uppercased versions of those same names on the OS/8 `DSK:`
volume, you’ll notice that these `#include` statements were stripped out
as part of the disk pack build process. This is [necessary](#os8pp); the
linked documentation tells you why and how the OS/8 version of CC8 gets
away without a `#include` feature.


If you need to write C programs that build with both compilers, you can
convert the files like so:

    sed '/^#include/d' < my-program-cross.c > MYPROG.C



<a id="native" name="os8"></a>
## The Native OS/8 Compiler

Whereas the [CC8 cross-compiler](#cross) is basically just a PDP-8 code
generator strapped to the preexisting Small-C compiler, the native OS/8
CC8 compiler was written from scratch by Ian Schofield. It gets
cross-compiled, assembled, linked, and saved to the OS/8 disk packs as
part of the PiDP-8/I software build process. Thereafter, it is a
standalone system using only OS/8 resources.





Because this compiler must work entirely within the stringent limits of
the PDP-8 computer architecture and its OS/8 operating system, it speaks
a [much simpler dialect of C](#nfeat) than the cross-compiler, which
gets to use your host’s much greater resources.

Unlike with the original CC8 software distribution, the PiDP-8/I
software project does not ship any pre-built CC8 binaries.  Instead, we
bootstrap CC8 binaries from source code with the powerful
[`os8-run`][os8r] scripting language interpreter and the PiDP-8/I
software build system.  (You can suppress this by passing the
`--disable-os8-cc8` option to the `configure` script.) This process is
controlled by the [`cc8-tu56.os8`][cctu] script, which you may want to
examine along with the `os8-run` documentation to understand this
process better.

If you change the OS/8 CC8 source code, saying `make` at the PiDP-8/I
build root will update `bin/v3d.rk05` with new binaries automatically.

Because the CC8 native compiler is compiled by the CC8 *cross*-compiler,
the [standard memory layout](#memory) applies to both.  Among other
things, this means each pass of the native compiler requires
approximately 20&nbsp;kWords of core.

The native OS/8 CC8 compiler’s source code is in the `src/cc8/os8`
subdirectory of the PiDP-8/I software distribution.

<a id="ncpass"></a>The compiler passes are:

1.  `c8.c` &rarr; `c8.sb` &rarr; `CC.SV`: The compiler driver: accepts
    the input file name from the user, does some [rudimentary
    preprocessing](#os8pp) on it, and calls the first proper compiler
    pass, `CC1`.

2.  `n8.c` &rarr; `n8.sb` &rarr; `CC1.SV`: The parser/tokeniser section
    of the compiler.

3.  `p8.c` &rarr; `p8.sb` &rarr; `CC2.SV`: The token to SABR code
    converter section of the compiler.




There is also `libc.c` &rarr; `libc.sb` &rarr; `LIBC.RL`, the [C
library](#libc) linked to any program built with CC8, including the
passes above, but also to your own programs.

All of these binaries end up on the automatically-built OS/8 boot disk:
`CC?.SV` on `SYS:`, and everything else on `DSK:`, based on the defaults
our OS/8 distribution is configured to use when seeking out files.
................................................................................
1.  **Recursion:** See [`FIB.C`][fib] for an example of this.

1.  **Simple arithmetic operators:** `+`, `-`, `*`, `/`, etc.

1.  **Bitwise operators:** `&`, &brvbar;, `~` and `!`

1.  **Simple comparison operators:** False expressions evaluate as 0 and
    true as -1 in twos complement form, meaning all 1's in binary form.
    See the list of limitations below for the operators excluded by our
    "simple" qualifier.

1.  **A few 2-character operators:** `++`, `--` (postfix only) and `==`.

1.  **Limited library:** See [below](#libc) for a list of library
    functions provided, including their known limitations relative to
................................................................................

[fib]: /doc/trunk/src/cc8/examples/fib.c


<a id="nlim" name="limitations"></a>
### Known Limitations of the OS/8 CC8 Compiler

The OS/8 version of CC8 supports a subset of the C dialect understood by
[the cross-compiler](#cross), and thus of K&R C:

1.  <a id="typeless"></a>The language is typeless in that everything is
    a 12 bit integer, and any variable/array can interpreted as `int`,
    `char` or pointer.  All variables and arrays must be declared as
    `int`. As with K&R C, the return type may be left off of a
    function's definition; it is implicitly `int` in all cases.

    Because the types are already known, it is not necessary to give
    types when declaring function arguments:







        myfn(n) { /* do something with n */ }

    This declares a function taking an `int` called `n` and returning
    an `int`.
    
    Contrast the CC8 cross-compiler, which requires function argument
    types to be declared, if not the return type, per K&R C rules:

        myfn(n)
        int n;
        {
            /* do something with n */
        }

    The cross-compiler supports `void` as an extension to K&R C, but the
    native compiler does not, and it is not yet smart enough to flag
    code including it with an error. It will simply generate bad code
    when you try to use `void`.

2.  There must be an `int main()`, and it must be the last function
    in the single input C file.

    Since OS/8 has no way to pass command line arguments to a program
    — at least, not in a way that is compatible with the Unix style
    command lines expected by C — the `main()` function is never
................................................................................
    support for preprocessor directives.

    *   Literal `#define` only: no parameterized macros, and no `#undef`.

    *   `#include` is not supported and must not appear in the C source
        code fed to the Native OS/8 Compiler.

        This means you cannot use `#include` directives to string
        multiple C modules into a single program.

        It also means that if you take a program that the cross-compiler
        handles correctly and just copy it straight into OS/8 and try to
        compile it, it probably still has the `#include <libc.h>` line and
        possibly one for `init.h` as well. *Such code will fail to compile.*
        You must strip such lines out when copying C files into OS/8.

        (The native compiler emits startup code automatically, and it
        hard-codes the LIBC call table in the [final compiler
        pass](#ncpass), implemented in `p8.c`, so it doesn’t need
        `#include` to make these things work.)

    *   No conditional compilation: `#if`, `#ifdef`, `#else`, etc.

    *   [Inline assmembly](#asm) via `#asm`.

5.  Variables are implicitly `static`, even when local.

6.  Arrays may only be single indexed. See `PS.C` for an example.

7.  The compiler does not yet understand how to assign a variable's
    initial value as part of its declaration. This:
................................................................................
    expression to a form that is now legal in our limited C dialect. All
    of this comes from the Laws section of the linked Wikipedia article;
    if you learn nothing else about Boolean algebra, you would be well
    served to memorize those rules.

9. Dereferencing parenthesized expressions does not work: `*(<expr>)`





10. There is no argument list checking, not even for functions

    previously declared in the same C file. If we did fix this, the
    problem would still exist for functions in other modules, such as
    [`LIBC`](#libc), since K&R C doesn’t have prototypes; ANSI added
    that feature to C.

11. `do/while` loops are parsed, but the code is not properly generated.
    Regular `while` loops work, as does `break`, so one workaround for a
    lack of `do/while` is:

        while (1) { /* do something useful */; if (cond) break; }

    We have no intention to fix this.

12. `switch` doesn't work.






<a id="warning"></a>
#### GOVERNMENT HEALTH WARNING

**You are hereby warned**: The native OS/8 compiler does not contain any
error checking whatsoever. If the source files contain an error or you
................................................................................

Another set of examples not preinstalled on the OS/8 disk are
`examples/pep001-*.c`, which are described [elsewhere][pce].

[pce]: /wiki?name=PEP001.C


## <a id="exes"></a>Making Executables 

Executing `CCR.BI` loads, links, and runs your C program without
producing an executable file on disk.  You need only a small variation
on this BATCH file's contents to get an executable core image that
you can run with the OS/8 `R` command:

    .R CC                   ⇠ kinda like Unix cc(1)
................................................................................
data field](#udf). That is to say, the code generated by CC8 does not
use 15-bit extended addresses; it just flips between pages depending on
what type of data or code it’s trying to access.

This means it is possible to iterate a pointer past the end of a 4096
word core memory field, causing it to wrap around to 0 and continue
blithely along.  Since the last page of the user data field [is reserved
for use by OS/8](#os8res) and the first page of the UDF has [several
special uses](#udf), programs that do this will most likely crash and
may even destroy data. Our [LIBC implementation](#libc) generally does
not try to check for such wraparound problems, much less signal errors
when it happens. The programmer is expected to avoid doing this.


Code that operates on pointers will generally only do its work within
the user data field. You will likely need to resort to [inline
assembly](#asm) and `CIF`/`CDF` instructions to escape that field.
Getting our [LIBC](#libc) to operate on other fields may be tricky or
even more difficult than it’s worth.

................................................................................
There is no `malloc()` in this C library and no space reserved for its
heap in [the user data field](#udf). Everything in a CC8 program is
statically-allocated, if you’re using stock C-level mechanisms. If your
program needs additional dynamically-allocated memory, you’ll need to
arrange access to it some other way, such as [via inline
assembly](#asm).


#### Fun Trivia: The History of `malloc()`

There is no “`malloc()`” in K&R C, either, at least as far as the first
edition of “[The C Programming Language”][krc] goes. About halfway into
the book they give a simple function called `alloc()` that just
determined whether the requested amount of space was available within a
large static `char[]` array it managed for its callers. If so, it
advanced the pointer that much farther into the buffer and returned that
pointer. The corresponding `free()` implementation just chopped the
globally-allocated space off again, so if you called that `alloc()`
twice and freed the first pointer, the second would be invalid, too!

Then in Appendix A, Kernighan & Ritchie give a much smarter alternative
based on the old Unix syscall [`sbrk(2)`][sbrk]. The impression given is
that memory allocation isn’t part of the language, it’s part of the
operating system, and different implementations of C were expected to
provide this facility in local ways.

[V6 UNIX][v6ux] preceded K&R C by several years, and there is no
`malloc()` there, either. There’s an `alloc()` implementation in its
`libc` that’s scarcely more complicated than the `char[]` based one
first presented in K&R. There is no `free()` in V6: new allocations just
keep extending the amount of core requested.

`malloc()` apparently first appeared about a year after K&R was
published, in [V7 UNIX][v7ux]. It and its corresponding `free()` call
are based on similar techniques to the `sbrk()`-based `alloc()` and
`free()` published in K&R Appendix A, though clearly with quite a lot of
evolution between the two.

[sbrk]: https://pubs.opengroup.org/onlinepubs/7908799/xsh/brk.html
[v6ux]: https://en.wikipedia.org/wiki/Version_6_Unix
[v7ux]: https://en.wikipedia.org/wiki/Version_7_Unix


### <a id="vonn"></a>There Are No Storage Type Distinctions

It may surprise you to learn that literals are placed in the same field
as globals and the call stack.

Other C compilers place literals in among the executable code instead, a
fact that’s especially helpful on [Harvard architecture
microcontrollers][harch] with limited RAM. We don’t do it that way in
CC8 because literals are implemented in terms of the SABR `COMMN`
feature, which in turn is how OS/8 FORTRAN II implements `COMMON`. These
subsystems have no concept of “storage type” as in modern C compilers.


### <a id="sover"></a>Stack Overflow

Since CC8 places the call stack immediately after the last literal
stored in core, a program with many globals and/or literals will have
less usable stack space than a program with fewer of each.

Neither version of CC8 generates code to detect stack overflow. If you
try to push too much onto the stack, it will simply begin overwriting
the page OS/8 is using at the top of field 1. If you manage to blow the
stack by more than a page without crashing the program or the computer
first, the [stack pointer will wrap around](#ptrwrap) and the stack will
begin overwriting the first page of field 1.

[harch]: https://en.wikipedia.org/wiki/Harvard_architecture


### <a id="flayout"></a>Field Layout, Concrete Example

The field layout given [at the start of this section](#memory) is not
fixed. The linking loader is free to use any layout it likes, consistent
with any constraints in the input binaries. You can use the `/M` option
with `LOADER.SV` to get a core memory map for a given output. Let’s work
................................................................................
That example is based on real code, the implementation of
[`itoa()`](#itoa) for radices beyond 10: we tried it both ways and ended
up doing it the obscure way to save code space in LIBC.

For the most part, CC8 currently leaves the task of optimization to the
end user.


### <a id="asmoct"></a>Inline Assembly is in Octal

Like the OS/8 FORTRAN II compiler, the CC8 compilers leave SABR in its
default octal mode. All integer constants emited by both compilers are
in octal.  (Even those in generated labels and in error output
messages!) This means integer constants in your inline assembly also get
interpreted as octal, by default.

If you use the `DECIM` SABR pseudo-op to get around this, you must be
careful to add an `OCTAL` op before the block ends to shift the mode
back. The compiler doesn’t detect use of `DECIM`, and it doesn’t blindly
inject `OCTAL` ops after every inline assembly block to force the mode
back on the off chance that the user had shifted the assembler into
decimal mode. If you leave the assembler in `DECIM` mode at the end of
an inline assembly block, the resulting SABR output will probably
assemble but won’t run correctly because all integer constants from that
point on will be misinterpreted.

It’s safer, if you wan a given constant to be interpreted as decimal, to
prefix it with a `D`. See the SABR manual for more details on this.


### <a id="linkage" name="varargs"></a>Library Linkage and Varargs

CC8 has some non-standard features to enable the interface between the
main program and the C library. This constitutes a compile time linkage
system to allow for standard and vararg functions to be called in the
library.

**TODO:** Explain this.


### <a id="os8asm"></a>Inline Assembly Limitations in the Native CC8 Compiler



The native compiler has some significant limitations in the way it
handles inline assembly.

The primary one is that snippets of inline assembly are gathered by the
[first pass](#ncpass) of the compiler in a core memory buffer that’s
only 1024 characters in size. If the total amount of inline assembly in
................................................................................
[`fopen()`](#fopen) is limited to a [single output file at a
time](#fiolim) and it cannot append to an existing file, it’s got one
shot to write everything it collected.

This is one reason the CC8 LIBC has to be cross-compiled: its inline
assembly is over 6&times; the size of this buffer.


Another problem to watch out for is that this inline assembly buffer is
broken into sections with `!` and `$` characters so that the final pass

of the compiler can break the `CASM.TX` file up into sections for
insertion into the SABR output. It is therefore unsafe to use these
characters in your inline assembly, lest they be seen as separators,
causing incorrect output.  This is especially easy to do in comments;
watch out! (See how easy it is to use an exclamation point when making

























comments?)


### <a id="opdef"></a>Predefined OPDEFs

In addition to the op-codes predefined for SABR — which you can find in
[Appendix C of the OS/8 Handbook, 1974 edition][os8hac] — the following
`OPDEF` directives are inserted at the top of every SABR file output

Changes to src/cc8/cross/code8.c.

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
...
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
...
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
...
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
...
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
....
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
....
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
....
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
//	output_with_tab (scptr[OFFSET] == FUNCTION ? "public\t" : "extrn\t");
//	prefix ();
//	output_string (scptr);
//	newline (); */
}

/*
 *  Output a decimal number to the assembler file
 */
void output_number (num) int num; {
	output_decimal (num);	/* pdp11 needs a "." here */
}


/*
 *	fetch a static memory cell into the primary register
getmem (sym)
char	*sym;
................................................................................
	if (sym->storage == LSTATIC) {
		gen_immediate3 ();
		print_label(-1-glint(sym));
		newline ();
	} else {
		if (stkp-glint(sym)==0) output_string ("/");
		gen_immediate3 ();
		output_decimal (stkp-glint(sym));
		newline ();
	}
    return 0;
}

/*
 *	store the primary register into the specified static memory cell
................................................................................
int		*nargs;
{
	char tm[10];

	if (strstr(sname,"vlibc")) {
	gen_immediate();
	sname++;
	output_decimal (*nargs);
	output_string ("\t/ PUSH ARG COUNT");
	newline ();
	output_line("\tJMSI PSH");
	stkp = stkp - INTSIZE;
	(*nargs)++;
	}
	if (strstr(sname,"libc"))
................................................................................
		return (newstkp);
	if (k>0 && k<5) {
		while (k--) output_line ("\tISZ STKP");
		return (newstkp);
	}
	output_line ("\tMQL");
	gen_immediate3 ();
	output_decimal (k);
	newline ();
	output_line ("\tTAD STKP");
	output_line ("\tDCA STKP");
	gen_swap ();
	return (newstkp);
}

................................................................................
void gen_less_than ()
{
        gen_pop();
	output_line("\tCIA");
	output_line("\tDCA JLC");
	output_line("\tACL");
	output_line("\tTAD JLC");
	output_line("\tAND (2048");
}

/*
 *	less than or equal (signed)
 *
 */
void gen_less_or_equal ()
................................................................................
        gen_pop();
	output_line("\tCIA");
	output_line("\tDCA JLC");
	output_line("\tACL");
	output_line("\tTAD JLC");
	output_line("\tSNA");
	output_line("\tCLA CMA");
	output_line("\tAND (2048");
}

/*
 *	greater than (signed)
 *
 */
void gen_greater_than ()
................................................................................
{
	gen_pop();
	output_line("\tSWP");
	output_line("\tCIA");
	output_line("\tDCA JLC");
	output_line("\tACL");
	output_line("\tTAD JLC");
	output_line("\tAND (2048");
}

/*
 *	greater than or equal (signed)
 *
 */
void gen_greater_or_equal ()
................................................................................
	output_line("\tSWP");
	output_line("\tCIA");
	output_line("\tDCA JLC");
	output_line("\tACL");
	output_line("\tTAD JLC");
	output_line("\tSNA");
	output_line("\tCLA CMA");
	output_line("\tAND (2048");
}

/*
 *	less than (unsigned)
 *
 */
void gen_unsigned_less_than ()







|


|







 







|







 







|







 







|







 







|







 







|







 







|







 







|







240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
...
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
...
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
...
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
...
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
....
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
....
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
....
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
//	output_with_tab (scptr[OFFSET] == FUNCTION ? "public\t" : "extrn\t");
//	prefix ();
//	output_string (scptr);
//	newline (); */
}

/*
 *  Output an octal number to the assembler file
 */
void output_number (num) int num; {
	output_octal (num);
}


/*
 *	fetch a static memory cell into the primary register
getmem (sym)
char	*sym;
................................................................................
	if (sym->storage == LSTATIC) {
		gen_immediate3 ();
		print_label(-1-glint(sym));
		newline ();
	} else {
		if (stkp-glint(sym)==0) output_string ("/");
		gen_immediate3 ();
		output_octal (stkp-glint(sym));
		newline ();
	}
    return 0;
}

/*
 *	store the primary register into the specified static memory cell
................................................................................
int		*nargs;
{
	char tm[10];

	if (strstr(sname,"vlibc")) {
	gen_immediate();
	sname++;
	output_octal (*nargs);
	output_string ("\t/ PUSH ARG COUNT");
	newline ();
	output_line("\tJMSI PSH");
	stkp = stkp - INTSIZE;
	(*nargs)++;
	}
	if (strstr(sname,"libc"))
................................................................................
		return (newstkp);
	if (k>0 && k<5) {
		while (k--) output_line ("\tISZ STKP");
		return (newstkp);
	}
	output_line ("\tMQL");
	gen_immediate3 ();
	output_octal (k);
	newline ();
	output_line ("\tTAD STKP");
	output_line ("\tDCA STKP");
	gen_swap ();
	return (newstkp);
}

................................................................................
void gen_less_than ()
{
        gen_pop();
	output_line("\tCIA");
	output_line("\tDCA JLC");
	output_line("\tACL");
	output_line("\tTAD JLC");
	output_line("\tAND (4000");
}

/*
 *	less than or equal (signed)
 *
 */
void gen_less_or_equal ()
................................................................................
        gen_pop();
	output_line("\tCIA");
	output_line("\tDCA JLC");
	output_line("\tACL");
	output_line("\tTAD JLC");
	output_line("\tSNA");
	output_line("\tCLA CMA");
	output_line("\tAND (4000");
}

/*
 *	greater than (signed)
 *
 */
void gen_greater_than ()
................................................................................
{
	gen_pop();
	output_line("\tSWP");
	output_line("\tCIA");
	output_line("\tDCA JLC");
	output_line("\tACL");
	output_line("\tTAD JLC");
	output_line("\tAND (4000");
}

/*
 *	greater than or equal (signed)
 *
 */
void gen_greater_or_equal ()
................................................................................
	output_line("\tSWP");
	output_line("\tCIA");
	output_line("\tDCA JLC");
	output_line("\tACL");
	output_line("\tTAD JLC");
	output_line("\tSNA");
	output_line("\tCLA CMA");
	output_line("\tAND (4000");
}

/*
 *	less than (unsigned)
 *
 */
void gen_unsigned_less_than ()

Changes to src/cc8/cross/extern.h.

538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
 * tabbed output
 * @param ptr
 * @return 
 */
void output_with_tab();

/**
 * output decimal number
 * @param number
 * @return 
 */
void output_decimal();

/**
 * stores values into memory
 * @param lval
 * @return 
 */
void store(LVALUE *lval);







|



|







538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
 * tabbed output
 * @param ptr
 * @return 
 */
void output_with_tab();

/**
 * output octal number
 * @param number
 * @return 
 */
void output_octal();

/**
 * stores values into memory
 * @param lval
 * @return 
 */
void store(LVALUE *lval);

Changes to src/cc8/cross/gen.c.

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

/**
 * print specified number as label
 * @param label
 */
void print_label(int label) {
    output_label_prefix ();
    output_decimal (label);
}

/**
 * glabel - generate label
 * not used ?
 * @param lab label number
 */
................................................................................
 */
void output_with_tab(char ptr[]) {
    // print_tab ();
    output_string (ptr);
}

/**
 * output decimal number
 * @param number
 * @return 
 */
void output_decimal(int number) {
	int	k, zs;
	char	c;

	if (number == -32768) {
		output_string ("-32768");
		return;
	}
	zs = 0;
	k = 10000;
	if (number < 0) {
		number = (-number);
		output_byte ('-');
	}
	while (k >= 1) {
		c = number / k + '0';
		if ((c != '0' || (k == 1) || zs)) {
			zs = 1;
			output_byte (c);
		}
		number = number % k;
		k = k / 10;
	}
    return;
}

/**
 * stores values into memory
 * @param lval







|







 







|



|



<
<
<
<

|











|







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117




118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

/**
 * print specified number as label
 * @param label
 */
void print_label(int label) {
    output_label_prefix ();
    output_octal (label);
}

/**
 * glabel - generate label
 * not used ?
 * @param lab label number
 */
................................................................................
 */
void output_with_tab(char ptr[]) {
    // print_tab ();
    output_string (ptr);
}

/**
 * output octal number
 * @param number
 * @return 
 */
void output_octal(int number) {
	int	k, zs;
	char	c;





	zs = 0;
	k = 01000;
	if (number < 0) {
		number = (-number);
		output_byte ('-');
	}
	while (k >= 1) {
		c = number / k + '0';
		if ((c != '0' || (k == 1) || zs)) {
			zs = 1;
			output_byte (c);
		}
		number = number % k;
		k = k / 8;
	}
    return;
}

/**
 * stores values into memory
 * @param lval

Changes to src/cc8/cross/main.c.

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
 * report errors
 */
void errorsummary() {
    if (ncmp)
        error("missing closing bracket");
    newline();
    gen_comment();
    output_decimal(errcnt);
    if (errcnt) errfile = YES;
    output_string(" error(s) in compilation");
    newline();
    gen_comment();
    output_with_tab("literal pool:");
    output_decimal(litptr);
    newline();
    gen_comment();
    output_with_tab("global pool:");
    output_decimal(global_table_index);
    newline();
    gen_comment();
    output_with_tab("Macro pool:");
    output_decimal(macptr);
    newline();
    if (errcnt > 0)
        pl("Error(s)");
}

/**
 * test for C or similar filename, e.g. xxxxx.x, tests the dot at end-1 postion







|





|



|



|







505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
 * report errors
 */
void errorsummary() {
    if (ncmp)
        error("missing closing bracket");
    newline();
    gen_comment();
    output_octal(errcnt);
    if (errcnt) errfile = YES;
    output_string(" error(s) in compilation");
    newline();
    gen_comment();
    output_with_tab("literal pool:");
    output_octal(litptr);
    newline();
    gen_comment();
    output_with_tab("global pool:");
    output_octal(global_table_index);
    newline();
    gen_comment();
    output_with_tab("Macro pool:");
    output_octal(macptr);
    newline();
    if (errcnt > 0)
        pl("Error(s)");
}

/**
 * test for C or similar filename, e.g. xxxxx.x, tests the dot at end-1 postion

Changes to src/cc8/include/init.h.

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
..
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
ABSYM PTSK 164
ABSYM POPR 165
ABSYM PCAL 166
ABSYM TMP 167
ABSYM GBL 170
ABSYM ZTMP 171


/
        DECIM
/
STK,    COMMN 3840
/
/
/
        ENTRY MAIN
MAIN,   BLOCK 2
        TAD GBLS
        DCA STKP
................................................................................
        TAD SRI
        DCA PSTRI
        TAD SRD
        DCA PSTRD
        TAD SRL
        DCA PSTRL
        RIF
        TAD (3201
        DCA PCL1
        TAD PCL1
        DCA DCC0
        JMS MCC0
        CLA CMA
        MQL
        CALL 1,LIBC







|

<
<
|







 







|







41
42
43
44
45
46
47
48
49


50
51
52
53
54
55
56
57
..
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
ABSYM PTSK 164
ABSYM POPR 165
ABSYM PCAL 166
ABSYM TMP 167
ABSYM GBL 170
ABSYM ZTMP 171

/
/


STK,    COMMN 7400
/
/
/
        ENTRY MAIN
MAIN,   BLOCK 2
        TAD GBLS
        DCA STKP
................................................................................
        TAD SRI
        DCA PSTRI
        TAD SRD
        DCA PSTRD
        TAD SRL
        DCA PSTRL
        RIF
        TAD (6201           / BUILD CDF + IF INSTR
        DCA PCL1
        TAD PCL1
        DCA DCC0
        JMS MCC0
        CLA CMA
        MQL
        CALL 1,LIBC

Changes to src/cc8/include/libc.h.

18
19
20
21
22
23
24
25
26











27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60




/*
 * Libc header
 *
 * Please note: no function declarations are made, so make sure the
 * arg lists are correct in the calling code.
 * 
*/












#define itoa libc0
#define	puts libc1
#define	dispxy libc2
#define	getc libc3
#define	gets libc4
#define	atoi libc5
#define	sscanf vlibc6
#define	xinit libc7
#define	memcpy libc8
#define	kbhit libc9
#define	putc libc10
#define	strcpy libc11
#define	strcat libc12
#define	strstr libc13
#define	exit libc14
#define	isnum libc15
#define isdigit libc15
#define	isalpha libc16
#define	sprintf vlibc17
#define	memset libc18
#define	fgetc libc19
#define	fopen libc20
#define fputc libc21
#define fclose libc22
#define printf vlibc23
#define isalnum libc24
#define isspace libc25
#define fprintf vlibc26
#define	fputs libc27
#define strcmp libc28
#define cupper libc29
#define fgets libc30
#define revcpy libc31
#define toupper libc32










|

>
>
>
>
>
>
>
>
>
>
>








|
|
|
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

/*
 * Libc header
 *
 * Please note: no function declarations are made, so make sure the
 * arg lists are correct in the calling code.
 * 
 */

/*
 * WARNING: The following values are indices into the function call
 * table declared in libc.c.  (Search for "CLIST".)  These two lists
 * must match.
 *
 * The indices are in octal, and it is *critical* that they be be so,
 * because these values are treated as text by the compiler, not as C
 * integers.  The compiler emits these numeric strings in the generated
 * SABR code to call through the CLIST table, so since SABR runs in
 * octal mode under CC8, the following indices also have to be in octal.
 */
#define itoa libc0
#define	puts libc1
#define	dispxy libc2
#define	getc libc3
#define	gets libc4
#define	atoi libc5
#define	sscanf vlibc6
#define	xinit libc7
#define	memcpy libc10
#define	kbhit libc11
#define	putc libc12
#define	strcpy libc13
#define	strcat libc14
#define	strstr libc15
#define	exit libc16
#define	isnum libc17

#define	isalpha libc20
#define	sprintf vlibc21
#define	memset libc22
#define	fgetc libc23
#define	fopen libc24
#define fputc libc25
#define fclose libc26
#define printf vlibc27
#define isalnum libc30
#define isspace libc31
#define fprintf vlibc32
#define	fputs libc33
#define strcmp libc34
#define cupper libc35
#define fgets libc36
#define revcpy libc37
#define toupper libc40

/* Declare function aliases. */ 
#define isdigit isnum

Changes to src/cc8/os8/header.sb.

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
ABSYM PTSK 164
ABSYM POPR 165
ABSYM PCAL 166
ABSYM TMP 167
ABSYM GBL 170
ABSYM ZTMP 171
/
    DECIM
/
STK,    COMMN 3840
/
/
/
        ENTRY MAIN
MAIN,   BLOCK 2
        TAD GBLS
        DCA STKP
................................................................................
        TAD MVL
        DCA PTSK
        TAD PVR
        DCA POPR
        TAD PVC
        DCA PCAL
        RIF
        TAD (3201
        DCA PCL1
        TAD PCL1
        DCA DCC0
        JMS MCC0
        CLA CMA
        MQL
        CALL 1,LIBC







<

|







 







|







48
49
50
51
52
53
54

55
56
57
58
59
60
61
62
63
..
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
ABSYM PTSK 164
ABSYM POPR 165
ABSYM PCAL 166
ABSYM TMP 167
ABSYM GBL 170
ABSYM ZTMP 171
/

/
STK,    COMMN 7400
/
/
/
        ENTRY MAIN
MAIN,   BLOCK 2
        TAD GBLS
        DCA STKP
................................................................................
        TAD MVL
        DCA PTSK
        TAD PVR
        DCA POPR
        TAD PVC
        DCA PCAL
        RIF
        TAD (6201           / BUILD CDF + IF INSTR
        DCA PCL1
        TAD PCL1
        DCA DCC0
        JMS MCC0
        CLA CMA
        MQL
        CALL 1,LIBC

Changes to src/cc8/os8/libc.c.

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
..
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
...
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
...
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
...
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
...
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
...
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
...
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
...
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
....
1027
1028
1029
1030
1031
1032
1033
1034
1035
ABSYM TMP 156
ABSYM GBL 157
ABSYM ZTMP 146
ABSYM ZPTR 145
ABSYM ZCTR 144
ABSYM FPTR 160
/
	DECIM
/
/
/
/
		DUMMY ARGST
		DUMMY ARGNM
ARGST,	BLOCK 2
................................................................................
		TAD MVL
		DCA PTSK
		TAD PVR
		DCA POPR
		TAD PVC
		DCA PCAL
		RIF
		TAD (3201
		DCA PCL1
		TAD PCL1
		DCA DCC0
		JMS MCC0
		TAD STKP
		DCA I ARGST		/ UPDATE MASTER STKP
		DCA ZPTR		/ INIT PRINTF FLAG
		DCA FPTR		/ INIT FPRINTF FLAG
................................................................................
PVL,	PUSH
OVL,	PPOP
MVL,	PUTSTK
PVR,	POPRET
PVC,	PCALL
/
CPNT,	CLIST
		CPAGE 33        / # OF ENTRIES IN CLIST BELOW
/
/		THIS IS THE DISPATCH LIST FOR THIS LIBRARY
/		MAKE SURE LIBC.H MATCHES
/
CLIST,	ITOA
		PUTS
		DISPXY
................................................................................
#asm

	CLA CLL
	CALL 2,CHRIO
	ARG (-4
	ARG FRSL
	TAD FRSL
	TAD (-26		/^Z
	SNA CLA
	DCA FRSL
	TAD FRSL
	CDF1
	JMPI POPR
FRSL,BLOCK 2

................................................................................

IDEV,	0
ICHAR,	0
ADDR,	0

RCHAR,	CIA		/READ A CHAR.
	JMS SETDEV
	1024		/SET BIT FOR READ. (8 UNITS NOW!)
	JMS GETP
	CLA
	TAD CDFB
	DCA CDFCH
	JMS CHSUB
CDFCH,	HLT
	AND (127	/ 7 BIT FOR NOW
	DCAI ADDR
XIT,	CLA
	RETRN CHRIO

SETDEV,	0
	TAD (-1
	AND (7
................................................................................
	TAD I SETDEV
	INC SETDEV
	DCA IDEV
	JMP I SETDEV

CHSUB,	0
	TAD ICHAR
	AND (255
	TAD IDEV
	CALL 0,GENIO
	JMP I CHSUB

GETP,	0
	TAD CHRIO
	DCA CDFA
................................................................................
#endasm
}
		sixbit(p)
char *p;
{
	*p++;
#asm
		AND (63
		BSW
		MQL
#endasm
	*p;
#asm
		AND (63
		MQA
#endasm
}

fputs(p)
int *p;
{
................................................................................
		DCA FBSE#
FC3,	CDF1
#endasm
	*p++=0;
	sixbit(p);
#asm
		PAGE
		/ OFFSET IOPEN+81 = FILEEX
		DCA ZTMP
		TAD FC2#		/ CODE
		AND (63
		TAD (128
		DCA FDCT
		CDF0
		TADI FDCT
		DCA FEX1
		TAD FDCT
		TAD (64
		DCA FDCT
		TADI FDCT
		TAD (81			/ OFFSET OF EXTENSION
		DCA FDCT
FEX1,	HLT
		TAD ZTMP
		DCAI FDCT
		CDF1
#endasm
	fnm;
................................................................................
		DCA FDCT
		TAD FDCA
		DCA FP4
FP1,	CAM
		TADI ZTMP
		SNA
		JMP FP2
		AND (63
		BSW
		MQL
		ISZ ZTMP
FP2,	TADI ZTMP	/ WILL USE STACK FIELD
		AND (63
		SZA
		ISZ ZTMP
		MQA
FP4,	DCA FFNM
		ISZ FP4
		ISZ FDCT
		JMP FP1
		TAD (46
		DCAI ZTMP	/ PUT . BACK INTO FNM
		CLA CLL CMA
		TAD STKP
		DCA STKP
FBSE,	CALL 2,IOPEN
		ARG FDEV
		ARG FFNM
................................................................................
getc()
{
#asm
	 CLA CLL
GT1, KSF
	 JMP GT1
	 KRB
	 TAD (-254
	 CLA
	 KRB
	 SNL			/ DO NOT ECHO BS
	 TLS
	 TAD (-131		/ ? ^C
	 SNA CLA
	 JMP OSRET
	 KRB
	 AND (127		/ 7 BITS!
#endasm
}

gets(p)
char *p;
{
int q,tm;
		tm=1;
		q=p;
		while (tm) {
		getc();
#asm
		AND (127
		TAD (-13	/ CR IS END OF STRING -> 0
		SZA
		TAD (13
	    DCAI STKP
#endasm
		if (tm-127)	/* Handle BS */
		  *p++=tm;
		else
			if (p-q) {
		   puts("\b \b");
................................................................................
atoi(p,rsl)
char *p;
int *rsl;
{
#asm
	DCA ZTMP
	DCA ZCTR
	TAD (3584		/ NOP
	DCA XINV
	CDF1			/ Change DF back to 1 in case SABR changes it!
#endasm
	while (*p==' ')
	 p++;
	if (*p=='-') {
#asm
	CLA
	TAD (3617
	DCA XINV		/ CIA
	CDF1
#endasm
	p++;
	}
	while (*p++) {
#asm
	TAD (-48		/ '0' ... SEE CODE
	DCA JLC
	TAD JLC
	SPA CLA
	JMP XRET
	TAD (-10
	TAD JLC
	SMA CLA
	JMP XRET		/ EXIT IF NOT NUMBER
	TAD ZTMP
	CLL RTL			/ *4
	TAD ZTMP		/ *5
	CLL RAL			/ *10
................................................................................
memcpy(dst,src,cnt)
int dst,src,cnt;
{
#asm
	CLA
	TAD STKP
	TAD (-4
	DCA 12
	CMA
	TADI 12
	DCA 11
	CMA
	TADI 12
	DCA 10
	TADI 12
	CIA
	DCA ZTMP
CP1,	TADI 10
		DCAI 11
		ISZ ZTMP
		JMP CP1
#endasm

}

kbhit()
................................................................................
}

isnum(vl)
int vl;
{
		vl;
#asm
		TAD (-48
		SPA
		JMP XNO
		TAD (-10
		SMA CLA
XNO,	CLA SKP
		IAC
#endasm
}

isspace(vl)
int vl;
{
		vl;
#asm
		SNA
		JMP YNO
		TAD (-33
		SMA CLA
YNO,	CLA SKP
		IAC
#endasm
}


isalpha(vl)
int vl;
{
		vl;				/* Include '?' and '@' as alpha vars */
#asm
		TAD (-65
		SPA
		JMP ANO
		TAD (-26
		SPA
		JMP BNO
		TAD (-6
		SPA
		JMP ANO
		TAD (-26
BNO,	SMA CLA
ANO,	CLA SKP
		IAC
#endasm
}

cupper(p)				/* In place convert to uppercase */
................................................................................
	p;
#asm
		DCA ZTMP
CPP1,	CLA
		TADI ZTMP
		SNA
		JMP CPP2
		TAD (-97
		SPA
		JMP CPP3
		TAD (-26
		SMA
		JMP CPP3
		TAD (91
		DCAI ZTMP
CPP3,	ISZ ZTMP
		JMP CPP1
CPP2,
#endasm
}

................................................................................
toupper(p)
int p;
{
	p;
#asm
		DCA ZTMP
		TAD ZTMP
		TAD (-97
		SPA
		JMP TPP3
		TAD (-26
		SMA
		JMP TPP3
		TAD (91
		JMP TPP2
TPP3,	CLA CLL
		TAD ZTMP
TPP2,
#endasm
}

................................................................................
int *dst,*src,cnt;
{
	dst+=cnt;
	src+=cnt;
	while (cnt--)
		*dst--=*src--;
}









<







 







|
|







 







|







 







|







 







|






|







 







|







 







|





|







 







<


|
|





|


|







 







|




|







|







 







|




|



|












|
|

|







 







|








|
|






|




|







 







|

|
|

|
|
|


|
|







 







|


|













|












|


|


|


|







 







|


|


|







 







|


|


|







 







<
<
45
46
47
48
49
50
51

52
53
54
55
56
57
58
..
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
...
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
...
370
371
372
373
374
375
376

377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
...
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
...
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
...
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
...
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
...
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
...
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
...
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
....
1025
1026
1027
1028
1029
1030
1031


ABSYM TMP 156
ABSYM GBL 157
ABSYM ZTMP 146
ABSYM ZPTR 145
ABSYM ZCTR 144
ABSYM FPTR 160
/

/
/
/
/
		DUMMY ARGST
		DUMMY ARGNM
ARGST,	BLOCK 2
................................................................................
		TAD MVL
		DCA PTSK
		TAD PVR
		DCA POPR
		TAD PVC
		DCA PCAL
		RIF
		TAD (6201		/ BUILD CDF + IF INSTR...
		DCA PCL1		/ ...AND SAVE AS FIRST OF PCL1 SUBROUTINE
		TAD PCL1
		DCA DCC0
		JMS MCC0
		TAD STKP
		DCA I ARGST		/ UPDATE MASTER STKP
		DCA ZPTR		/ INIT PRINTF FLAG
		DCA FPTR		/ INIT FPRINTF FLAG
................................................................................
PVL,	PUSH
OVL,	PPOP
MVL,	PUTSTK
PVR,	POPRET
PVC,	PCALL
/
CPNT,	CLIST
		CPAGE 41        / # OF ENTRIES IN CLIST BELOW, IN OCTAL
/
/		THIS IS THE DISPATCH LIST FOR THIS LIBRARY
/		MAKE SURE LIBC.H MATCHES
/
CLIST,	ITOA
		PUTS
		DISPXY
................................................................................
#asm

	CLA CLL
	CALL 2,CHRIO
	ARG (-4
	ARG FRSL
	TAD FRSL
	TAD (D-26		/^Z
	SNA CLA
	DCA FRSL
	TAD FRSL
	CDF1
	JMPI POPR
FRSL,BLOCK 2

................................................................................

IDEV,	0
ICHAR,	0
ADDR,	0

RCHAR,	CIA		/READ A CHAR.
	JMS SETDEV
	2000		/SET BIT FOR READ. (8 UNITS NOW!)
	JMS GETP
	CLA
	TAD CDFB
	DCA CDFCH
	JMS CHSUB
CDFCH,	HLT
	AND (177	/ 7 BIT FOR NOW
	DCAI ADDR
XIT,	CLA
	RETRN CHRIO

SETDEV,	0
	TAD (-1
	AND (7
................................................................................
	TAD I SETDEV
	INC SETDEV
	DCA IDEV
	JMP I SETDEV

CHSUB,	0
	TAD ICHAR
	AND (377	/ DEAL IN 8 BIT CHARS, MAX
	TAD IDEV
	CALL 0,GENIO
	JMP I CHSUB

GETP,	0
	TAD CHRIO
	DCA CDFA
................................................................................
#endasm
}
		sixbit(p)
char *p;
{
	*p++;
#asm
		AND (77		/ MASK OFF LOWER 6 BITS
		BSW
		MQL
#endasm
	*p;
#asm
		AND (77
		MQA
#endasm
}

fputs(p)
int *p;
{
................................................................................
		DCA FBSE#
FC3,	CDF1
#endasm
	*p++=0;
	sixbit(p);
#asm
		PAGE

		DCA ZTMP
		TAD FC2#		/ CODE
		AND (77
		TAD (200
		DCA FDCT
		CDF0
		TADI FDCT
		DCA FEX1
		TAD FDCT
		TAD (100
		DCA FDCT
		TADI FDCT
		TAD (121		/ OFFSET OF EXTENSION (FILEEX) IN IOPEN CODE
		DCA FDCT
FEX1,	HLT
		TAD ZTMP
		DCAI FDCT
		CDF1
#endasm
	fnm;
................................................................................
		DCA FDCT
		TAD FDCA
		DCA FP4
FP1,	CAM
		TADI ZTMP
		SNA
		JMP FP2
		AND (77		/ MASK OFF LOWER 7 BITS
		BSW
		MQL
		ISZ ZTMP
FP2,	TADI ZTMP	/ WILL USE STACK FIELD
		AND (77
		SZA
		ISZ ZTMP
		MQA
FP4,	DCA FFNM
		ISZ FP4
		ISZ FDCT
		JMP FP1
		TAD (56     / ASCII '.'
		DCAI ZTMP	/ PUT . BACK INTO FNM
		CLA CLL CMA
		TAD STKP
		DCA STKP
FBSE,	CALL 2,IOPEN
		ARG FDEV
		ARG FFNM
................................................................................
getc()
{
#asm
	 CLA CLL
GT1, KSF
	 JMP GT1
	 KRB
	 TAD (D-254
	 CLA
	 KRB
	 SNL			/ DO NOT ECHO BS
	 TLS
	 TAD (D-131		/ ? ^C
	 SNA CLA
	 JMP OSRET
	 KRB
	 AND (177		/ 7 BITS!
#endasm
}

gets(p)
char *p;
{
int q,tm;
		tm=1;
		q=p;
		while (tm) {
		getc();
#asm
		AND (177
		TAD (D-13	/ CR IS END OF STRING -> 0
		SZA
		TAD (D13
	    DCAI STKP
#endasm
		if (tm-127)	/* Handle BS */
		  *p++=tm;
		else
			if (p-q) {
		   puts("\b \b");
................................................................................
atoi(p,rsl)
char *p;
int *rsl;
{
#asm
	DCA ZTMP
	DCA ZCTR
	TAD (7000		/ NOP
	DCA XINV
	CDF1			/ Change DF back to 1 in case SABR changes it!
#endasm
	while (*p==' ')
	 p++;
	if (*p=='-') {
#asm
	CLA
	TAD (7041       / CIA
	DCA XINV
	CDF1
#endasm
	p++;
	}
	while (*p++) {
#asm
	TAD (D-48		/ ASCII '0'
	DCA JLC
	TAD JLC
	SPA CLA
	JMP XRET
	TAD (D-10       / # OF DECIMAL DIGITS
	TAD JLC
	SMA CLA
	JMP XRET		/ EXIT IF NOT NUMBER
	TAD ZTMP
	CLL RTL			/ *4
	TAD ZTMP		/ *5
	CLL RAL			/ *10
................................................................................
memcpy(dst,src,cnt)
int dst,src,cnt;
{
#asm
	CLA
	TAD STKP
	TAD (-4
	DCA 14
	CMA
	TADI 14
	DCA 13
	CMA
	TADI 14
	DCA 12
	TADI 14
	CIA
	DCA ZTMP
CP1,	TADI 12
		DCAI 13
		ISZ ZTMP
		JMP CP1
#endasm

}

kbhit()
................................................................................
}

isnum(vl)
int vl;
{
		vl;
#asm
		TAD (D-48		/ ASCII '0'
		SPA
		JMP XNO
		TAD (D-10		/ # OF DECIMAL DIGITS
		SMA CLA
XNO,	CLA SKP
		IAC
#endasm
}

isspace(vl)
int vl;
{
		vl;
#asm
		SNA
		JMP YNO
		TAD (D-33		/ ONE PAST ASCII ' '
		SMA CLA
YNO,	CLA SKP
		IAC
#endasm
}


isalpha(vl)
int vl;
{
		vl;				/* Include '?' and '@' as alpha vars */
#asm
		TAD (D-65		/ ASCII 'A'
		SPA
		JMP ANO
		TAD (D-26		/ # OF UPPERCASE ENGLISH LETTERS
		SPA
		JMP BNO
		TAD (D-6		/ 'a' - 'Z' IN ASCII
		SPA
		JMP ANO
		TAD (D-26		/ # OF LOWERCASE ENGLISH LETTERS
BNO,	SMA CLA
ANO,	CLA SKP
		IAC
#endasm
}

cupper(p)				/* In place convert to uppercase */
................................................................................
	p;
#asm
		DCA ZTMP
CPP1,	CLA
		TADI ZTMP
		SNA
		JMP CPP2
		TAD (D-97		/ ASCII 'a'
		SPA
		JMP CPP3
		TAD (D-26		/ # OF LOWERCASE ENGLISH LETTERS
		SMA
		JMP CPP3
		TAD (D91		/ 97 + 26 - 91 = 32 = ('a' - 'A')
		DCAI ZTMP
CPP3,	ISZ ZTMP
		JMP CPP1
CPP2,
#endasm
}

................................................................................
toupper(p)
int p;
{
	p;
#asm
		DCA ZTMP
		TAD ZTMP
		TAD (D-97		/ SEE cupper() COMMENTARY
		SPA
		JMP TPP3
		TAD (D-26
		SMA
		JMP TPP3
		TAD (D91
		JMP TPP2
TPP3,	CLA CLL
		TAD ZTMP
TPP2,
#endasm
}

................................................................................
int *dst,*src,cnt;
{
	dst+=cnt;
	src+=cnt;
	while (cnt--)
		*dst--=*src--;
}


Changes to src/cc8/os8/p8.c.

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
..
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
..
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
...
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

int gsym,lsym,gadr,ladr,stkp,lctr,*fptr,gsz,ctr,tm,ectr;
int glim;
int ltsz,pflg,t;
int tmstr[32];

main()
{

	iinit(128);
	fopen("CASM.TX","r");
	fopen("CC.SB","w");
	while (1) {
		t=fgetc();
		if (t=='!')
			break;
................................................................................
				break;
			case 3:
				strcpy(tmstr,"\tCIA\r\n");
			case 2:
				fprintf("%s\tTADI STKP\r\n\tJMSI POP\r\n",tmstr);
				break;
			case 4:
				fprintf("\tCLA\r\n\tTAD (%d\r\n",strd());
				break;
			case 5:
				tm=strd();
				if (tm<0)
					tm=200-tm;
				fprintf("CC%d,\r\n",tm);
				break;
			case 6:
				if (strl()>1)
					fprintf("\tTAD STKP\r\n\tTAD (%d\r\n\tDCA STKP\r\n",strl());
				else
					if (strl()>0)
						fputs("\tISZ STKP\r\n");
				strd();
				break;
			case 7:
				p=gm+strd();
................................................................................
					fputc(*p++);
				fputs(",\r\n");
				break;
			case -8:
				strcpy(tmstr,"\tDCA JLC\r\n\tTADI JLC\r\n");
			case 8:
				if (strl()>0)
					fprintf("\tCLA\r\n\tTAD (%d\r\n%s\tJMSI PSH\r\n",strd(),tmstr);
				else
					fprintf("\tCLA\r\n\tTAD STKP\r\n\tTAD (%d\r\n%s\tJMSI PSH\r\n",strd(),tmstr);
				break;
			case 9:
				tm=strd();
				p=gm+strd();
				strcpy(tkbf,"        ");
				memcpy(tkbf,p,7);
				if (p=strstr(xlt,tkbf)) {
					t=(p-xlt)>>3;
					if ((t==6) + (t==17) + (t==23)) 
						fprintf("\tCLA\r\n\tTAD (%d\r\n\tJMSI PSH\r\n",tm++);
					fprintf("\tCLA\r\n\tTAD (%d\r\n\tMQL\r\n\tCALL 1,LIBC\r\n\tARG STKP\r\n\tCDF1\r\n",t);
				}
				else
					fprintf("\tCPAGE 2\r\n\tJMSI PCAL\r\n\t%s\r\n",tkbf);
				if (tm)
				    fprintf("\tMQL\r\n\tTAD (%d\r\n\tTAD STKP\r\n\tDCA STKP\r\n\tSWP\r\n",-tm);
				break;
			case 10:
				fprintf("\tCLA\r\n\tTAD GBL\r\n\tTAD (%d\r\n",strd());
				break;
			case -11:
				fputs("\tCIA\r\n\tTADI STKP\r\n\tJMSI POP\r\n\tSMA SZA CLA\r\n\tCMA\r\n");
				break;
			case 11:
				fputs("\tCIA\r\n\tTADI STKP\r\n\tJMSI POP\r\n\tSPA CLA\r\n\tCMA\r\n");
				break;
			case 12:
					fprintf("\tSNA\r\n\tJMP CC%d\r\n",strd());
				break;
			case 13:
				fputs("\tJMSI POP\r\n\tDCA JLC\r\n\tSWP\r\n\tCALL 1,MPY\r\n\tARG JLC\r\n\tCDF1\r\n");
				break;
			case -14:
				fputs("\tCALL 1,IREM\r\n\tARG 0\r\n\tCDF1\r\n");
				break;
................................................................................
			case 14:
				fputs("\tJMSI POP\r\n\tDCA JLC\r\n\tSWP\r\n\tCALL 1,DIV\r\n\tARG JLC\r\n\tCDF1\r\n");
				break;
			case 15:
				fputs("\tISZI JLC\r\n\tNOP\r\n");
				break;
			case 16:
				fprintf("\tMQL\r\n\tTAD STKP\r\n\tTAD (%d\r\n\tDCA STKP\r\n\tSWP\r\n\tJMPI POPR\r\n/\r\n",strd());
				break;
			case 17:
				pflg++;
			case -17:
				if (strl()>0) 
					fprintf("\tCLA\r\n\tTAD (%d\r\n\tDCA JLC\r\n\tTADI JLC\r\n",strd());
				else
					fprintf("\tCLA\r\n\tTAD STKP\r\n\tTAD (%d\r\n\tDCA JLC\r\n\tTADI JLC\r\n",strd());
				if (pflg==0)
					break;
			case 19:
				fputs("\tJMSI PSH\r\n");
				break;
			case 20:
				fputs("\tANDI STKP\r\n\tJMSI POP\r\n");
				break;
			case -20:
				fputs("\tJMSI POP\r\n\tMQA\r\n");
				break;
			case 21:
				if (strl()>0) 
					fprintf("\tCLA\r\n\tTAD (%d\r\n",strd());
				else
					fprintf("\tCLA\r\n\tTAD STKP\r\n\tTAD (%d\r\n",strd());
				break;
			case 22:
				fputs("\tDCA JLC\r\n\tTADI JLC\r\n");
				break;
			case 23:
				if (strl()<200)
					fprintf("\tJMP CC%d\r\n",strl());
				strd();
				break;
			case -23:
					fprintf("\tJMP CC%d\r\n",strd());
				break;
			case 24:
				fputs("\tCIA\r\n\tTADI STKP\r\n\tJMSI POP\r\n\tSNA CLA\r\n\tCMA\r\n");
				break;
			case 25:
				fputs("\tMQL\r\n\tCMA\r\n\tTADI JLC\r\n\tDCAI JLC\r\n\tSWP\r\n");
				break;
................................................................................
					if (t=='$')
						break;
					fputc(t);
				}
		}
	}
	ltsz=ltpt-ltbf;
	fprintf("\tLAP\r\n\tCPAGE %d\r\nLCC0,\t%d\r\nXCC0,\tCC0\r\nCC0,\t\r\n",ltsz+2,-ltsz);
	p=ltbf;
	while (ltsz) {
		fprintf("%d",*p++);
		if (ltsz>1)
			fputs("; ");
		if ((ltsz&7)==0) 
			fputc(13);
		ltsz--;
	}
	fprintf("\r\n\tEAP\r\nGBLS,\t%d\r\n",gadr);
	fputs("\r\nMCC0,\t0\r\n\tCDF1\r\n\tTAD LCC0\r\n\tSNA CLA\r\n\tJMP I MCC0\r\n\tTAD XCC0\r\n\tDCA JLC\r\nDCC0,\tCDF0\r\n\tTADI JLC\r\n");
	fputs("\tJMSI PSH\r\n\tCLA\r\n\tISZ JLC\r\n\tISZ LCC0\r\n\tJMP DCC0\r\n\tJMP I MCC0\r\nCCEND,\t0\r\n\t\END\r\n");

	fclose();
}








<







 







|





|



|







 







|

|









|
|




|


|








|







 







|





|

|













|

|






|



|







 







|


|






|




|
>
36
37
38
39
40
41
42

43
44
45
46
47
48
49
..
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
..
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
int gsym,lsym,gadr,ladr,stkp,lctr,*fptr,gsz,ctr,tm,ectr;
int glim;
int ltsz,pflg,t;
int tmstr[32];

main()
{

	iinit(128);
	fopen("CASM.TX","r");
	fopen("CC.SB","w");
	while (1) {
		t=fgetc();
		if (t=='!')
			break;
................................................................................
				break;
			case 3:
				strcpy(tmstr,"\tCIA\r\n");
			case 2:
				fprintf("%s\tTADI STKP\r\n\tJMSI POP\r\n",tmstr);
				break;
			case 4:
				fprintf("\tCLA\r\n\tTAD (%o\r\n",strd());
				break;
			case 5:
				tm=strd();
				if (tm<0)
					tm=200-tm;
				fprintf("CC%o,\r\n",tm);
				break;
			case 6:
				if (strl()>1)
					fprintf("\tTAD STKP\r\n\tTAD (%o\r\n\tDCA STKP\r\n",strl());
				else
					if (strl()>0)
						fputs("\tISZ STKP\r\n");
				strd();
				break;
			case 7:
				p=gm+strd();
................................................................................
					fputc(*p++);
				fputs(",\r\n");
				break;
			case -8:
				strcpy(tmstr,"\tDCA JLC\r\n\tTADI JLC\r\n");
			case 8:
				if (strl()>0)
					fprintf("\tCLA\r\n\tTAD (%o\r\n%s\tJMSI PSH\r\n",strd(),tmstr);
				else
					fprintf("\tCLA\r\n\tTAD STKP\r\n\tTAD (%o\r\n%s\tJMSI PSH\r\n",strd(),tmstr);
				break;
			case 9:
				tm=strd();
				p=gm+strd();
				strcpy(tkbf,"        ");
				memcpy(tkbf,p,7);
				if (p=strstr(xlt,tkbf)) {
					t=(p-xlt)>>3;
					if ((t==6) + (t==17) + (t==23)) 
						fprintf("\tCLA\r\n\tTAD (%o\r\n\tJMSI PSH\r\n",tm++);
					fprintf("\tCLA\r\n\tTAD (%o\r\n\tMQL\r\n\tCALL 1,LIBC\r\n\tARG STKP\r\n\tCDF1\r\n",t);
				}
				else
					fprintf("\tCPAGE 2\r\n\tJMSI PCAL\r\n\t%s\r\n",tkbf);
				if (tm)
				    fprintf("\tMQL\r\n\tTAD (%o\r\n\tTAD STKP\r\n\tDCA STKP\r\n\tSWP\r\n",-tm);
				break;
			case 10:
				fprintf("\tCLA\r\n\tTAD GBL\r\n\tTAD (%o\r\n",strd());
				break;
			case -11:
				fputs("\tCIA\r\n\tTADI STKP\r\n\tJMSI POP\r\n\tSMA SZA CLA\r\n\tCMA\r\n");
				break;
			case 11:
				fputs("\tCIA\r\n\tTADI STKP\r\n\tJMSI POP\r\n\tSPA CLA\r\n\tCMA\r\n");
				break;
			case 12:
					fprintf("\tSNA\r\n\tJMP CC%o\r\n",strd());
				break;
			case 13:
				fputs("\tJMSI POP\r\n\tDCA JLC\r\n\tSWP\r\n\tCALL 1,MPY\r\n\tARG JLC\r\n\tCDF1\r\n");
				break;
			case -14:
				fputs("\tCALL 1,IREM\r\n\tARG 0\r\n\tCDF1\r\n");
				break;
................................................................................
			case 14:
				fputs("\tJMSI POP\r\n\tDCA JLC\r\n\tSWP\r\n\tCALL 1,DIV\r\n\tARG JLC\r\n\tCDF1\r\n");
				break;
			case 15:
				fputs("\tISZI JLC\r\n\tNOP\r\n");
				break;
			case 16:
				fprintf("\tMQL\r\n\tTAD STKP\r\n\tTAD (%o\r\n\tDCA STKP\r\n\tSWP\r\n\tJMPI POPR\r\n/\r\n",strd());
				break;
			case 17:
				pflg++;
			case -17:
				if (strl()>0) 
					fprintf("\tCLA\r\n\tTAD (%o\r\n\tDCA JLC\r\n\tTADI JLC\r\n",strd());
				else
					fprintf("\tCLA\r\n\tTAD STKP\r\n\tTAD (%o\r\n\tDCA JLC\r\n\tTADI JLC\r\n",strd());
				if (pflg==0)
					break;
			case 19:
				fputs("\tJMSI PSH\r\n");
				break;
			case 20:
				fputs("\tANDI STKP\r\n\tJMSI POP\r\n");
				break;
			case -20:
				fputs("\tJMSI POP\r\n\tMQA\r\n");
				break;
			case 21:
				if (strl()>0) 
					fprintf("\tCLA\r\n\tTAD (%o\r\n",strd());
				else
					fprintf("\tCLA\r\n\tTAD STKP\r\n\tTAD (%o\r\n",strd());
				break;
			case 22:
				fputs("\tDCA JLC\r\n\tTADI JLC\r\n");
				break;
			case 23:
				if (strl()<200)
					fprintf("\tJMP CC%o\r\n",strl());
				strd();
				break;
			case -23:
					fprintf("\tJMP CC%o\r\n",strd());
				break;
			case 24:
				fputs("\tCIA\r\n\tTADI STKP\r\n\tJMSI POP\r\n\tSNA CLA\r\n\tCMA\r\n");
				break;
			case 25:
				fputs("\tMQL\r\n\tCMA\r\n\tTADI JLC\r\n\tDCAI JLC\r\n\tSWP\r\n");
				break;
................................................................................
					if (t=='$')
						break;
					fputc(t);
				}
		}
	}
	ltsz=ltpt-ltbf;
	fprintf("\tLAP\r\n\tCPAGE %o\r\nLCC0,\t%o\r\nXCC0,\tCC0\r\nCC0,\t\r\n",ltsz+2,-ltsz);
	p=ltbf;
	while (ltsz) {
		fprintf("%o",*p++);
		if (ltsz>1)
			fputs("; ");
		if ((ltsz&7)==0) 
			fputc(13);
		ltsz--;
	}
	fprintf("\r\n\tEAP\r\nGBLS,\t%o\r\n",gadr);
	fputs("\r\nMCC0,\t0\r\n\tCDF1\r\n\tTAD LCC0\r\n\tSNA CLA\r\n\tJMP I MCC0\r\n\tTAD XCC0\r\n\tDCA JLC\r\nDCC0,\tCDF0\r\n\tTADI JLC\r\n");
	fputs("\tJMSI PSH\r\n\tCLA\r\n\tISZ JLC\r\n\tISZ LCC0\r\n\tJMP DCC0\r\n\tJMP I MCC0\r\nCCEND,\t0\r\n\t\END\r\n");

	fclose();

}