This is also a very very old post. It’s mostly here on this blog for personal reasons and entertainment puroses.
This is a migrated post from Wordpress so the formatting is sub optimal
[caption id=”” align=”aligncenter” width=”360”] Ernst-Hugo Järegård being awesome.[/caption]
I have been spending the entire weekend writing a assembler in Java for my automaton. I felt that I needed to give it a name while I worked on it so I have decided to call it the Ernst-Hugo Automaton after the Swedish actor and awesome person Ernst-Hugo Järegård. I have used the Automaton package that I wrote earlier with some changes. I had to change some of the scoping of the classes to make them work with the new Assembler package.
The Assembler class is a very ugly class which contains a constructor which acts as a main loop where the basic input parsing occurs. Most of the methods in the assembler class handles the parsing of the input but it also handles what is to be sent to be assembled and the running of the program. Valid assembly code is then sent from the Assembler class to the Program class when the assemble method in Program is called. The Program class is the program that you are working on. It contains both the assembly code for the program and the assembled program(given that it has been assembled). The Program class also contains the assemble method and the other methods needed for the assembling of the program. I know I should put this in a separate class... but I didn't.
I have tried to write good looking code but I know that especially the Assembler class is mighty ugly. I have also tried to implement some form of exception handling. There is also a somewhat extensive documentation of the classes used. I know that the documentation is rather incoherent and does not really adhere to any specific formatting convention but I guess its better than nothing.
As of now the assembler does not implement that many features apart from some very basic editing of the input. It would be very nice to add things such as the ability to load and save programs. Right now the assembler cant handle any functions except for a function to add Jump lines.
The Automaton class contains some rudimentary unit testing methods. I really only wrote those for educational purposes and they can be pretty much disregarded but they are included just for fun.
The documentation can be found here.
The JAR containing the actual program and the source code can be found here. To run the program you will need to have a ANSI compatible command line interface or the program will look really stupid. It would not be that difficult to re factor it to normal output not using ANSI formatting. Just rewrite the AnsiDrawer class. It shouldn't be that hard.
The operation of the assembler is pretty basic. The code is simply inputted using the symbols : ( 0 , 1 , . , ! , + , x , & , < , > ). Which are the same as specified in previous posts although the symbols have been changed to ASCII equivalents.. The index (row) where the code will be added is shown before the ":" on each line. The index can be set by the commands which will be specified in detail later.
The jump command is implemented in this assembler and is typed as "#jump:pos:target". Only one Jump will be added on a line and the rest of the line will be padded with Drop. You can add a Jump command with the target lying outside of the program. But when you assemble the program all Jump operators has to point to a line in the assembly code. Remember that all of the jump targets are absolute! So inserting lines in your program can screw up your Jumps.
All of the commands are preceded with a “§” symbol and are as follows:
new Prompts the user for the memory size of the program and then creates a new Program instance. This has to be done before any code can be written. Calling new again will over ride any code or assembled program.
run Runs the program given that it has been assembled. Prompts the user for a starting configuration of the memory. The program will run for a maximum of 200 steps in order to avoid infinite loops.
print Prints out the assembly code.
assemble Assembles the program. Will fail if any Jump targets lies outside of the program length. Prints the assembled code when done.
clear Clears the screen.
help Prints a help text.
set n Will set the index to n. The index will then increment from n when a line of code is entered. All lines after n will be pushed down. Using set will screw up your Jumps if you are not careful.
replace n Replaces line n with the next line of code entered. The index then returns to the end of the assembly code.
reset Resets the index to the end of the assembly code
quit quits the program.
Here follows a little usage example. It is the 1 bit adder that I showed in the first post about this automaton. This example should give you a little better understanding of how the assembler works.
0:§new Enter the memory size of the program: 5 You may now begin coding! 0:..... 1:.<.>. 2:<>..x 3:...<. 4:.<>.. 5:.&<>. 6:...&x 7:..>.. 8:...+. 9:§print 0: ..... 1: .<.>. 2: <>..x 3: ...<. 4: .<>.. 5: .&<>. 6: ...&x 7: ..>.. 8: ...+. 9:§compile The command is silly! Use a proper one! 9:§assemble Compiling: default 0: DROP DROP DROP DROP DROP 1: DROP CLONE_RIGHT DROP CLONE_LEFT DROP 2: CLONE_RIGHT CLONE_LEFT DROP DROP XOR 3: DROP DROP DROP CLONE_RIGHT DROP 4: DROP CLONE_RIGHT CLONE_LEFT DROP DROP 5: DROP AND CLONE_RIGHT CLONE_LEFT DROP 6: DROP DROP DROP AND XOR 7: DROP DROP CLONE_LEFT DROP DROP 8: DROP DROP DROP OR DROP It would appear as if the program has compiled without any major trouble! 9:§run Please enter the starting configuration 00101 00101 0 00101 1 00101 2 01111 3 10110 4 10100 5 11000 6 11000 7 11000 8 11100 G 11110 Done! 9:
I have been thinking a bit about writing some form of language for the system capable of some more complicated operations. I have figured out a way of creating a addressable memory space with the Ernst-Hugo automaton which would enable more advanced programming using a fixed but arbitrary word length. Although creating a new language would require a lot of effort to implement but it would be really fun to be able to write more complicated programs for it. But that depends on the interest from you folks. If you really want me to write one I would definitely do it or if anyone of you would like to do that you are more than welcome! Just email me if you have any questions about the code or want to start a collaborative project with the Ernst-Hugo Automaton!