PHP code example of superinstance / flux-vm

1. Go to this page and download the library: Download superinstance/flux-vm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

superinstance / flux-vm example snippets



use SuperInstance\FluxVM\FluxVM;
use SuperInstance\FluxVM\Assembler;
use SuperInstance\FluxVM\Loader;

$vm = new FluxVM();

// Load bytecode from file
$bytecode = Loader::load('program.flux');
$vm->load($bytecode);
$vm->run();

// Or assemble from source
$source = '
    # Compute factorial of 5
    IMov R0, 5      # counter
    IMov R1, 1      # accumulator
loop:
    IMul R1, R1, R0 # R1 = R1 * R0
    IDec R0, 1      # R0--
    JumpIfNot R0, loop  # if R0 != 0, jump to loop
    Halt            # done
';
$bytecode = Assembler::assemble($source);
$vm->load($bytecode);
$vm->run();

echo "Result: " . $vm->getGP(1) . "\n"; // 120 (5!)
bash
git clone https://github.com/SuperInstance/flux-vm-php.git
cd flux-vm-php
composer install
bash
# Run bytecode
php src/CLI.php run program.flux

# Assemble assembly to bytecode
php src/CLI.php asm program.asm

# Disassemble bytecode
php src/CLI.php dis program.flux

# Start REPL
php src/CLI.php repl
bash
php src/CLI.php repl
flux> asm IMov R8, 5; IMov R9, 3; IAdd R7, R8, R9; Halt
flux> regs
flux> quit

0000  20 00 07 00     IMov R0, 7
0004  20 01 01 00     IMov R1, 1
0008  23 01 01 00     IMul R1, R1, R0
000C  29 00 01 00     IDec R0, 1
0010  05 00 F8 FF     JumpIfNot R0, -8
0014  00              Halt