Java-implemented Hack language assembly compiler-the sub-device part

Source: Internet
Author: User

Java-implemented Hack language assembly compiler-the sub-device part

I recently read computer system elements and read some basic things. I found that it would be nice to read this book before the university course compilation. Although some of it is difficult, it is still possible to browse it. After reading this article, you will know how to use this compilation item. The same is true for model-based electricity. At the beginning, I didn't know where the and or door came from, and I didn't know how to use it.


In the first five chapters of this book, we are talking about the hardware platform and starting from the most basic NAND door to construct other doors for you. To the Boolean logic, you can build your own multiplier. The input selector also has a multi-bit selector. Next is the time series logic, about the clock cycle. The next step is the assembly language, which is different from the assembly of 8086, but very similar. Basically, I joined my sophomore year and freshman year. I saw this book after graduation, so I haven't graduated yet. If you just saw this, I suggest you read it, even if you don't do exercises in each chapter. I think it will be of great benefit after the first time.


A bad thing about the book is that you don't know how to express answers to some exercises even though you know your ideas. Like those in the array.


This student writes well, http://book.douban.com/review/7115224/


Some of the exercises in the book are written by a relatively young foreigner in Chicago, and some are written by this netizen.


In chapter 6, I am very interested in writing an assembly compiler. After all, I have never learned how to compile. The Github code of netizens is implemented in Python.
Then I thought about it myself and implemented it in Java. First, Add. asm is compiled based on the Assembly file provided by the exercise.


Hack is a 16-bit computer based on the von noriman architecture. The Hack language contains two Commands: A-instruction and C-instruction.


For more information, see Chapter 4.


The basic idea is. Filter out the // annotated code. Binary Conversion is performed based on the identifier @ of the command.
It is also converted according to the form of the C command.
Commands have a special relationship with binary numbers.


So write it into the properties file.


Java code:

Import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. util. arrayList; import java. util. enumeration; import java. util. list; import java. util. properties; /*** @ author Caikc * @ description * @ date 3:52:32 * @ company www.ewan.cn */public class Assembly {/*** get keyvalue from compile file * @ param key * @ return * @ throws IOException */public static String getcompile (String key) throws IOException {InputStream ips = Assembly. class. getResourceAsStream ("compile. properties "); Properties p = new Properties (); p. load (ips);/** Enumeration e = p. propertyNames (); while (e. hasMoreElements () {* if (key. equals (String) e. nextElement () {* System. out. println (p. get (key) ;}} */return (String) p. get (key);} public static String getdest (String key) throws IOException {InputStream ips = Assembly. class. getResourceAsStream ("dest. properties "); Properties p = new Properties (); p. load (ips); return (String) p. get (key);} public static String getjump (String key) throws IOException {InputStream ips = Assembly. class. getResourceAsStream ("jump. properties "); Properties p = new Properties (); p. load (ips); return (String) p. get (key);}/*** read lines of file * and replace the annotation * @ param filename * @ throws IOException */public void getfile (String filename) throws IOException {BufferedReader bfr = new BufferedReader (new InputStreamReader (new FileInputStream (new File (filename); String s = ""; List
 
  
List = new ArrayList
  
   
(); While (s = bfr. readLine ())! = Null) {s = s. trim (); int index = s. indexOf ("//"); if (! S. startsWith ("//")&&! S. equals ("") {if (index! =-1) {s = s. substring (0, index);} list. add (s) ;}for (String string: list) {// System. out. println (string); if (string. indexOf ("@")! =-1) {System. out. println (Assembly. tohexstring (string);} else {System. out. println (Assembly. compileCInstruction (string); }}/ *** number to 16 bit binary string * last bit 0 represent positive, 1 represent negative. * @ order compile to binary string * @ param s * @ return */public static String tohexstring (String s) {String hex = ""; if (s. startsWith ("@") {s = s. replace ('@',''). trim (); String temp = Integer. ToBinaryString (Integer. valueOf (s. replace ('-',''). trim (); int n = temp. length (); while (n <16) {if (n = temp. length () {if (Integer. valueOf (s)> = 0) {hex + = "0";} else {hex + = "1" ;}} else {hex + = "0 ";} n + = 1;} hex + = temp;} else {hex = s;} return hex;}/*** compile C * dest = comp; jump * dest and jump cocould be null, if dest is null, left out the = * if jump is null, left out the; * 1 1 a c1-c6 d1- D3 j1-j3 * @ throws IOException */public static String compileCInstruction (String line) throws IOException {String assembcode = "111"; int indexdest = line. indexOf ("="); int indexjump = line. indexOf (";"); if (indexdest! =-1 & indexjump! =-1) {String dest = line. substring (0, indexdest); String cmp = line. substring (indexdest, indexjump); String jmp = line. substring (indexjump + 1, line. length (); // System. out. println (dest + "" + cmp + "" + jmp); String cmpcode = Assembly. getcompile (cmp. trim (); String destcode = Assembly. getdest (dest. trim (); String jumpcode = Assembly. getjump (jmp. trim (); assembcode = assembcode + cmpcode + destcode + jumpcode;} else if (ind Exdest! =-1 & indexjump =-1) {String dest = line. substring (0, indexdest); String cmp = line. substring (indexdest + 1, line. length (); // System. out. println (dest + "" + cmp); String cmpcode = Assembly. getcompile (cmp. trim (); String destcode = Assembly. getdest (dest. trim (); // System. out. println ("com:" + cmpcode); // System. out. println ("dest:" + destcode); String jumpcode = "000"; assembcode = assembcode + cmpcode + destcode + jumpcode;} Else if (indexdest =-1 & indexjump! =-1) {String jmp = line. substring (indexjump + 1, line. length (); // System. out. println (jmp); String cmpcode = "000000"; String destcode = "000"; String jumpcode = Assembly. getjump (jmp. trim (); assembcode = assembcode + cmpcode + destcode + jumpcode;} return assembcode;} public static void main (String [] args) throws IOException {/** Assembly a = new Assembly (); try {. get ();} catch (IOException e) {* e. printStackTrace ();} * // System. out. println (new Assembly (). getcompile ("0"); // System. out. println (new Assembly (). tohexstring ("9"); new Assembly (). getfile ("C:/Users/Administrator/Desktop/nand2tetris (1)/nand2tetris/projects/06/add/Add. asm ");}}
  
 

Compile. properties

0=01010101=0111111-1=0111010D=0001100A=0110000!D=0001101!A=0110001-D=0001111-A=0110011D+1=0011111A+1=0110111D-1=0001110A-1=0110010D+A=0000010D-A=0010011A-D=0000111D&A=0000000D|A=0010101M=1110000,!M=1110001-M=1110011M+1=1110111M-1=1110010D+M=1000010D-M=1010011M-D=1000111D&M=1000000D|M=1010101

Dest. properties

null=000M=001D=010MD=011A=100AM=101AD=110AMD=111

Jump. properties

null=000M=001D=010MD=011A=100AM=101AD=110AMD=111

The final output will Add. asm

// This file is part of www.nand2tetris.org// and the book "The Elements of Computing Systems"// by Nisan and Schocken, MIT Press.// File name: projects/06/add/Add.asm// Computes R0 = 2 + 3@2D=A //dgg@3D=D+A@0M=D

Convert to binary code

000000000000001011101100000100000000000000000011111000001001000000000000000000001110001100001000


Because I still don't understand some questions when writing exercises, I just added a group of Douban. We found that although the age in the group is large. However, I still keep my interest in learning. We also use FPGA to implement the operating system. If I started this way in my sophomore year, maybe I am an embedded development engineer now.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.