就像我说的那样,我正在缓慢地构建一个基于平衡三进制数系统的非常简单但功能强大且毫不妥协的三进制计算器。在本文中,我将介绍计算器的仿真器,这将有助于调试铁。如果您有兴趣,请不要犹豫为其编写程序,我一定会在准备好后立即在真实硬件上启动它们!这很简单,Triador理解通常的非常原始的命令式语言,类似于汇编器或Brainfuck :)
![](https://habrastorage.org/webt/cz/e-/ee/cze-eepro8zhxk1hf8bnzxiakkm.jpeg)
-噩梦!零和一无处不在。看来我看到了一个平局。
“这只是一个梦想,班德。” 没有二。
, ! - , , !
![](https://habrastorage.org/getpro/habr/post_images/795/95e/3c0/79595e3c085c9880fa6b78355c7d5759.jpg)
, -13 +13. R1-R4 R5-R13. , R13 — , ( ). , 13 [-13..+13]. , / . 27 27 . , 729 . :
![](https://habrastorage.org/getpro/habr/post_images/4a5/099/651/4a50996513b41a9be43eec98bfb87764.png)
, , ( ).
9 , . , EX ttt halt and catch fire. :
![](https://habrastorage.org/getpro/habr/post_images/29d/bdc/ec9/29dbdcec9a40f2ddfccca6cde5378687.png)
git clone https://github.com/ssloy/triador.git
cd triador
mkdir build
cd build
cmake ..
make
./triador ../prog/add.txt
:
![在gitpod中打开](https://habrastorage.org/getpro/habr/post_images/a59/ff6/9ea/a59ff69eae523af001f680b9ba9fa9ec.svg)
, . ( ).
. . . , , ttt NNN (-13) PPP (+13):
- EX ttt
- JP ttt
- SK ttt
- OP ttt
- RR ttt
- R1 ttt
- R2 ttt
- R3 ttt
- R4 ttt
.
, . , , R1-R4.
, , R2 R3. ! /. ? !
. , R2 R3 . /, R2 R3 , R2 :
int main() {
unsigned int R2 = 2;
unsigned int R3 = 11;
while (R2!=0) {
R3++;
R2--;
}
return R3;
}
, ? , , R2 :
int main() {
int R2 = -2;
int R3 = 13;
while (R2!=0) {
if (R2>0) R3++;
if (R2<0) R3--;
if (R2>0) R2--;
if (R2<0) R2++;
}
return R3;
}
, , . . : / R1. , R3 , R1, , R1 R3!
, R2 R3 . R3:
![](https://habrastorage.org/getpro/habr/post_images/d67/93a/ac0/d6793aac06fe36f835a16a9507a3f5d7.png)
( ) — . NNN NNN, -364 . , , , .
:
$ ./triador ../prog/add.txt | tail -n 3
R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13 C PC
11 0 11 5 0 6 -12 -2 11 8 11 -10 -13 0 -345
, R3 11, -2 + 13.
, while , , JP, SK, .
: , JP ttt ttt, . ? R13! JP ttt 27*R13 + ttt. , ttt , R13. NNN (-13),
R1 NNN # write -13 to R1
RR NNN # copy R1 to R13
, R13, NNN.
, . , , . R2 R3, R3 R4: R3 + R4*27, , R4 -1, 0 or 1.
![](https://habrastorage.org/getpro/habr/post_images/4f8/c34/1dc/4f8c341dcb7f4ffae0a20507ad7b8c00.png)
$ ./triador ../prog/add-with-overflow-control.txt |tail -n 3
R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13 C PC
-12 0 -12 1 -9 2 -12 -6 4 5 6 7 -13 0 -338
, R3 + 27 * R4 15, 2+13. - R4 C:
[...]
SK OOO # skip if C==0
JP OPO # overflow ───────┐
JP PNO # no overflow ────│─┐
R4 OOP # write 1 to R4 <─┘ │
SK OOP # skip if C==1 │
R4 OON # write -1 to R4 │
RR OPN # copy R2 to R1 <───┘
[...]
, [-13..+13] ? , word ( , ). R1,R2 R3,R4. , R4,R5.
![](https://habrastorage.org/getpro/habr/post_images/9ef/4a2/cda/9ef4a2cda6e1b4a0b98b56d12d5b0cb3.png)
$ ./triador ../prog/long-add.txt |tail -n 3
R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13 C PC
3 0 0 -6 3 -13 1 3 6 5 13 -2 -13 0 -335
, R4+27 * R5 = 75, 331-256. , ? , , . , . , . , C++ :)
: , . , . ! R7 , -. , R7 :
[...]
SK ONO # skip if R1!=0
JP OON # sub return 1
JP POP # sub return 2
, . , R2 R3. R2.
, , C++. , R2 R3 , :
int main() {
int R2 = 12, R3 = 8;
while (true) {
if (R2==R3) break;
if (R2>R3)
R2 = R2 - R3;
else
R3 = R3 - R2;
}
return R2;
}
, ? , : R2, R3; , . m R2 R3, , , R2-R3. , R2 = a m, R3 = b m , R2-R3 = (a-b) m. - .
, ? :) : , . , !
int main() {
int R2 = 12, R3 = -8;
while (true) {
if (R2<0) R2 = -R2;
if (R3>0) R3 = -R3;
if (R2==-R3) break;
int R4 = R2 + R3;
if (R4>0)
R2 = R4;
else
R3 = R4;
}
return R2;
}
:
![](https://habrastorage.org/getpro/habr/post_images/10c/63b/943/10c63b9437b18a9c3cbfdfd8be082f85.png)
:
$ ./triador ../prog/gcd.txt |tail -n 3
R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13 C PC
-13 4 -4 0 4 12 13 1 -7 -9 3 4 -13 0 -338
请注意,R2存储4,最大公因数是12和-8。
结论
对我的三元计算器进行编程并不比任何计算器(例如MK-61)困难,而且三位一体不会影响构建程序的原理。我的微型系列小说的第二季将专门研究算术逻辑设备的构造。同时,动荡不安使您无法用铁运动,可以用模拟器摆脱灵魂。提供解决上述任务的选项,提供有趣的新任务,让我们玩得开心!
敬请关注。