{"id":17464396,"url":"https://github.com/buhe/hack_assembler","last_synced_at":"2025-06-12T17:09:17.825Z","repository":{"id":76742943,"uuid":"493924180","full_name":"buhe/hack_assembler","owner":"buhe","description":"👷🏽 assembler of https://github.com/buhe/study_nand2tetris","archived":false,"fork":false,"pushed_at":"2022-05-23T06:03:30.000Z","size":131,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T07:26:49.319Z","etag":null,"topics":["assembler"],"latest_commit_sha":null,"homepage":"","language":"Hack","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/buhe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-19T04:50:27.000Z","updated_at":"2022-06-03T07:33:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"198c895d-8992-4eeb-b3c8-fad57645cc05","html_url":"https://github.com/buhe/hack_assembler","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/buhe/hack_assembler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buhe%2Fhack_assembler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buhe%2Fhack_assembler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buhe%2Fhack_assembler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buhe%2Fhack_assembler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/buhe","download_url":"https://codeload.github.com/buhe/hack_assembler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buhe%2Fhack_assembler/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259509473,"owners_count":22868835,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assembler"],"created_at":"2024-10-18T10:45:38.361Z","updated_at":"2025-06-12T17:09:17.787Z","avatar_url":"https://github.com/buhe.png","language":"Hack","funding_links":[],"categories":[],"sub_categories":[],"readme":"## hack assembler\n### Hack Assembly-to-Binary Translation Specification\nThe Hack assembly language and its equivalent binary representation were specified\nin chapter 4. A compact and formal version of this language specification is repeated\nhere, for ease of reference. This specification can be viewed as the contract that Hack\nassemblers must implement, one way or another.\n#### 6.2.1 Syntax Conventions and File Formats\nFile Names By convention, programs in binary machine code and in assembly\ncode are stored in text files with ‘‘hack’’ and ‘‘asm’’ extensions, respectively. Thus, a\nProg.asm file is translated by the assembler into a Prog.hack file.\nBinary Code (.hack) Files A binary code file is composed of text lines. Each line is\na sequence of 16 ‘‘0’’ and ‘‘1’’ ASCII characters, coding a single 16-bit machine language instruction. Taken together, all the lines in the file represent a machine language program. When a machine language program is loaded into the computer’s\ninstruction memory, the binary code represented by the file’s nth line is stored in address n of the instruction memory (the count of both program lines and memory\naddresses starts at 0).\nAssembly Language (.asm) Files An assembly language file is composed of text\nlines, each representing either an instruction or a symbol declaration:\nm Instruction: an A-instruction or a C-instruction, described in section 6.2.2.\nm (Symbol): This pseudo-command binds the Symbol to the memory location\ninto which the next command in the program will be stored. It is called ‘‘pseudocommand’’ since it generates no machine code.\n(The remaining conventions in this section pertain to assembly programs only.)\nConstants and Symbols Constants must be non-negative and are written in decimal\nnotation. A user-defined symbol can be any sequence of letters, digits, underscore (_),\ndot (.), dollar sign ($), and colon (:) that does not begin with a digit.\nComments Text beginning with two slashes (//) and ending at the end of the line is\nconsidered a comment and is ignored.\nWhite Space Space characters are ignored. Empty lines are ignored.\nCase Conventions All the assembly mnemonics must be written in uppercase. The\nrest (user-defined labels and variable names) is case sensitive. The convention is to\nuse uppercase for labels and lowercase for variable names.\n#### 6.2.2 Instructions\nThe Hack machine language consists of two instruction types called addressing instruction (A-instruction) and compute instruction (C-instruction). The instruction\nformat is as follows.\nA-instruction: @value // Where value is either a non-negative decimal number\n// or a symbol referring to such number.\nvalue (v ¼ 0 or 1)\nBinary: 0 v vv v v v v v v v v v v v v\nC-instruction: dest=comp;jump // Either the dest or jump fields may be empty.\n// If dest is empty, the ‘=’’ is omitted;\n// If jump is empty, the ‘‘;’’ is omitted.\ncomp dest jump\nBinary: 1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3\nThe translation of each of the three fields comp, dest, jump to their binary forms is\nspecified in the following three tables.\n```\ncomp(when a=0) c1 c2 c3 c4 c5 c6 comp(when a=1)\n0   1 0 1 0 1 0\n1   1 1 1 1 1 1\n-1  1 1 1 0 1 0\nD   0 0 1 1 0 0\nA   1 1 0 0 0 0  M\n!D  0 0 1 1 0 1\n!A  1 1 0 0 0 1 !M\n-D  0 0 1 1 1 1\n-A  1 1 0 0 1 1 -M\nD+1 0 1 1 1 1 1\nA+1 1 1 0 1 1 1 M+1\nD-1 0 0 1 1 1 0\nA-1 1 1 0 0 1 0 M-1\nD+A 0 0 0 0 1 0 D+M\nD-A 0 1 0 0 1 1 D-M\nA-D 0 0 0 1 1 1 M-D\nD\u0026A 0 0 0 0 0 0 D\u0026M\nD|A 0 1 0 1 0 1 D|M\n\ndest d1 d2 d3 jump j1 j2 j3\nnull 0 0 0    null 0 0 0\nM 0 0 1       JGT 0 0 1\nD 0 1 0       JEQ 0 1 0\nMD 0 1 1      JGE 0 1 1\nA 1 0 0       JLT 1 0 0\nAM 1 0 1      JNE 1 0 1\nAD 1 1 0      JLE 1 1 0\nAMD 1 1 1     JMP 1 1 1\n```\n#### 6.2.3 Symbols\nHack assembly commands can refer to memory locations (addresses) using\neither constants or symbols. Symbols in assembly programs arise from three\nsources.\nPredefined Symbols Any Hack assembly program is allowed to use the following\npredefined symbols.\n\nLabel RAM address (hexa)\n```\nSP 0 0x0000\nLCL 1 0x0001\nARG 2 0x0002\nTHIS 3 0x0003\nTHAT 4 0x0004\nR0-R15 0-15 0x0000-f\nSCREEN 16384 0x4000\nKBD 24576 0x6000\n```\nNote that each one of the top five RAM locations can be referred to using two\npredefined symbols. For example, either R2 or ARG can be used to refer to\nRAM[2].\nLabel Symbols The pseudo-command (Xxx) defines the symbol Xxx to refer to the\ninstruction memory location holding the next command in the program. A label can\nbe defined only once and can be used anywhere in the assembly program, even before\nthe line in which it is defined.\nVariable Symbols Any symbol Xxx appearing in an assembly program that is\nnot predefined and is not defined elsewhere using the (Xxx) command is treated as\na variable. Variables are mapped to consecutive memory locations as they are first\nencountered, starting at RAM address 16 (0x0010).\n```\nc: 15(opcode)|xx|12(control)|11 10 9 8 7 6(control)|5 4 3(dest)|2 1 0(jump)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuhe%2Fhack_assembler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbuhe%2Fhack_assembler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuhe%2Fhack_assembler/lists"}