Древний printf
2025-04-12 02:13![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Компилятор Би для БЭСМ-6 маленько подрос и уже способен правильно сгенерить функцию printf(). Вот как она выглядела в районе 1969 года.
Напишем тест, задействуя все форматы.printf(fmt, args) {
auto ap, a, c, i, n;
i = 0;
ap = &args;
loop:
while ((c = char(fmt, i)) != '%') {
if (c == '*0')
return;
writeb(c);
i = i + 1;
}
i = i + 1;
c = char(fmt, i);
if (c == '%') {
writeb('%');
i = i + 1;
goto loop;
}
a = *ap;
if (c == 'd') {
printd(a);
} else if (c == 'o') {
printo(a);
} else if (c == 'c') {
write(a);
} else if (c == 's') {
n = 0;
while ((c = char(a, n)) != '*0') {
writeb(c);
n = n + 1;
}
} else {
/* bad format specification, ignore */
goto loop;
}
i = i + 1;
ap = ap + 1;
goto loop;
}
Компилируем, запускаем.main() {
printf("Hello, World!*n");
printf("%% %% %%%%*n");
printf("format %%d: %d %d*n", 123, -123);
printf("format %%o: %o %o*n", 234, -234);
printf("format %%c: %c %c*n", 'foo', 'bar');
printf("format %%s: *"%s*" *"%s*"*n", "Hello", "World");
}
Фунциклирует в лучшем виде однако.$ bbesm < test.b > test.assem
$ besmc test.assem libb.obj
$ ./test.exe
Hello, World!
% % %%
format %d: 123 -123
format %o: 352 -352
format %c: foo bar
format %s: "Hello" "World"