Programación ternaria: juega con el emulador

Como dije, estoy construyendo lentamente una calculadora muy simple, pero funcional y al mismo tiempo inflexiblemente ternaria basada en un sistema de números ternarios equilibrado. En este artículo, describo el emulador de mi calculadora , que me ayudará a depurar el hierro. Si está interesado, no dude en escribir programas para él, ¡definitivamente los lanzaré en hardware real tan pronto como esté listo! Es muy simple, el Triador comprende el lenguaje imperativo muy primitivo habitual, similar al ensamblador o al brainfuck :)



- ¡Una terrible pesadilla! Los ceros y unos están en todas partes. Y parece que vi un deuce.
"Es solo un sueño, Bender". No hay dos

, ! - , , !




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

:


Abierto en 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

Tenga en cuenta que R2 almacena 4, el factor común más grande es 12 y -8.




Conclusión


Programar mi calculadora ternaria no es más difícil que cualquier calculadora como la MK-61, y Trinity no afecta los principios de los programas de construcción. La segunda temporada de mi micro- serie estará dedicada a la construcción de un dispositivo aritmético lógico. Mientras tanto, los disturbios sanitarios no te permiten moverte bien con hierro, puedes quitarte el alma con un emulador. Ofrezca sus opciones para resolver las tareas anteriores, ofrezca nuevas tareas interesantes, ¡divirtámonos!




Manténganse al tanto.

All Articles