130 lines
2.7 KiB
Markdown
130 lines
2.7 KiB
Markdown
|
# Bytecode
|
||
|
|
||
|
TODO
|
||
|
|
||
|
## Example
|
||
|
|
||
|
Let's consider a simple algorithm that tests the [Collatz conjecture](collatz_conjecture.md) (which says that applying a simple operation from any starting number over and over will always lead to number 1). The algorithm in [C](c.md) would look as follows:
|
||
|
|
||
|
```
|
||
|
// Collatz conjecture
|
||
|
#include <stdio.h>
|
||
|
|
||
|
int next(int n)
|
||
|
{
|
||
|
return n % 2 ? // is odd?
|
||
|
3 * n + 1 :
|
||
|
n / 2;
|
||
|
}
|
||
|
|
||
|
int main(void)
|
||
|
{
|
||
|
int n = getchar() - '0'; // read input ASCII digit
|
||
|
|
||
|
while (1)
|
||
|
{
|
||
|
printf("%d\n",n);
|
||
|
|
||
|
if (n == 1)
|
||
|
break;
|
||
|
|
||
|
n = next(n);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
```
|
||
|
|
||
|
The program reads a number (one digit for simplicity) and then prints the sequence until reaching the final number 1. Now let's rewrite the same algorithm in [comun](comun.md), a language which will allow us to produce bytecode:
|
||
|
|
||
|
```
|
||
|
# Collatz conjecture
|
||
|
|
||
|
next:
|
||
|
$0 2 % ? # is odd?
|
||
|
3 * 1 +
|
||
|
;
|
||
|
2 /
|
||
|
.
|
||
|
.
|
||
|
|
||
|
<- # read input ASCII digit
|
||
|
"0" - # convert it to number
|
||
|
|
||
|
@@
|
||
|
# print:
|
||
|
$0 10 / "0" + ->
|
||
|
$0 10 % "0" + ->
|
||
|
10 ->
|
||
|
|
||
|
$0 1 = ?
|
||
|
!@
|
||
|
.
|
||
|
|
||
|
next
|
||
|
.
|
||
|
```
|
||
|
|
||
|
The bytecode this compiles to is following:
|
||
|
|
||
|
```
|
||
|
000000: DES 00 0111 # func
|
||
|
000001: JMA 00 0100... # 20 (#14)
|
||
|
000002: COC 00 0001
|
||
|
000003: MGE 00 0000
|
||
|
000004: CON' 00 0010 # 2 (#2)
|
||
|
000005: MOX 00 0000
|
||
|
000006: DES 00 0001 # if
|
||
|
000007: JNA 00 0000... # 16 (#10)
|
||
|
000008: COC 00 0001
|
||
|
000009: CON' 00 0011 # 3 (#3)
|
||
|
00000a: MUX 00 0000
|
||
|
00000b: CON' 00 0001 # 1 (#1)
|
||
|
00000c: ADX 00 0000
|
||
|
00000d: DES 00 0010 # else
|
||
|
00000e: JMA 00 0011... # 19 (#13)
|
||
|
00000f: COC 00 0001
|
||
|
000010: CON' 00 0010 # 2 (#2)
|
||
|
000011: DIX 00 0000
|
||
|
000012: DES 00 0011 # end if
|
||
|
000013: RET 00 0000
|
||
|
000014: INI 00 0000
|
||
|
000015: INP 00 0000
|
||
|
000016: CON' 00 0000... # 48 (#30)
|
||
|
000017: COC 00 0011
|
||
|
000018: SUX 00 0000
|
||
|
000019: DES 00 0100 # loop
|
||
|
00001a: MGE 00 0000
|
||
|
00001b: CON' 00 1010 # 10 (#a)
|
||
|
00001c: DIX 00 0000
|
||
|
00001d: CON' 00 0000... # 48 (#30)
|
||
|
00001e: COC 00 0011
|
||
|
00001f: ADX 00 0000
|
||
|
000020: OUT 00 0000
|
||
|
000021: MGE 00 0000
|
||
|
000022: CON' 00 1010 # 10 (#a)
|
||
|
000023: MOX 00 0000
|
||
|
000024: CON' 00 0000... # 48 (#30)
|
||
|
000025: COC 00 0011
|
||
|
000026: ADX 00 0000
|
||
|
000027: OUT 00 0000
|
||
|
000028: CON' 00 1010 # 10 (#a)
|
||
|
000029: OUT 00 0000
|
||
|
00002a: MGE 00 0000
|
||
|
00002b: CON' 00 0001 # 1 (#1)
|
||
|
00002c: EQX 00 0000
|
||
|
00002d: DES 00 0001 # if
|
||
|
00002e: JNA 00 0100... # 52 (#34)
|
||
|
00002f: COC 00 0011
|
||
|
000030: DES 00 0101 # break
|
||
|
000031: JMA 00 1000... # 56 (#38)
|
||
|
000032: COC 00 0011
|
||
|
000033: DES 00 0011 # end if
|
||
|
000034: CAL 00 0011 # 3 (#3)
|
||
|
000035: DES 00 0110 # end loop
|
||
|
000036: JMA 00 1010... # 26 (#1a)
|
||
|
000037: COC 00 0001
|
||
|
000038: END 00 0000
|
||
|
```
|
||
|
|
||
|
TODO: analyze the above, show other bytecodes (python, java, ...)
|