<iostream>, <cstdio> and <cstring> for CPPComp, on the keyboard and the screen¶
Status: implemented 2026-10-20 (v9.1). Manual: The C++ language, section “The standard headers”, and Peripherals.
1. Where it comes from¶
With myKeyPS2 and myVGAText a C++ program read keys with
cade_sync(0) and wrote them with toma_sync(1, c), a character at a time,
and had to turn numbers into digits itself. Luciano asked (2026-10-20) how a
PC does it and whether the SAPHO’s C++ could follow the same pattern: “pode
atualizar o compilador sim, vamos fazer o mais completo”, with printf and
std::string left for later as long as they are recorded and the ground is
prepared for them (GitHub issue #5).
On a PC, C++ writes with std::cout << x and reads with std::cin >> x or
std::cin.getline(buf, n); C has putchar, getchar, puts, fgets,
printf. Between the keyboard and the program sits the terminal, which
echoes what is typed and lets backspace edit the line; the program gets the
line only when Enter is pressed. The SAPHO has no terminal, so std::cin
does that work itself.
2. Decided¶
The standard names and behaviour, so a program written for a PC reads the same:
std::cout,std::cin,std::endl, the C functions, the<cstring>functions. Where C++ has a well-known trap, YANC keeps it (cin >> nleaves the Enter for the nextgetline, as on a PC).The compiler learns what the headers need (Luciano, 2026-10-20), rather than the headers dodging it:
an overloaded method is called by the overload its arguments pick (it was called by its bare name, a label that does not exist: the program compiled and did not assemble);
char,boolandunsignedare told apart fromintwhen an overload is chosen, by C++’s ranking (the same type, then a promotion char/bool to int, then a conversion);'A'has typecharandtrue/falsetypebool; a reference parameter (int&) matches by the type it refers to; a pointer tocharis apart from other pointers;operator<<andoperator>>can be overloaded (members, as the other operators), and a reference result chains (cout << a << b). Template instances keep the old coarse codes:f<char>is stillf<int>.
The links are set by
#definebefore the include (defaults: keyboard on link 0, screen on link 1, both_sync;YANC_ECHO,YANC_LINE_MAX). A#pragma yancwas proposed first; a#defineneeds nothing from the compiler (the link number must be a literal, and the macro expands to one).Three layers in one internal header,
<yanc_term>, soprintf,scanfandstd::stringstand on them later: one character on the links; numbers as text (integer in a base, with width, fill and sign; a float with significant digits); reading (the edited line, blanks, an integer, a float, a word, a line).No division anywhere: printing a number must not give the processor a divider (large, and a long path). Decimal digits are made by doubling a digit array one bit at a time; a float is printed with powers of 10 built by multiplication and digits counted by subtraction; reading multiplies by 10 as
(v << 3) + (v << 1).A float prints as C++ prints it by default (6 significant digits,
%g):7.5,3.14159,1e+10,0.00015,1e-07.<cstring>complete now (Luciano: the base ofstd::string):strlen strcmp strncmp strcpy strncpy strcat strncat strchr strrchr strstr memcmp memmove memchr, besidememcpy memset.Later (GitHub issue #5):
printf/scanf(need variadic functions),std::stringandstd::getline(cin, s),setw,setprecision,if (cin >> x)(needs a conversion operator).
3. What came up while building it¶
Namespaces are transparent in CPPComp: a name in
stdor in any namespace is global. The internal names of<yanc_term>all start withyanc_so they cannot collide with a program’s; the standard names (cout,endl,hex,dec…) are global once<iostream>is included.A link used only by code that is never called:
<iostream>holds the keyboard’s code, so a program withcoutalone left a#LKSYNC 0behind afterasm_reachremoved that code, and asmcomp refused the program.asm_reach(shared by both compilers) now drops a#LKSYNC kwhen noTOM k/CAD kis kept. Fixture test93.A float’s digits: scaling a large number down by 0.1 piled up the error (
1e10printed9.99999e+09); building the power of 10 up by multiplication fixed it.The old overload codes: compiled with the previous cppcomp, test92 stops at “redefinition of function ‘f’” (
f(char)besidef(int)). Every other C++ and C± program of the regress compiles to the same.asmas before (93 C++, 93 C±, compared one by one).
3b. Constant data in the data memory (TODO item 36, 2026-10-20)¶
Every string literal was written at the start of main by code, 3
instructions a character (LOD k, P_LOD c, STI __strN): in the DE2-115
demonstration for Dênis (nucleo.cpp, a cin/cout dialogue) 493 of its
1,672 instructions. Luciano chose (2026-10-20) option 2 of four:
string literals only;
string literals and global
constarrays with constant values (taken);every initialized global array, as C±’s
#arrays: a program that changes such a table and is reset would not get the values back;option 2 plus the arrays the compiler proves are never written.
The data memory’s .mif is loaded when the FPGA is configured, not on
reset, so only data the program cannot change goes there in C++: options 1
and 2 change nothing a program does. asmcomp got a directive with the
values on its own line, #arrayd <name> <type> <size> <n> <v0> ...
(the words after the n given are zero); the #arrays file form would have
made cppcomp write files into the user’s folder. Measured: nucleo.cpp
1,672 -> 1,180 instructions, the same screen in the board simulation.
Found on the way: char s[] = "..." (no size) got no words at all (the
parser deduced the size only from a braced list), so the string was copied
over whatever came next; fixed, test94. And the shared asm passes read a
line through a 4,096-byte buffer, which would have cut a long #arrayd
line; they read lines of any length now.
Luciano asked (2026-10-20) for the same directive in C±: int v[4] = {1, 2, 3};, which C± did not have, now goes to the .mif too, with C±’s file
semantics (loaded once; a local keeps its values between calls, with a
warning). Fixture cmm_braces.
4. Tests¶
test92: the overload rules (hand-computed golden).
test93: a program that only writes, with
<cstdio>and the string functions (hand-computed golden).test94:
constarrays and string literals in the data memory (int, float, char from a string, 2D, designated, bool, unsigned, int8_t), a non-const array still stored by code,char s[] = "..."sized (hand- computed golden).cmm_braces (C±): arrays between braces, 1D, 2D with and without inner braces, float, comp, a local one (hand-computed golden).
Regress pass VGATEXT, system C: a 32-bit C++ SAPHO with
<iostream>and<cstring>on amyKeyPS2and amyVGATextof its own; the keyboard types a name with a backspace and two numbers, and the bench reads the screen memory:Nome? Ana,Oi, Ana!,a b? 12 30,12 + 30 = 42,-7 ff 18 3.14159 1e+10 0.00015,Ana tem 3 letras.
5. For the documentation (issue #40)¶
Written: docs/language-cpp.md (the headers, overloading, the operators) and docs/peripherals.md (the keyboard and the screen from C++).