forked from skx/go.vm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
710 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# | ||
# About | ||
# | ||
# This program adds two numbers. | ||
# | ||
# | ||
# Usage | ||
# | ||
# $ compiler ./add.in ; ./simple-vm ./add.raw | ||
# | ||
# | ||
# | ||
|
||
store #1, 10 | ||
store #2, 20 | ||
add #0, #1, #2 | ||
print_int #0 | ||
|
||
# add newline to the output | ||
store #1, "\n" | ||
print_str #1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# | ||
# About | ||
# | ||
# This program demonstrates that we can use subroutines. | ||
# | ||
# | ||
# Usage | ||
# | ||
# $ compiler ./call.in ; ./simple-vm ./call.raw | ||
# | ||
# | ||
# | ||
store #1, 15 | ||
call print | ||
|
||
store #1, 240 | ||
call print | ||
|
||
store #1, 255 | ||
call print | ||
|
||
exit | ||
|
||
|
||
# | ||
# This routine will print the integer stored in register #1 | ||
# | ||
# Then it will print a newline. | ||
# | ||
# Then it will return to the caller. | ||
# | ||
print_int #1 | ||
store #1, "\n" | ||
print_str #1 | ||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# | ||
# About | ||
# | ||
# This program compares the number in a register with a constant. | ||
# | ||
# | ||
# Usage | ||
# | ||
# $ compiler ./compare.in ; ./simple-vm ./compare.raw | ||
# | ||
# | ||
# | ||
|
||
store #1, 44 | ||
cmp #1, 44 | ||
jmpz equal | ||
|
||
store #1, "Inequal\n" | ||
print_str #1 | ||
exit | ||
|
||
:equal | ||
store #1, "EQUAL\n" | ||
print_str #1 | ||
|
||
|
||
store #1, "Steve" | ||
cmp #1, "Kemp" | ||
jmpnz str1 | ||
|
||
store #1, "Eek string 1 compare failed - BUG?\n" | ||
print_str #1 | ||
exit | ||
|
||
:str1 | ||
store #1, "Steve" | ||
cmp #1, "Steve" | ||
jmpz str2 | ||
|
||
store #1, "Eek string 2 compare failed - BUG?\n" | ||
print_str #1 | ||
exit | ||
|
||
|
||
:str2 | ||
store #1, "String comparisions OK\n" | ||
print_str #1 | ||
exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# | ||
# About | ||
# | ||
# Use a conditional jump to loop ten times, building up a string as we go. | ||
# | ||
# | ||
# Usage | ||
# | ||
# $ compiler ./concat.in ; ./simple-vm ./concat.raw | ||
# | ||
# | ||
# | ||
|
||
store #1, "Counting bananas" | ||
print_str #1 | ||
|
||
store #1, 10 | ||
store #2, 1 | ||
|
||
store #3, "" | ||
store #4, " banana" | ||
:repeat | ||
|
||
# | ||
# This means "reg1 = reg1 - reg2" | ||
# | ||
sub #1, #1, #2 | ||
|
||
concat #3, #3, #4 | ||
print_str #3 | ||
|
||
# add newline to the output | ||
store #5, "\n" | ||
print_str #5 | ||
|
||
# | ||
# This jump only occur if the zero-flag is set. | ||
# | ||
jmpnz repeat | ||
|
||
|
||
store #1, "Done" | ||
print_str #1 | ||
|
||
exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# | ||
# About | ||
# | ||
# This program decrements an integer. | ||
# | ||
# | ||
# Usage | ||
# | ||
# $ compiler ./dec.in ; ./simple-vm ./dec.raw | ||
# | ||
# | ||
# | ||
|
||
store #1, 3 | ||
store #0, "Stored in register #1 -> 3\n" | ||
print_str #0 | ||
|
||
|
||
dec #1 | ||
store #0, "Decremented contents of register #1 -> 2\n" | ||
print_str #0 | ||
jmpz empty | ||
|
||
dec #1 | ||
store #0, "Decremented contents of register #1 -> 1\n" | ||
print_str #0 | ||
jmpz empty | ||
|
||
dec #1 | ||
store #0, "Decremented contents of register #1 -> 0\n" | ||
print_str #0 | ||
jmpz empty | ||
|
||
dec #1 | ||
store #0, "Decremented contents of register #1 -> -1 [not reached]\n" | ||
print_str #0 | ||
jmpz empty | ||
|
||
:empty | ||
exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# | ||
# About | ||
# | ||
# This program tests our comparision operation. | ||
# | ||
# | ||
# Usage | ||
# | ||
# $ compiler ./equal.in ; ./simple-vm ./equal.raw | ||
# | ||
# | ||
# | ||
|
||
store #1, 20 | ||
store #2, 30 | ||
|
||
cmp #1, #2 | ||
jmpz twenty_eq_thirty | ||
|
||
store #2, 20 | ||
cmp #1, #2 | ||
jmpz twenty_eq_twenty | ||
|
||
exit | ||
|
||
:twenty_eq_twenty | ||
store #0, "20 == 20 - OK!\n" | ||
print_str #0 | ||
exit | ||
|
||
:twenty_eq_thirty | ||
store #0, "20 == 30 - Bug!\n" | ||
print_str #0 | ||
exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
store #1, "Hello, World!\n" | ||
print_str #1 | ||
exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# | ||
# About | ||
# | ||
# This program only outputs some static strings and an integer | ||
# | ||
# However it does prove that the goto/label handling is correct. | ||
# | ||
# | ||
# Usage | ||
# | ||
# $ compiler ./jump.in ; ./simple-vm ./jump.raw | ||
# | ||
# | ||
# | ||
|
||
store #1, "Steve Kemp\n" | ||
print_str #1 | ||
goto number | ||
|
||
# padding here - just for amusement. | ||
nop | ||
exit | ||
|
||
:kirsi | ||
store #1, "Kirsi Kemp\n" | ||
print_str #1 | ||
exit | ||
|
||
:number | ||
store #1, 32 | ||
print_int #1 | ||
|
||
# add newline to the output | ||
store #1, "\n" | ||
print_str #1 | ||
|
||
goto kirsi | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# | ||
# About | ||
# | ||
# Use a conditional jump to loop ten times, the conditional will trigger | ||
# if a "SUB" operation results in <0. | ||
# | ||
# | ||
# Usage | ||
# | ||
# $ compiler ./loop.in ; ./simple-vm ./loop.raw | ||
# | ||
# | ||
# | ||
|
||
store #1, "Counting from ten to zero\n" | ||
print_str #1 | ||
|
||
store #1, 11 | ||
store #2, 1 | ||
:repeat | ||
|
||
# | ||
# This means "reg1 = reg1 - reg2" | ||
# | ||
sub #1, #1, #2 | ||
print_int #1 | ||
|
||
# add newline to the output | ||
store #5, "\n" | ||
print_str #5 | ||
|
||
# | ||
# This jump only occur if the zero-flag is set. | ||
# | ||
jmpnz repeat | ||
|
||
|
||
store #1, "Done\n" | ||
print_str #1 | ||
|
||
exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# | ||
# About | ||
# | ||
# This program copies a bunch of memory about, then jumps to it. | ||
# | ||
# | ||
# Usage | ||
# | ||
# $ compiler ./memcpy.in ; ./simple-vm ./memcpy.raw | ||
# | ||
# | ||
# | ||
|
||
goto run | ||
:code | ||
|
||
# | ||
# This is the code we're going to copy and execute. | ||
# | ||
store #1, "Steve Kemp\n" | ||
print_str #1 | ||
store #1, "memcpy works\n" | ||
print_str #1 | ||
exit | ||
:code_end | ||
|
||
:run | ||
# | ||
# Copy the memory between `code` and `code_end` to 0x5000. | ||
# | ||
|
||
# First of all calcuate the length | ||
store #2, code | ||
store #3, code_end | ||
sub #3, #3, #2 | ||
|
||
# Show the length of the code we're copying | ||
# Remember the result is in #3. | ||
store #1, "Code length is " | ||
print_str #1 | ||
print_int #3 | ||
store #1, "\n" | ||
print_str #1 | ||
|
||
# setup the copy and run it | ||
store #1, 0x5000 | ||
store #2, code | ||
memcpy #1, #2, #3 | ||
|
||
# Jump to the copied code. | ||
goto 0x5000 |
Oops, something went wrong.