## Sample MINIL Programs

Here are some sample MINIL programs to illustrate the operation of the MINIL Machine-Code Monitor.

### Pulsating light

This is MINIL's answer to the Arduino Blink program. It smoothly pulsates the LED on and off:

```// Pulsating light
00 0E  Start: ENT  R0
01 1C         CLR  R1
02 1B  Up:    BRI  R1
04 0D         DEC  R0
05 C2         JNZ  Up
06 1B  Down:  BRI  R1
08 1D         DEC  R1
09 C6         JNZ  Down
0A 82         JZ   Up```

To run this enter 0255 and press Enter.

### Raise x to the y

This routine finds x^y:

```// Find x^y
00 0E  Start: ENT  R0   // x
01 1E         ENT  R1   // y
02 20         R2 = R0
03 1D  Nexty: DEC  R1
04 C6         JNZ  Power
05 02         R0 = R2
06 80  Power: JZ   Start
07 32         R3 = R2
08 2C         CLR  R2
09 40  Timesx:R4 = R0
0B 4D         DEC  R4
0C CA         JNZ  Inc
0D 3D         DEC  R3
0E C9         JNZ  Timesx
0F 83         JZ   Nexty```

For example, entering 7 and 3 will display 343.

### Greatest Common Divisor

This program uses Euclid's algorithm to find the greatest common divisor of two numbers:

```// Greatest common divisor
00 0E  GCD:   ENT  R0
01 1E         ENT  R1
02 21  Again: R2 = R1
03 10  Loop:  R1 = R0
04 02         R0 = R2
05 2D  Minus: DEC  R2
06 8A         JZ   Stop
07 1D         DEC  R1
08 C5         JNZ  Minus
09 83         JZ   Loop
0A 1D  Stop:  DEC  R1
0B C2         JNZ  Again
0C 80         JZ   GCD   // Display GCD in R0```

For example, if you enter 111 and 185 you'll get 37.

### Highest prime factor

This program prompts for a number, and then finds the highest prime factor of that number.

```// Highest Prime Factor
00 0E  Go:    ENT  R0
01 30  Not:   R3 = R0
02 23  New:   R2 = R3
03 10  Next:  R1 = R0
04 1D  Loop:  DEC  R1
05 83         JZ   Next
06 2D         DEC  R2
07 C4         JNZ  Loop
08 0D         DEC  R0
09 1D         DEC  R1
0A C2         JNZ  New
0B 20         R2 = R0
0C 2D         DEC  R2
0D C1         JNZ  Not
0E 3E         ENT  R3   // Display result
0F C0         JNZ  Go
10 80         JZ   Go   // Return```

This program is particularly slow; for example, on a 1MHz ATtiny85 it takes about 4 minutes to find the highest prime factor of 407, 37!