{"id":18839261,"url":"https://github.com/solareenlo/42libasm","last_synced_at":"2025-10-15T02:25:37.005Z","repository":{"id":93865660,"uuid":"339123212","full_name":"solareenlo/42libasm","owner":"solareenlo","description":"[WIP]Assembler Basics","archived":false,"fork":false,"pushed_at":"2021-02-24T21:15:06.000Z","size":134,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-30T09:16:12.420Z","etag":null,"topics":["42","42born2code","assembler","assembly","libasm"],"latest_commit_sha":null,"homepage":"","language":"Assembly","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/solareenlo.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":"2021-02-15T15:37:53.000Z","updated_at":"2021-03-11T23:04:03.000Z","dependencies_parsed_at":"2023-03-07T22:45:11.679Z","dependency_job_id":null,"html_url":"https://github.com/solareenlo/42libasm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solareenlo%2F42libasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solareenlo%2F42libasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solareenlo%2F42libasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solareenlo%2F42libasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/solareenlo","download_url":"https://codeload.github.com/solareenlo/42libasm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239774333,"owners_count":19694700,"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":["42","42born2code","assembler","assembly","libasm"],"created_at":"2024-11-08T02:42:34.309Z","updated_at":"2025-10-15T02:25:31.960Z","avatar_url":"https://github.com/solareenlo.png","language":"Assembly","funding_links":[],"categories":[],"sub_categories":[],"readme":"# libasm\n\n## 大文字と小文字の区別\n- label と section は大文字と小文字を区別する．\n\n## Syscall\n### syscall rapper\n- errno.h に errno の一覧がある．\n- syscall に失敗すると，syscall_error 関数が働いて，errno と同じ値が rax に入ってくる．\n- syscall の失敗の ret に errno が入っているのを ret を -1 にするラッパー関数が働いて，移植性を高められるようにしている．\n- Reference: [Linuxカーネルに見る、システムコール番号と引数、システムコール・ラッパーとは](https://www.atmarkit.co.jp/ait/articles/1703/01/news172_2.html)\n\n### syscall number\n- sys/syscall.h に記載されている．\n  - References: [syscall.h](https://opensource.apple.com/source/xnu/xnu-792/bsd/sys/syscall.h.auto.html), [x86 Linux の 32bit と 64bit のシステムコールの違い](https://www.mztn.org/lxasm64/x86_x64_table.html)\n- mac は，syscall_sw.h で，syscall number を6種類のバージョンへと使い分けている．\n  - Reference: [syscall_sw.h](https://opensource.apple.com/source/xnu/xnu-4570.1.46/osfmk/mach/i386/syscall_sw.h.auto.html)\n\n## Register\n### syscall の引数と register との位置関係\n\n| syscall num | 1st para | 2nd para | 3rd para | 4th para | 5th para | 6th para | result |\n|-------------|----------|----------|----------|----------|----------|----------|--------|\n| rax         | rdi      | rsi      | rdx      | r10      | r8       | r9       | rax    |\n\n### 一般的な関数の引数と register との位置関係\n\n| 1st para | 2nd para | 3rd para | 4th para | 5th para | 6th para | それ以降     | result |\n|----------|----------|----------|----------|----------|----------|--------------|--------|\n| rdi      | rsi      | rdx      | rcx      | r8       | r9       | 逆順に stack | rax    |\n\nReference: [x86 Assembly/Interfacing with Linux](https://en.wikibooks.org/wiki/X86_Assembly/Interfacing_with_Linux)\n\n### callee-saved register \u0026 caller-saved register\n- Callee-saved register (呼び出し先退避レジスタ)\n  - rbx, rbp, rsp, r12, r13, r14, r15\n  - 呼び出された関数自身が退避/復旧するレジスタ．\n- Caller-saved register (呼び出し元退避レジスタ)\n  - rax, rcx, rdx, rsi, rdi, r8, r9, r10, r11\n  - 関数を呼び出す側，呼び出し前に退避し，呼び出し後に復旧するレジスタ．\n\n### References\n- [条件ジャンプとフラグ, フラグレジスタの内容](http://wisdom.sakura.ne.jp/programming/asm/assembly13.html)\n\n## Addressing\n```asm\nbase + scale * offset + displacement\nmov rax, [rdx + 8*rcx + 42]\n```\n- displacement: immediate value\n- scale: 1, 2, 4 or 8\n\n## Instruction\n### shr, sar\n\n| ニーモニック | 動作                                                            |\n|:------------:|-----------------------------------------------------------------|\n|      shr     | logic shift (シフトしてできた端の空白は必ず 0 を挿入する)       |\n|      sar     | arithmetic shift (シフトしても最も右端のビットの値が変化しない) |\n\n### Logical operation\n\n| ニーモニック | 動作                                                                     |\n|:------------:|--------------------------------------------------------------------------|\n|      and     | ビット毎の論理積 (両方が真で真)                                          |\n|      or      | ビット毎の論理和 (どちらかが真で真)                                      |\n|      xor     | ビット毎の排他的論理和 (異なれば真)                                      |\n|     test     | ビット毎の論理積による比較 (結果をフラグに反映させる,演算結果は返さない)\u003cbr\u003e レジスタの値が0かどうかを調べるには，test \\\u003creg\\\u003e\\\u003creg\\\u003e 命令を使うのが一般的|\n\n### lea, move\n\n| ニーモニック | 動作                                               |\n|:------------:|----------------------------------------------------|\n|      lea     | 実行アドレスの値そのものをレジスタに設定する       |\n|     move     | 実行アドレスの内容をレジスタまたはメモリに書き込む |\n\n## Directive\n### db, dw, dd, dq\n\n| Directive | Description                    |\n|:---------:|--------------------------------|\n| db        | bytes                          |\n| dw        | words, equal to 2 bytes each   |\n| dd        | double words, equal to 4 bytes |\n| dq        | quad words, equal to 8 bytes   |\n\n## Register\n\n| Name     | Onother Name | Description                              |\n|----------|--------------|------------------------------------------|\n| r0       | rax          | 算術処理で accumulator として使われる    |\n| r3       | rbx          | Base register                            |\n| r1       | rcx          | ループの回数 (cycles) に使われる         |\n| r2       | rdx          | 入出力処理の間，データ (data) を格納する |\n| r4       | rsp          | Stack pointer                            |\n| r5       | rbp          | Base pointer of stack frame              |\n| r6       | rsi          | Source index                             |\n| r7       | rdi          | Destination index                        |\n| r8...r15 | なし         |                                          |\n\n## Basic\n### system call\n1. rax register にシステムコールの番号を入れる．\n2. 引数は，以下のレジスタに入れる．\n    - rdi, rsi, rbx, r10, r8, r9\n    - システムコールは 6 個を超える引数を受け取ることはできない．\n3. system call を実行する．\n- レジスタを初期化する順序は問わない．\n- システムコール命令は，rcx と r11 の値を書き換える．\n  - 理由は，後ほど．\n- 関数のリターン値は，rax に入ってる．\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsolareenlo%2F42libasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsolareenlo%2F42libasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsolareenlo%2F42libasm/lists"}