Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

An assembler for the Hack assembly language, built as a maven project

NotificationsYou must be signed in to change notification settings

KBlischke/hasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

1 Description
2 Usage
3 Dependencies
4 Implementation
    4.1 Parser
    4.2 Coder
    4.3 SymbolTable
    4.4 Main

1 Description

The HASM program is an assembler for the Hack assembly language. It was created as part of the coursenand2tetris and the bookThe Elements of Computing Systems: Building a Modern Computer from First Principles. The specification for the Hack assembly language can be found inchapter 4 and the description for the project inchapter 6 of the subsequent book.

The program is build as a Maven project and follows its according conventions. All source files are inside the directorysrc/main/java and all test source files inside the directorysrc/test/java. The compiled class files can be found inside the directorysrc/target/classes for the main program and inside the directorysrc/target/test-classes for the test files. The compiled jar file of the program is located inside the directorysrc/target.

2 Usage

The HASM assembler can be used inside the console with the command:
java -jar <programName>.jar <inputFileName>.asm [<outputFileName>.hack]

<programName>.jar is the name of the program. It is calledhasm-1.0.0-SNAPSHOT.jar and positioned inside the directorysrc/target per default, but can be renamed and repositioned in any way, as long as the suffix.jar isn't changed.

<inputFileName>.asm is the name of the Hack assembly file to be assembled. A relative path to that assembly file can be provided. The process failes if the provided file doesn't use the suffix.asm.

[<outputFileName>.hack] is the name that should be provided to the assembled file. A relative path to the destination of that file can be provided. The process failes if the provided file doesn't use the suffix.hack. This argument can be omitted, which will result in the creation of a file named like the provided assembly file and inside the same location.

If no arguments or more than two arguments are given to the program, the process failes.

If the provided assembly file does contain syntax invalid to the Hack assembly language specification, the process will be aborted and an error message will be produced. This error message contains a warning, the line number of the detected invalid syntax and the invalid syntax itself.

3 Dependencies

  • Java 17 or higher
  • Maven 3.9 or higher (Optional:only for building the project)
  • JUnit 5.10 or higher (Optional:only for testing the project)

4 Implementation

The implementation of the HASM assembler follows theAPI specification given inside the course material. Some additions and minor changes were made for the sake of better usability and debugging.

The API contains several modules containing routines. These are realized as classes with according methods.

4.1 Parser

TheParser module breaks every Hack assembly instruction into its underlying fields. It's realized as follows:

MethodArgumentsReturnDescriptionComment
constructorStringParserReturns a new parser object with argument to parseNot in the original API
constructorString[]ParserReturns a new parser objec with argument to parseNot in the original API
constructorFileParserReturns a new parser object with argument to parse-
getCurrentLinevoidintReturns the current line in the Parser objectNot in the original API
getCurrentInstructionvoidStringReturns the current instruction in the Parser objectNot in the original API
getInstructionsvoidString[]Returns all instructions passed to the constructorNot in the original API
hasMoreInstructionsvoidbooleanChecks if the last instruction hasn't been reachedCalled "hasMoreCommands" in the original API
advancevoidvoidAdvances the current instruction to the next instruction-
setCurrentintvoidSets the current instruction to the line passed by argument-
getInstructionTypevoidStringReturns the instruction type of the current instruction. The following returns are possible:
  • "A_INSTRUCTION"
  • "C_INSTRUCTION"
  • "LABEL"
  • "EMPTY"
  • null
Called "commandType" in the original API. "A_INSTRUCTION" is called "A_COMMAND" in the original API. "C_INSTRUCTION" is called "C_COMMAND" in the original API. "EMPTY" is not in the original API.
getSymbolvoidStringReturns the symbol or address inside the current A-instruction or labelCalled "symbol" in the original API
getDestvoidStringReturns the mnemmonic of the destination field inside the current C-instructionCalled "dest" in the original API
getCompvoidStringReturns the mnemmonic of the computation field inside the current C-instructionCalled "comp" in the original API
getJumpvoidStringReturns the mnemmonic of the jump field inside the current C-instructionCalled "jump" in the original API

4.2 Coder

TheCoder module translates Hack assembly mnemonics into Hack machine instructions. It's realized as follows:

MethodArgumentsReturnDescriptionComment
constructorvoidCoderReturns a new Coder objectRedundant because its methods are all static. Not in the original API
getDestStringStringReturns the machine instruction for the mnemonic of a Hack assembly destination fieldCalled "dest" in the original API
getCompStringStringReturns the machine instruction for the mnemonic of a Hack assembly computation fieldCalled "comp" in the original API
getJumpStringStringReturns the machine instruction for the mnemonic of a Hack assembly jump fieldCalled "jump" in the original API
getAddressStringStringReturns the binary value of a decimal address valueNot in the original API

4.3 SymbolTable

TheSymbolTable module keeps a correspondence between symbolic labels and numeric addresses. It's realized as follows:

MethodArgumentsReturnDescriptionComment
constructorvoidSymbolTableReturns a new SymbolTable object, with all built-in pointers of the Hack assembly language added per default-
addEntryString, StringvoidAdds a new entry with the symbol and its address value to the SymbolTable objectThe original API takes anint for the address value
deleteEntryStringvoidDeletes a specified entry in the SymbolTable objectNot in the original API
getTablevoidHashMap<String, String>Returns all entries in the SymbolTable objectNot in the original API
containsStringbooleanChecks if a symbol exists in the SymbolTable object-
getAddressStringStringGet the address associated with a value in the SymbolTable object, returnnull instead when the symbol doesn't existThe original API returns anint for the address value

4.4 Main

TheMain module drives the entire assembling process and executes the modules and their routines. It's realized in the following way:

  1. Initialization of the provided input and output files
  2. Initialization of a SymbolTable object
  3. Adding the labels and their line numbers of the assembly file to the SymbolTable object (labels, comments and empty lines don't add up to the line number)
  4. Parsing each line of the assembly file with a Parser object
  5. Keeping track of all symbols with the SymbolTable object
  6. Translating each parsed line to Hack machine instructions with the Coder class
  7. Creating a Hack file from all parsed and translated lines

About

An assembler for the Hack assembly language, built as a maven project

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp