Ternary computer programming: play with the emulator

As I said, I am slowly building a very simple, but functional and at the same time uncompromisingly ternary calculator based on a balanced ternary number system. In this article, I describe the emulator of my calculator , which will help me in debugging iron. If you are interested, do not hesitate to write programs for it, I will definitely launch them on real hardware as soon as it is ready! It's very simple, the Triador understands the usual very primitive imperative language, similar to assembler or brainfuck :)



- A terrible nightmare! Zeros and ones are everywhere. And it seems I saw a deuce.
β€œIt's just a dream, Bender.” There are no twos.

, ! - , , !




, -13 +13. R1-R4 R5-R13. , R13 β€” , ( ). , 13 [-13..+13]. , / . 27 27 . , 729 . :






, , ( ).


9 , . , EX ttt halt and catch fire. :






git clone https://github.com/ssloy/triador.git
cd triador
mkdir build
cd build
cmake ..
make
./triador ../prog/add.txt

:


Open in gitpod


, . ( ).





. . . , , 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:



( ) β€” . 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.



$ ./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.



$ ./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;
}

:



:


$ ./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

Note that R2 stores 4, the largest common factor is 12 and -8.




Conclusion


Programming my ternary calculator is no more difficult than any calculator such as MK-61, and trinity does not affect the principles of building programs. The second season of my micro- series will be devoted to the construction of an arithmetic-logical device. In the meantime, sanitary unrest does not allow you to move well with iron, you can take your soul with an emulator. Offer your options for solving the above tasks, offer new interesting tasks, let's have fun!




Stay tuned.

All Articles