aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 671c05d86f4d90f4f2dcae2ebd77c3211c4b3675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Fantasy Virtual Machine

A 64-bit stack-based virtual machine and its assembler, created for fun.

## Build

FVM includes two parts: the virtual machine and the assembler.

The build the assembler, you need to intall CHICKEN Scheme first. For example, on Arch Linux:

    sudo pacman -S chicken

Then install CHICKEN Scheme dependencies:

    sudo chicken-install matchable srfi-69 srfi-4

Then run make, which will build both components:

    make

If successful, you will see two executables, the virtual machine `fvm` and the assembler`fvm-as`.

## Run

After writing the assembly code (examples can be found in the `examples` directory), first run the assembler:

    ./fvm-as input.asm bytecode.bin

This will generate the bytecode file `output.bin`. Then run the virtual machine:

    ./fvm bytecode.bin

## Registers

FVM contains 4 registers:

- SP: stack pointer
- BP:base pointer
- PC:program counter
- RV: returned value

There are no general-purpose registers; all operations happen on the stack.

## Opcodes

All opcodes are single-byte, except for immediate value instructions.

The instructions supported by FVM are as follows:

### Register Instructions

**sp**
<br>0x00
<br>push stack pointer to stack

**ssp**
<br>0x01
<br>pop a 64-bit number from stack and set stack pointer to it.

**bp**

**sbp**

**pc**

**rv**

**srv**

### Immediate Number

**imm**
<br>
<br>

### Memory

**ld**

**ld8**

**ld16**

**ld32**


**st**

**st8**

**st16**

**st32**

### Stack Operations

**dup**

**pop**

**swap**

**over**

**rot**


## Call Frame


## Pseudo-Opcodes