Code Snippets

All code on this page is PUBLIC DOMAIN

## Universal tritwise operations

Using OPA you can implement ANY 1-argument tritwise ternary function (there are 27 possibilities), for example:

OPA NOP - no operation (N->N, O->O, P->P), but changing flag RSF based on value of register A OPA PON - invert value of register A (N->P, O->O, P->N) and flag RSF will be also inverted OPA OPN - tritwise rotate up register A (N->O, O->P, P->N) and set flag RSF OPA PNO - tritwise rotate down register A (N->P, O->N, P->O) and set flag RSF OPA NNN - write NNN to register A and set flag RSF to N OPA OOO - write OOO to register A and set flag RSF to O OPA PPP - write PPP to register A and set flag RSF to P

Using OPB you can implement ANY 2-argument tritwise ternary function (there are 19683 possibilities), for example:

OPB NNN NOO NOP - perform operation MIN on A and B, save result to A and set flag RSF based on result OPB NOP OOP PPP - perform operation MAX on A and B, save result to A and set flag RSF based on result

## Ternary conditional jump

To prepare 3 DP (data pointer) registers, we may use this code:

LAI ONO - choose DPn SAF - save A to F (DPF=N) LDI xxx xxx xxx - save 9-trit address to DPn LAI OOO - choose DPo SAF - save A to F (DPF=O) LDI yyy yyy yyy - save 9-trit address to DPo LAI OPO - choose DPp SAF - save A to F (DPF=P) LDI zzz zzz zzz - save 9-trit address to DPp

Then we may do conditional jump dependent on value of RSF:

LAF - load A from F RRA - shift A right (move RSF value to DPF place) SAF - save A to F LPCD - load PC from chosen DP

Or we may do conditional jump dependent on value of BCF:

LAF - load A from F RLA - shift A left (move BCF value to DPF place) SAF - save A to F LPCD - load PC from chosen DP

## Simple subprogram call

Lets use DPo to save return pointer:

LAI OOO SAF - set DPo as current DP LDI return_adr - save value of label "return_adr" to DPo JMP subprg_adr - call subprogram using label "subprg_adr" return_adr: subprogram will return here

Then to return from subprogram by saved address we should do:

LAI OOO - if DPF was changed during subprogram SAF - set DPo as current DP LPCD - load PC from DPo