Heyo, I got distracted on friday and wrote a compiler for my (not yet complete) processor. There are still a few bugs (like needing a \n at the end of the file, and only outputting to the console) but it is now compiling everything properly. I will post it once I give it some polish
for now, here's the fibonacci sequence in assembly to show the capabilities of the compiler:
(I know having an if statement in fibonacci is stupid, but it's there for illustrative purposes XP)
Oh if you're interested in how I made the compiler, it's written in Python, and reads the source file one character at a time in two passes: once to log the macro/meta stuff, and then again for a proper compile and error checking.
for now, here's the fibonacci sequence in assembly to show the capabilities of the compiler:
(I know having an if statement in fibonacci is stupid, but it's there for illustrative purposes XP)
Code:
# Fibonacci sequence in assembly code.
###############
# MACROS #
###############
# Use the ! symbol to name a constant.
!overflow = $111 # labeling the address for the overflow flag bit
# for if statements.
# The $ symbol marks a 3-bit argument.
!x = $\1 # register one being named as x
!y = $\2 # register two being named as y
# Using a backslash will tell the compiler
# to convert a number to binary. It will know
# how many bits to convert to, and will tell
# you at compile time if the number is above
# the bit limit.
###############
# ASSEMBLY #
###############
# The @ symbol marks an 8-bit immediate.
con @\0 # load constant 0 to reg a
con @\1 # load constant 1 to reg b
# The ? marker refers to its assigned constant.
add ?x # reg x = a + b
regR ?x # a = reg x
add ?y # reg y = a + b
# The > marker is an address pointer. It assigns
# the address of the following instruction to the
# constant name next to it. The constant can be
# referenced by any instruction that takes an
# 8-bit immediate. It is most useful for branching
# statements, obviously.
>loopStart
regR ?x # a = reg x
regR ?y # b = reg y
add ?x # reg x = a + b
ifT ?overflow @00000000 # restarts program if overflow detected.
regR ?y # a = reg y
regR ?x # b = reg x
add ?y # reg y = a + b
ifT ?overflow @00000000 # restarts program if overflow detected.
jump ?loopStart # otherwise loop. Essentially an if/else statement.
Oh if you're interested in how I made the compiler, it's written in Python, and reads the source file one character at a time in two passes: once to log the macro/meta stuff, and then again for a proper compile and error checking.