{"id":19481743,"url":"https://github.com/mahdiydev/hack-assembly","last_synced_at":"2026-04-15T23:33:12.912Z","repository":{"id":261121022,"uuid":"883331434","full_name":"MahdiyDev/Hack-Assembly","owner":"MahdiyDev","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-05T08:20:01.000Z","size":1498,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-25T17:37:27.509Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","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/MahdiyDev.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":"2024-11-04T19:24:04.000Z","updated_at":"2025-01-05T08:20:04.000Z","dependencies_parsed_at":"2025-01-05T09:21:26.439Z","dependency_job_id":null,"html_url":"https://github.com/MahdiyDev/Hack-Assembly","commit_stats":null,"previous_names":["mahdiydev/hack-assembly"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MahdiyDev/Hack-Assembly","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MahdiyDev%2FHack-Assembly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MahdiyDev%2FHack-Assembly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MahdiyDev%2FHack-Assembly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MahdiyDev%2FHack-Assembly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MahdiyDev","download_url":"https://codeload.github.com/MahdiyDev/Hack-Assembly/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MahdiyDev%2FHack-Assembly/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31865067,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-15T15:24:51.572Z","status":"ssl_error","status_checked_at":"2026-04-15T15:24:39.138Z","response_time":63,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-11-10T20:06:23.355Z","updated_at":"2026-04-15T23:33:12.877Z","avatar_url":"https://github.com/MahdiyDev.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C-Instructions Reference\n\n![C-Instructions Reference](c_instructions_reference.png \"C-Instructions Reference\")\n\n## Hack Assembly Specification\n\nCurrently, assembly languages have become somewhat complex. However, we need basic concepts free of excessive abstraction. Therefore, according to the Nand2Tetris project, Hack Assembly is designed in a minimalistic form.\n\nHack Assembly instructions are divided into two types: **A (Address Mode)** and **C (Command Mode)**.\n\n### **A-instruction**\n- Represents either an address or a number in 15 bits.\n  \n### **C-instruction**\n- Used for performing operations (e.g., addition, subtraction, data writing, etc.).\n\nThe distinction between A-instruction and C-instruction lies in the first bit of a 16-bit number:\n- **0** for A-instruction.\n- **1** for C-instruction. \n\nThus, the remaining 15 bits are used to store the number in A-instruction.\n\n**Maximum value representation of A-instruction:** `0111 1111 1111 1111`.\n\n**Structure of a C-instruction:** `111 a c1,c2,c3,c4,c5,c6 d1,d2,d3 j1,j2,j3`.  \nAlthough letters are used here for explanation, they are replaced with `0` or `1` in practice.\n\n- **`a`** - Specifies the source of the ALU \"y\" operand during computation.\n- **`c`** - Represents the computation (`comp`) operation.\n- **`d`** - Indicates the destination (`dest`) register.\n- **`j`** - Specifies the jump condition (`jump`).\n\nYou can observe this structure in the truth table shown above.\n\n### Registers in Hack Assembly\n\nHack Assembly operates with three main registers: **D**, **A**, and **M**.\n\n- **A**: Represents the value currently stored in the A register.\n- **D**: Represents the value currently stored in the D register.\n- **M**: Represents the value stored at the memory address specified by the A register (`M == RAM[A]`).\n\n### A-instruction Usage\nA-instructions are denoted using the `@` symbol.  \n- Example: `@34` or via named variables like `@SP`.\n\n### C-instruction Usage\nC-instructions use the above structure to express operations, assignments (`D=A`), or jumps (`0;JMP`).\n\n### Labels in Hack Assembly\nHack Assembly also supports **labels**, represented as `(LOOP)`.  \nLabels are used to structure code and facilitate operations like loops. For instance:\n- A label `(LOOP)` can be defined.\n- By using `@LOOP` and `0;JMP`, operations can repeatedly execute until a condition is met.\n\n[Hack assembly example](test.asm)\n\n#lowlevel #asm\n\nVirtual Machine (VM)  \n\nVirtual Machine (VM) nima?\n\nAgar siz Java dasturlash tili bilan tanish bo'lsangiz, **virtual mashina** (VM) atamasini eshitgan bo'lishingiz mumkin. Biz bu tushunchani soddaroq versiyasini ko'rib chiqamiz.  \n\n**Virtual machine** – bu virtual mashina buyruqlari (bytecode) orqali amallarni bajaradigan maxsus dasturiy vosita. Hamda **stack** va **funksiyalarni** boshqarish imkoniyatini beradi.\n\nVirtual Machine nima uchun kerak?\n\nHozirda kompyuterlar va boshqa qurilmalarda ishlatiladigan **uch xil asosiy protsessor turi** mavjud:  \n- **x86**  \n- **x64**  \n- **ARM**  \n\nHar bir protsessor o'zining unikal buyruqlar to'plamiga ega. High-level (yuqori darajadagi) dasturlash tillaridan bu protsessorlarning buyruqlariga to'g'ridan-to'g'ri o'girib chiqish murakkab va ko'p mehnat talab qiladi. Shuning uchun o'rtaga virtaul mashina qo'yiladi. Keyin bytecode'dan instruction'larga compile qilinadi.\n\nBu yondashuv orqali dasturlarni turli platformalarda qayta yozmasdan ishlatish mumkin bo'ladi. Masalan, Java dasturi bir marta yozilib, turli operatsion tizim va qurilmalarda ishlaydi.\n\nAmallarni bajarish uchun xotira bo'laklarga (segmentlarga) bo'linadi. Segmentlar haqida batafsilroq ma'lumotni **Modern Assembly** mavzusida ko'rib chiqamiz. Hozir esa faqat **stack segmenti** haqida gaplashamiz.\n\n**Stack** – bu xotiraning maxsus qismi bo'lib, unda ma'lumotlar **tartibli** saqlanadi. Bu haqida oldinroq [mana bu yerda](https://t.me/mahdiydev/121) gaplashganmiz.\n\n**Stack** ikki asosiy amalni bajaradi:\n1. **`push`** – stack'ga ma'lumot qo'shadi.\n2. **`pop`** – stack'dan oxirgi qo'shilgan ma'lumotni olib tashlaydi.\n\nStack'da o'zgaruvchini saqlash **Modern Assembly**da sal murakkabroq ishlaydi. Biz hozircha oddiyroq versiyasi ko'ramiz. Stack'dagi o'zgaruchilarni saqlash uchun xotiradan local, argument, static, temp, internal lar uchun joy ajratamiz.\n\n**VM** (virtual mashina) da bu amallar quyidagicha ishlaydi:\n```\npush constant 3   // 3-ni stack'ga qo'shadi.\npop local 0       // Stack'dan 3-ni olib, uni local 0'ga saqlaydi.\n```\n\nVirtual machina arifmetik amallarni ham qo'llab quvatlaydi. Arifmetik amallar quyidagilardan iborat:\n- `add` - qo'shish\n- `sub` - ayrish\n- `and` - va\n- `or`  - yoki\n- `neg` - manfiyga o'zgartirish\n- `not` - 0 bo'lsa 1 yoki 1 bo'lsa 0\n- `eq`  - teng\n- `gt`  - dan katta\n- `lt`  - dan kichik\n\nFunksiya:\n```\nfunction add 0      // local o'zgaruvchilar soni\n    push argument 0 // birinchi argument yuklash (arg0)\n    push argument 1 // ikkinchi argument yuklash (arg1)\n    add             // ikkasini qo'shish\n    return          // natijani qaytarish\n```\n\nFunksiyani chaqirish uchun:\n```\nfunction Sys.main 2  // local o'zgaruvchilar soni\n    push constant 10\n    push constant 20\n    call add 2       // funksiyani 10 va 20 argumentlar bilan chaqirish\n    pop local 0      // natijani local ga yuklash\n    push local 0     // local o'zgaruvchini saqlash\n    return\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmahdiydev%2Fhack-assembly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmahdiydev%2Fhack-assembly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmahdiydev%2Fhack-assembly/lists"}