Crates.io | pure_stack_machine |
lib.rs | pure_stack_machine |
version | 0.1.0 |
source | src |
created_at | 2024-03-03 12:56:14.756691 |
updated_at | 2024-03-03 12:56:14.756691 |
description | Pure stack machine. |
homepage | |
repository | https://github.com/georgiyozhegov/stack_machine.git |
max_upload_size | |
id | 1160587 |
size | 3,845,713 |
Pure stack machine. Small number of instructions. There are currently 20 instructions.
cargo build --release
cp target/release/stack_machine stack_machine
./stack_machine <path>
Code | Instruction | Pop | Top | Push | Description |
---|---|---|---|---|---|
0 | load |
- | - | value |
loads literal value |
1 | add |
a , b |
- | result |
result = a + b |
2 | sub |
b , a |
- | result |
result = a - b |
3 | mul |
a , b |
- | result |
result = a * b |
4 | div |
b , a |
- | result |
result = a / b |
5 | read |
mode |
- | input |
reads char (mode = 0 ) or string (mode = 1 ) from stdin |
6 | put |
format |
value |
- | prints value to stdout as a number (format = 0 ), char (format = 1 ) or bits (format = 2 ) |
7 | and |
a , b |
- | result |
result = a & b |
8 | or |
a , b |
- | result |
result = a | b |
9 | xor |
a , b |
- | result |
result = a ^ b |
10 | not |
value |
- | result |
result = !value |
11 | cmp |
b |
a |
result |
if a > b : result = 1 else if a < b : result = 2 else result = 0 |
12 | jmp |
- | - | - | jumps to #label |
13 | jeq |
value |
- | - | jumps to #label if value = 0 |
14 | jne |
value |
- | - | jumps to #label if value != 0 |
15 | jgr |
value |
- | - | jumps to #label if value = 1 |
16 | jle |
value |
- | - | jumps to #label if value = 2 |
17 | copy |
- | value |
value |
makes copy of value |
18 | drop |
value |
- | - | just drops value |
19 | halt |
- | - | - | stops executing |
2 + 2
#start ; '#start' label is required
load 2 ; stack = [2]
load 2 ; stack = [2, 2]
add ; stack = [4]
2 + 2 + 2
#start
load 2 ; stack = [2]
load 2 ; stack = [2, 2]
add ; stack = [4]
load 2 ; stack = [4, 2]
add ; stack = [6]
infinite loop
#start
jmp #start
condition
#equal
load "e"
load 1
put
halt
#notequal
load "n"
load 1
put
halt
#start ; program starts here!
load 2
load 1
cmp
jeq #equal
jmp #notequal ; jumps to '#notequal' because 2 != 1
Add more instructions.