## MINIL Programs

Here's a selection of simple MINIL programs for the Tiny Machine-Code Monitor, with an explanation of each program in pseudocode:

Greatest Common Divisor: finds the largest number which divides into two given numbers.

Highest prime factor: finds the largest prime number that divides into a given number.

X^Y: raises X to the power Y.

Takeuchi function: a highly recursive function used for computer benchmarks.

Mystery function: what's the algorithm?

This is the MINIL version of the classic Arduino Blink program:

```do
toggle LED
do
t ← t - 1
while t ≠ 0
while true```

#### MINIL program

```00 66 Blink: TOG
01 1D Loop:  DEC R1
02 A1        JNZ Loop

### Greatest Common Divisor

Prompts for two numbers, and then displays the greatest common divisor (GCD) of the numbers. For example, entering 3287 and 3460 will display 173.

#### Pseudocode

```input a, b
do
x ← a
a ← b
do
b ← x
x ← x - a
while x > 0
while x ≠ 0
print b```

#### MINIL program

```00 1E  GCD:  ENT R1
01 2E        ENT R2
02 01  Swap: MOV R0,R1
03 12        MOV R1,R2
04 20  Again:MOV R2,R0
05 1B  Minus:SUB R1
06 C2        JC  Swap
07 A4        JNZ Again
08 2E        ENT R2```

### Highest prime factor

Prompts for a number, and displays the highest prime factor of that number. For example, 9999 gives 101.

#### Pseudocode

```input n
do
c ← n
for c-1 > d > 1 do
t ← d
do
t ← t - d
while t > 0
if t = 0 then
break
end if
end for
n ← d
while d > 1
print n```

#### MINIL program

Here's the MINIL program:

```00 1E  Go:   ENT R1
01 31  Not:  MOV R3,R1
02 23  New:  MOV R2,R3
03 2D  Fail: DEC R2
04 01  Next: MOV R0,R1
05 2B  Loop: SUB R2
06 C3        JC Fail
07 A5        JNZ Loop
08 12        MOV R1,R2
09 2D        DEC R2
0A A1        JNZ Not
0B 3E  Done: ENT R3```

### X^Y

Prompts for two numbers, X and Y, and returns X^Y. For example entering 19 and 3 displays 6859.

#### Pseudocode

```input x, y
z ← x
while y > 1
y ← y - 1
n ← z
z ← 0
for 0 ≤ i < n
z ← z + x
end for
end while
print z```

#### MINIL program

```00 1E Power: ENT R1
01 2E        ENT R2
02 01        MOV R0,R1
03 2D More:  DEC R2
04 A6        JNZ Loop
05 10        MOV R1,R0
06 80 Loop:  JZ  Power
07 30        MOV R3,R0
08 0C        CPY #0
0B 3D        DEC R3
0C A9        JNZ Again
0D 83        JZ  More```

### Takeuchi function

This is a classic recursive benchmark for comparing computer languages, originally used by Ikuo Takeuchi of Japan, and described in the book Performance and Evaluation of Lisp Systems .

#### Pseudocode

Here's the pseudocode:

```function tak(x,y,z)
if y >= x then
return z
else
return tak(tak(x-1,y,z), tak(y-1,z,x), tak(z-1,x,y))
end if
end function```

A good set of parameters is:

`(tak 18 12 6)`

which should give the result 7.

#### MINIL program

Here's the program:

```00 1E  Go:   ENT R1 ; x
01 2E        ENT R2 ; y
02 3E        ENT R3 ; z
03 E5        JSR Tak
04 0E        ENT R0
05 02  Tak:  MOV R0,R2
06 1B        SUB R1
07 CA        JC Less
08 03        MOV R0,R3
09 77        RTS
0A 18  Less: PSH R1
0B 28        PSH R2
0C 38        PSH R3
0D 1D        DEC R1
0E E5        JSR Tak
0F 29        POP R2
10 19        POP R1
11 39        POP R3
12 08        PSH R0
13 38        PSH R3
14 18        PSH R1
15 28        PSH R2
16 1D        DEC R1
17 E5        JSR Tak
18 19        POP R1
19 39        POP R3
1A 29        POP R2
1B 1D        DEC R1
1C 08        PSH R0
1D E5        JSR Tak
1E 30        MOV R3,R0
1F 29        POP R2
20 19        POP R1
21 E5        JSR Tak
22 77        RTS```

### Mystery function

The final function is a mystery function. For an input of 6 it gives the result 1957. What's the algorithm?

#### MINIL program

Here's the MINIL program:

```00 1E Go:    ENT R1
01 E3        JSR Fun
02 0E        ENT R0
03 89 Fun:   JZ  One
04 18 Swap:  PSH R1
05 1D        DEC R1
06 E3        JSR Fun
07 20        MOV R2,R0
08 19        POP R1
09 1C One:   CPY #1
0A 1D Loop:  DEC R1
0B CE        JC RET