︙ | | |
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
-
+
|
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 led to C’s own
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.
|
︙ | | |
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
-
+
|
[sms]: http://so-much-stuff.com/pdp8/C/C.php
## Requirements
The CC8 system generally assumes the availability of:
* [At least 16 kWords of core](#memory) at run time for programs
* [At least 12 kWords of core](#memory) at run time for programs
compiled with CC8. The [native OS/8 CC8 compiler passes](#ncpass)
require 20 kWords to compile programs.
CC8 provides no built-in way to use more memory than this, so you
will probably have to resort to [inline assembly](#asm) or FORTRAN
II library linkage to get access to more than 16 kWords of core.
|
︙ | | |
326
327
328
329
330
331
332
333
334
335
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
326
327
328
329
330
331
332
333
334
335
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
-
+
-
+
-
-
-
-
-
-
-
+
+
-
-
-
-
+
+
-
-
+
+
-
-
+
-
+
-
+
-
+
-
-
-
-
+
+
+
+
-
|
...
}
1. **Recursion:** See [`FIB.C`][fib] for an example of this.
1. **Simple arithmetic operators:** `+`, `-`, `*`, `/`, etc.
1. **Bitwise operators:** `&`, `|`, `~` and `!`
1. **Bitwise operators:** `&`, ¦, `~` and `!`
1. **Simple comparison operators:** False expressions evaluate as 0 and
true as -1 in two’s complement form, meaning all 1's in binary form.
See the list of limitations below for the operators excluded by our
"simple" qualifier.
1. **2-character operators:** `++`, `--`, `==`, `!=`,`>=`, `<=`, `&&`,
1. **A few 2-character operators:** `++`, `--` (postfix only) and `==`.
and `||`. Note that `++` and `--` are postfix only, and
that `&&` and `||` are [implemented as `&` and `|`](#2cbo).
1. **Ternary operator:** The `?:` operator works as of May 2020; it may
be nested.
1. **Limited library:** See [below](#libc) for a list of library
functions provided, including their known limitations relative to
Standard C.
There are many limitations in this library relative to Standard C or
even K&R C, which are documented below.
1. **Limited structuring constructs:** `if`, `while`, `for`, etc. are
supported. There is a nesting limit of 10 which is rarely exceeded in
most applications. In addition, `switch` statements are now supported
supported, but they may not work as expected when deeply nested or
in long `if/else if/...` chains.
via a code re-write in the C pre-processor (cc.sv). See [`FORTH.C`][forth]
for an example.
[fib]: /doc/trunk/src/os8/examples/fib.c
[forth]: /doc/trunk/src/os8/examples/forth.c
[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.
It is not necessary to give argument types when declaring function
arguments, but you must declare a return type with the OS/8 CC8
Because the types are already known, it is not necessary to give
types when declaring function arguments:
compiler:
int myfn(n) { /* do something with n */ }
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 but not the return type, per K&R C rules:
types to be declared, if not the return type, per K&R C rules:
int myfn(n)
myfn(n)
int n;
{
/* do something with n, then _maybe_ return something */
/* do something with n */
}
The type int is mandatory for all functions.
The cross-compiler supports `void` as an extension to K&R C. This type
is converted to `int` in the pre-processor. Similarly, the type `char` is
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`.
converted. These type may be used for readability purposes.
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
|
︙ | | |
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
-
+
-
-
-
-
-
+
+
+
+
-
-
+
+
-
+
-
+
|
(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` / `#endasm`. See
* [Inline assmembly](#asm) via `#asm`.
[`FIB.C`][fib] for an example
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:
int i = 5;
must instead be:
int i;
i = 5;
8. <a name="2cbo"></a>`&&` and `||` work, but because they
are internally converted to `&` and `|`, their precedence has
changed, and they do not short-circuit as in a conforming C
compiler.
8. There is no `&&` nor ¦¦, nor are there plans to add
them in the future. Neither is there support for complex relational
operators like `>=` nor even `!=`. Abandon all hope for complex
assignment operators like `+=`.
You can work around such differences with clever coding. For
example, this code for a conforming C compiler:
Most of this can be worked around through clever coding. For
example, this:
if (i != 0 || j == 5)
should be rewritten for CC8 to avoid the precedence changes as:
could be rewritten to avoid both missing operators as:
if (!(i == 0) || (j == 5))
if (!(i == 0) | (j == 5))
because a true result in each subexpression yields -1 per the
previous point, which when bitwise OR'd together means you get -1 if
either subexpression is true, which means the whole expression
evaluates to true if either subexpression is true.
If the code you were going to write was instead:
|
︙ | | |
498
499
500
501
502
503
504
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
534
535
536
537
538
539
540
541
542
|
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
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. As of May 2020, `switch` is implemented via re-write to cascading
`if`/`then` statements. There are a number of limitations to this
approach that a CC8 user should be aware of.
12. `switch` doesn't work.
The primary one to keep in mind is that that if you use a
memory-mutating expression in the `switch` clause with a conforming
C compiler, it is evaluated just once at the start of the block, but
in CC8, it is evaluated once for each generated `if` sub-expression
that the code visits. That is, you should not say things like this
in code meant to be compiled with the CC8 native compiler:
switch (*p++) {...}
Say instead:
int temp = *p++;
switch (temp) {....}
Also, there **must** be a `default` case, and cases (including the
default case) must be terminated with a `break`. CC8 does not allow
for cases that fall through to the following case. The following
code has at least three syntax errors:
switch (x) {
case 1: foo();
case 2: bar();
default: qux();
}
13. `sizeof()` is not implemented.
<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
|
︙ | | |
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
-
+
-
-
-
+
+
|
The bulk of the Standard C Library is not provided, including some
functions you’d think would go along nicely with those we do provide,
such as `feof()` or `fseek()`. Keep in mind that the library is
currently restricted to [a single 4 kWord field](#memory), and we
don’t want to lift that restriction. Since the current implementation
nearly fills that space, it is unlikely that we’ll add much more
functionality beyond the currently provided 33 LIBC functions plus [the
functionality beyond the currently provided 33 functions. If we ever fix
9 additional functions](#addfn). If we ever fix any of the limitations
we’ve identified below, consider it “gravy” rather than any kind of
obligation fulfilled.
any of the limitations we’ve identified below, consider it “gravy”
rather than any kind of obligation fulfilled.
Some of these missing functions are less useful in the CC8 world than in
more modern C environments. The low-memory nature of this world
encourages writing loops over [word strings](#wordstr) in terms of
pointer arithmetic and implicit zero testing (e.g. `while (*p++) { /*
use p */ }`) rather than make expensive calls to `strlen()`, so that
function isn’t provided.
|
︙ | | |
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
777
778
779
780
781
|
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
733
|
-
+
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
+
+
|
[cppr]: https://en.cppreference.com/w/c
[iot]: /wiki?name=IOT+Device+Assignments
[libch]: /doc/trunk/src/cc8/include/libc.h
[libcsrc]: /doc/trunk/src/cc8/os8/libc.c
### <a id="atoi"></a>`int atoi(s, *result)`
### <a id="atoi"></a>`atoi(s, outlen)`
Takes a null-terminated ASCII character string pointer `s` and tries to
interpret it as a 12-bit PDP-8 two’s complement signed integer, storing
the value in `*result` and returning the number of bytes in `s`
Takes a null-terminated ASCII character string pointer `s` and returns a
12-bit PDP-8 two’s complement signed integer. The length of the numeric
string is returned in `*outlen`.
consumed.
**Standard Violations:**
* Instead of returning the converted integer, this function stores
that value in `*result`.
* Whereas `atoi()` in Standard C returns the converted value, in this
function, `s[retval]` is the first invalid — non-sign, non-digit,
non-space — character in the string, where `retval` is the return
value.
* Skips leading ASCII 32 (space) characters only, not those matched by
[`isspace()`](#isspace), as the Standard requires.
* The `outlen` parameter is nonstandard.
### <a id="cupper"></a>`cupper(p)`
Implements this loop more efficiently:
char* tmp = p;
|
︙ | | |
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
|
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
|
-
+
|
* There appears to be a bug in the current implementation that
requires you to open the input file before opening an output file
when you need both. It may not be possible to fix this within the
current limitations on the library, but if you come up with
something, [we accept patches][hakp].
[hakp]: /doc/trunk/CONTRIBUTING.md#patches
[hakp]: /doc/trunk/HACKERS.md#patches
### <a id="fprintf"></a>`fprintf(fmt, args...)`
Writes its arguments (`args`...) to the currently-opened output file
according to format string `fmt`.
|
︙ | | |
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
|
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
|
-
+
+
-
-
-
-
+
+
+
+
+
+
+
|
of characters written so far, not a negative value as the Standard
requires. In the case of `sprintf()`, this means the trailing NUL
character will not be written!
* There is no `snprintf()`, `vprintf()`, etc.
### <a id="scanf" name="fscanf"></a>`fscanf`, `scanf`, `sscanf`
### <a id="sscanf"></a>`sscanf`
Reads formatted input from a file.
Parse strings according to a `printf`-like format specification. `scanf`
gets the string from the interactive terminal, `fscanf` gets it from a
file opened with [`fopen()`](#fopen), and `sscanf` gets it from a
NUL-terminated C string already in core.
**Standard Violations:**
* `[f]scanf()` is not provided. Call [`[f]gets()`](#gets) to get a
string and then call `sscanf()` on it.
* This list cannot possibly be complete.
**DOCUMENTATION INCOMPLETE**
### <a id="strcat"></a>`strcat(dst, src)`
Concatenates one [0-terminated word string](#wordstr) to the end of
|
︙ | | |
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
|
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
### <a id="xinit"></a>`xinit()`
Prints the CC8 compiler’s banner message. This is in LIBC only because
it’s called from several places within CC8 itself.
**Nonstandard.**
## <a id="addfn"></a>Additional Utility Routines
The functions that CC8 uses to manipulate the software stack are also
available to be called by end-user programs: `PUSH`, `POP`, `PUTSTK`,
`POPRET`, and `PCALL`. The page zero pointers for this stack are
initialized by code in `header.sb`, which is injected into your
program’s startup sequence during compilation.
In addition, there are a set of functions that may be used to provide
temporary storage in field 4, acting like a temporary binary file:
`void iinit(int address)`: Reset the file pointer to an arbitrary
address range 0-4095.
`void stri(int value)`: Store ‘value’ at the current address, and
increment the address pointer.
`int strl()`: Read the word at the current address, and do not increment
the address.
`int strd()`: Read the word at the current address, and increment the
address.
As field 4 is not used by OS/8, your program may use the entire field.
This library code does not check for overflow: going beyond address 4095
will simply wrap to address 0.
<a id="examples"></a>
## Trying the Examples
The standard PiDP-8/I OS/8 RK05 boot disk contains several example
C programs that the OS/8 version of CC8 is able to compile.
|
︙ | | |
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
|
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
|
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
-
-
+
-
-
-
-
-
+
-
|
Pascal’s triangle without using factorials, which are a bit out of
range for 12 bits!
The other examples preinstalled are:
* **<code>calc.c</code>** - A simple 4-function calculator program.
* **<code>pd.c</code>** - Shows methods for doing double-precision
(i.e. 24-bit) integer calculations.
* **<code>hlb.c</code>** - Generates [Hilbert curves][hc] on a Tek4010
series display using raw terminal codes. Therefore, you must be
running a Tek4010 emulator when running this program, else you will
get garbage on the display!
* **<code>fib.c</code>** - Calculates the first 10 Fibonacci numbers.
This implicitly demonstrates CC8's ability to handle recursive
function calls.
* **<code>basic.c</code>** - A simple Basic interpreter used to test
a simple recursive expression processor.
* **<code>forth.c</code>** - A simple Forth interpreter used to test
If you look in `src/cc8/examples`, you will find these same programs
plus `basic.c`, a simple BASIC language interpreter. This one is
not preinstalled because its complexity is currently beyond the
capability of the OS/8 version of CC8. To build it, you will have
switch statemments etc.
to use [the cross-compiler](#cross), then assemble the resulting
The two interpeters are quite complex, particularly the Forth
interpreter, which contains 300 lines of code and implements a number of
basic Forth functions. This example is intended to show what can be
crammed into 4k of core.
`basic.sb` file under OS/8.
Another set of examples not preinstalled on the OS/8 disk are
`examples/pep001-*.c`, which are described [elsewhere][pce].
[hc]: https://en.wikipedia.org/wiki/Hilbert_curve
[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
|
︙ | | |
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
|
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
|
-
|
**Field 1:** The user data field (UDF): globals, literals, and stack
**Field 2:** The program's executable code
**Field 3:** The LIBC library code
**Field 4:** (Optional) see the binary utilities above (stri...).
### <a id="os8res"></a>OS/8 Reservations
The uppermost page of fields 0 thru 2 hold the
[resident portion of OS/8][os8res] and therefore must not be touched by
a program built with CC8 while running under OS/8. For example, the
[OS/8 keyboard monitor][os8kbd] re-entry point is at 07600₈, [the output
|
︙ | | |
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
|
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
|
-
+
-
-
+
+
-
-
+
+
+
-
-
-
+
+
+
+
|
[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
Literals are placed in the same field as globals and the call stack,
It may surprise you to learn that literals are placed in the same field
rather that inline within the generated executable code. This may cause
surprise size limitations of the user programs.
as globals and the call stack.
Other C compilers place literals in among the executable code instead, a
CC8 does it this way because the FORTRAN II / SABR system does allow any
initialisation of COMMON storage in field 1, so the literals have to be
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`
stored in the user program page and then be copied into field 1 at
program initialization time. Various pointers to these regions are
mainatined by the compiler.
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
|
︙ | | |
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
|
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
|
-
+
|
|`DCAI` |3400 |same as `DCA I` in PAL8|
|`JMSI` |4400 |same as `JMS I` in PAL8|
|`JMPI` |5400 |same as `JMP I` in PAL8|
|`MQL` |7421 |load MQ from AC, clear AC|
|`ACL` |7701 |load AC from MQ (use `CLA SWP` to give inverse of `MQL`)|
|`MQA` |7501 |OR MQ with AC, result in MQ|
|`SWP` |7521 |swap AC and MQ|
|`DILX` |6053 |set VC8E X coordinate (used by [`dispxy()`](#dispxy))|
|`DILX` |6053 |set VC8E X coordinate (used by [`dispxy()`](#dispxy)|
|`DILY` |6054 |set VC8E Y coordinate|
|`DIXY` |6054 |pulse VC8E at (X,Y) set by `DIXY`,`DILY`|
|`CDF0` |6201 |change DF to field 0|
|`CDF1` |6211 |change DF to field 1|
|`CAF0` |6203 |change both IF and DF to field 0|
|`RIF` |6224 |read instruction field: OR IF with bits 6-8 of AC|
|`BSW` |7002 |exchange the high and low 6 bits of AC|
|
︙ | | |