Talk:Divmeq

From Esolang
Jump to navigation Jump to search

I am the creator of Divmeq. This page is where the community will ask questions about Divmeq, propose extensions (which may added to the standard interpreter). The official programming tutorial will be here as well.

Programming tutorial

This will cover the nature of Divmeq and how various common constructions can be implemented in Divmeq.

Storing values

Single values

The most natural way to store a value into the accumulator is as a number.

e.g. 4 is stored into the accumulator as 4, using the code 0: 0.25 1

However, it is impossible to store multiple unbounded values using this system, and incrementing and decrementing is extremely difficult (if not impossible) to express.

Multiple values

Multiple values can be stored into the accumulator by using a Gödel numbering-like system in which the accumulator is the product of powers with values being exponents with prime-number bases.

e.g. 3 registers with values 7, 1, and 8, can be encoded into the accumulator as 273158, which is 150000000.

When values are encoded in this format (which will be referred to as the prime-addressing format), the accumulator become less of a single register and more of an unbounded set of unbounded sub-registers with prime number addresses. Multiplication and division is incrementing and decrementing these sub-registers; checking for divisibility is the same as checking if the values in these sub-registers are nonzero.

Assembly notation

This tutorial will use assembly-like syntax to demonstrate concepts in a more readable manner. This syntax can be converted to raw Divmeq syntax in trivial ways.

Assembly notation Divmeq
INC $a JMP b 1/a b
JMP b 1 b (This may not be representative of non-integers
TDE $a CJM b a b

CTM/TEM is used in place of JMP/CJM len(prgm). Letters are the addresses of sub-registers and instructions, dollar-sign letters are the values at those addresses.

For non-prime constants, the INC/TDE format is INC $w $x TDE $y $z.

e.g. The golfed addition program is 0: INC $2 TDE $3 CJM 0

e.g. The golfed subtraction program is 0: TDE $2 $3 CJM 0.

This notation is designed for the prime-addressing format. Multiplying and conditionally dividing by constants is shown as incrementing and conditionally decrementing the values in sub-registers. As such, the Divmeq assembly notation adds a layer of abstraction between the programmer and the raw code.

Brainfuck-style loops

In brainfuck, loops are allowed to execute only if the value at the pointer is not zero. This logic can be translated into Divmeq as follows:

B: TDE $X CJM B+2     // Checks if $X is nonzero
B+1: JMP E+1          // Exits the loop
B+2: INC $X JMP B+3   // Undoes the division done at line b
B+3..E-3: (Code within the brackets)
E-2: TDE $Y CJM E     // Checks if $Y is nonzero
E-1: JMP E+1          // Exits the loop
E: INC $Y JMP B+3     // Undoes the division done at line e-2

X and Y are the addresses of sub-registers, lines B and E are the beginning and ending lines of the loop.

For loops

For loops can be easily implemented in Divmeq. In the following code, the start parameter is stored in X and the difference between the stop and start parameters is stored in Y.

B: TDE $Y CJM B+2
B+1: JMP E+1
B+2: INC $X JMP 1
B+3..E: (code within loop)   // Iterator is in X
E: JMP B

Lines B and E are the beginning and end of a loop.

Operations

Addition

L: INC $X TDE $Y CJM L

X and Y are the addresses of the addends. Line L is the addition loop.

Subtraction

L: TDE $X $Y CJM L

Line L is the subtraction loop. The absolute of the result will be stored at whichever sub-register is greater.

Y = X

This loop non-destructively copies X to Y, i.e. X is copied to Y without X being destroyed. This loop allows data to be loaded in from another sub-register with zero loss of information.

C: TDE $Y CJM C               // Clears $Y
C+1: INC $Y $Z TDE $X CJM C+1 // Destructively copies $X to Y and Z
C+2: INC $X TDE $Z CJM C+2    // Destructively copies $Z to X

X are Y are the addresses of the sub-registers being copied from and to respectively. Z is the address of a free sub-register. Line C is the beginning of the copy loop.

Multiplication

With non-destructive copying, multiplication can be achieved.

C: TDE $V CJM M               // Stops the repeated addition if 0
C+1: JMP P
M: TDE $X CJM M               // M..M+2 safe-copies $U to X
M+1: INC $X $Y TDE $U CJM M+1
M+2: INC $U TDE $Y CJM M+2
M+3: INC $W TDE $X CJM M+3    // Adds to the product
M+4: JMP C
P: TDE $U CJM P               // Clears $U
P+1: INC $U TDE $W CJM P+1    // Destructively copies $W to U

U and V are the addresses of the factors. W, X, and Y are the addresses of free sub-registers. Line C is the check, line M is the beginning of the multiplication loop, and lines P and P+1 are the destructive copy loop.

Squares

Using the multiplication algorithm, finding the square of a number is trivial.

C-2: INC $V $W TDE $U CJM C-2 // Safe-copies $U to V
C-1: INC $U TDE $W CJM C-1
C: TDE $V CJM M               // Stops the repeated addition if 0
C+1: JMP P
M: TDE $X CJM M               // M..M+2 safe-copies $U to X
M+1: INC $X $Y TDE $U CJM M+1
M+2: INC $U TDE $Y CJM M+2
M+3: INC $W TDE $X CJM M+3    // Adds to the product
M+4: JMP C
P: TDE $U CJM P               // Destructively copies $W to U
P+1: INC $U TDE $W CJM P+1

U is the address of the value being squared. V, W, X, and Y are the addresses of free sub-registers. Lines C-2 and C-1 copy $U to V to allow for the multiplication.

Logic

Boolean AND

B: TDE $X $Y CJM T
B+1: JMP F
T: INC $X $Y JMP (wherever) // Undoes the division done at B

X and Y are the addresses of sub-registers. Line B checks if both sub-registers are nonzero. Line F is the beginning of execution when and($X $Y) returns false, T is the beginning of execution when and($X $Y) returns true.

Boolean OR

B: TDE $X CJM T-1
B+1: TDE $Y CJM T-2
B+2: JMP F
T-2: INC $Y JMP T // Undoes the division done at B+1
T-1: INC $X JMP T // Undoes the division done at B

X and Y are the addresses of sub-registers. Line B and B+1 check if either sub-register is nonzero. Line F is the beginning of execution when or($X $Y) returns false, T is the beginning of execution when or($X $Y) returns true.

X != Y

The subtraction algorithm can be combined with the boolean or algorithm to check if $X != $Y.

L: TDE $X $Y CJM L
B: TDE $X CJM T-1
B+1: TDE $Y CJM T-2
B+2: JMP F
T-2: INC $Y JMP T // Undoes the division done at B+1
T-1: INC $X JMP T // Undoes the division done at B

However, information is destroyed when subtracting.

X == Y

The subtraction algorithm can be combined with the flipped form of the boolean or algorithm to check if $X == $Y.

L: TDE $X $Y CJM L
B: TDE $X CJM F-1
B+1: TDE $Y CJM F-2
B+2: JMP T
F-2: INC $Y JMP F // Undoes the division done at B+1
F-1: INC $X JMP F // Undoes the division done at B

Line F is the beginning of execution when $X is not equal to $Y, T is the beginning of execution when $X is equal to $Y. Information is destroyed when subtracting.