{"id":17988214,"url":"https://github.com/program-spiritual/gab","last_synced_at":"2025-03-25T22:33:22.551Z","repository":{"id":112520914,"uuid":"281935314","full_name":"program-spiritual/GAB","owner":"program-spiritual","description":"图解汇编基础 (graphical assembly basis)","archived":true,"fork":false,"pushed_at":"2024-01-21T10:05:29.000Z","size":722,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T15:39:15.676Z","etag":null,"topics":["assembler","assembly","call1-craft","cpp","gab"],"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/program-spiritual.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":"2020-07-23T11:40:18.000Z","updated_at":"2025-01-09T17:00:16.000Z","dependencies_parsed_at":"2024-05-20T10:06:28.906Z","dependency_job_id":null,"html_url":"https://github.com/program-spiritual/GAB","commit_stats":null,"previous_names":["yiyandaoren/gab","program-spiritual/gab"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/program-spiritual%2FGAB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/program-spiritual%2FGAB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/program-spiritual%2FGAB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/program-spiritual%2FGAB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/program-spiritual","download_url":"https://codeload.github.com/program-spiritual/GAB/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245509876,"owners_count":20627073,"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","assembly","call1-craft","cpp","gab"],"created_at":"2024-10-29T19:11:01.411Z","updated_at":"2025-03-25T22:33:22.220Z","avatar_url":"https://github.com/program-spiritual.png","language":"Assembly","readme":"![](images/icon.svg)\n\n# GAB\n\u003e 亲爱的朋友们，现在 AI 时代已经到来，我的项目维护虽然在继续，但是无法同步和大家分享知识，最近我已经注册了微信订阅号，希望看到的朋友互相通知一下： 微信公众号：编程悟道\n\n\n\u003e 图解汇编基础 \n\n![](images/gab-snap.png)\n\n[在线文档](https://xiaomiwujiecao.github.io/GAB)\n\n### 后端编译任务流程\n\n![](images/backend-task.png)\n\n### OS  GNU 汇编器\n\n- macos -- `内置`\n\n- linux -- `内置`\n\n- Windows -- [`MinGW`](https://osdn.net/projects/mingw/releases/)  or `linux vm`\n\n### 汇编语言的要素\n\n- 指令\n- 伪指令\n- 标签\n- 注释\n\n\n### 汇编语言组成\n\n![](images/assembly-language.png)\n\n### 常用指令\n\n\n\n### 指令后缀\n\n![](./images/instruction-suffix-1.png)\n\n### 汇编指令-操作数\n\n![](images/operand.png)\n\n\n### x86-64 架构的寄存器\n\n![](images/x86-64-architectural-register.png)\n\n### 生成汇编代码\n\n```bash \nclang -S -O2 hello.c -o hello.s\n#或者：\ngcc -S -O2 hello.c -o hello.s\n```\n\n### 将汇编代码编译成可执行文件\n\n\n```bash \n#  // 用汇编器编译成目标文件\nas hello.s -o hello.o  \n#  //链接成可执行文件 \ngcc hello.o -o hello  \n#  //运行程序\n./hello                \n```\n\n\n## 文档目录\n\n### 第一章\n\n#### 函数调用\n\n函数调用\n```c\n\n/*function-call1.c */\n#include \u003cstdio.h\u003e\nint fun1(int a, int b){\n    int c = 10;\n    return a+b+c;\n}\n\nint main(int argc, char *argv[]){\n    printf(\"fun1: %d\\n\", fun1(1,2));\n    return 0;\n}\n\n```\n\n\n生成的汇编代码，文档里有注释:\n\n```asm6502\n# function call\n\n# text segment, pure code\n\n.section __TEXT,__text,regular,pure_instructions\n\n_fun1:\n\n  # prelude to the function call, setting the stack pointer\n\n  pushq %rbp # save the bottom address of the caller's stack frame\n\n  movq %rsp , %rbp # set the top address of the caller's stack frame to the bottom of this stack frame\n\n  subq $4 , %rsp  # extension stack\n\n  movl $10 , -4(%rbp) # The variable c is assigned a value of 10, which can also be written as movl $10, (%rsp)\n\n  # do addition\n\n  movl %edi , %eax  # put the first parameter in %eax\n\n  addl %esi , %eax  # add the second parameter to %eax, %eax is also the register that stores the return value\n\n  addl -4(%rbp), %eax # plus the value of c\n\n  addq $4 , %rsp  # shrink the stack\n\n  # at the end of the function call, restore the stack pointer to its original value\n\n  pushq %rbp # save the bottom address of the caller's stack frame\n\n  movq %rsp , %rbp  # set the top address of the caller's stack frame to the bottom of this stack frame\n\n  # set the first and second parameters, 1 and 2 respectively\n\n  movl $1, %edi\n\n  movl $2, %esi\n\n  callq _fun1  # call function\n\n  # set parameters for pritf\n\n  leaq L_.str(%rip), %rdi  # the first parameter is the address of the string\n\n  movl %eax, %esi  # the second parameter is the return value of the previous parameter\n\n  # set the return value. This sentence is also commonly used in commands such as xorl %esi, %esi, which are all set to zero\n\n  movl $0 , %eax\n\n  # restore the bottom value of the caller's stack frame\n\n  popq %rbp # restore the bottom value of the caller's stack frame\n\n  retq  # return\n\n  # text segment, save string literal\n\n  .section __TEXT,__cstring,cstring_literals\n\nL_.str:                             ## @.str\n\n  .asciz \"Hello World! :%d \\n\"\n\n\n```\n#### IF 语句的汇编\n\n```asm6502\n\t.text\n\t.globl _fun1\n_fun1:\nLFB0:\n\tpushq\t%rbp\nLCFI0:\n\tmovq\t%rsp, %rbp\nLCFI1:\n\tmovl\t%edi, -4(%rbp)\n\tjmp\tL2\nL3:\n\taddl\t$1, -4(%rbp)\nL2:\n\tcmpl\t$9, -4(%rbp)\n\tjle\tL3\n\tnop\n\tnop\n\tpopq\t%rbp\nLCFI2:\n\tret\nLFE0:\n\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\nEH_frame1:\n\t.set L$set$0,LECIE1-LSCIE1\n\t.long L$set$0\nLSCIE1:\n\t.long\t0\n\t.byte\t0x1\n\t.ascii \"zR\\0\"\n\t.byte\t0x1\n\t.byte\t0x78\n\t.byte\t0x10\n\t.byte\t0x1\n\t.byte\t0x10\n\t.byte\t0xc\n\t.byte\t0x7\n\t.byte\t0x8\n\t.byte\t0x90\n\t.byte\t0x1\n\t.align 3\nLECIE1:\nLSFDE1:\n\t.set L$set$1,LEFDE1-LASFDE1\n\t.long L$set$1\nLASFDE1:\n\t.long\tLASFDE1-EH_frame1\n\t.quad\tLFB0-.\n\t.set L$set$2,LFE0-LFB0\n\t.quad L$set$2\n\t.byte\t0\n\t.byte\t0x4\n\t.set L$set$3,LCFI0-LFB0\n\t.long L$set$3\n\t.byte\t0xe\n\t.byte\t0x10\n\t.byte\t0x86\n\t.byte\t0x2\n\t.byte\t0x4\n\t.set L$set$4,LCFI1-LCFI0\n\t.long L$set$4\n\t.byte\t0xd\n\t.byte\t0x6\n\t.byte\t0x4\n\t.set L$set$5,LCFI2-LCFI1\n\t.long L$set$5\n\t.byte\t0xc\n\t.byte\t0x7\n\t.byte\t0x8\n\t.align 3\nLEFDE1:\n\t.ident\t\"GCC: (Homebrew GCC 9.3.0_1) 9.3.0\"\n\t.subsections_via_symbols\n\n```\n另一种写法：\n\n```asm6502\n\t.text\n\t.globl _fun1\n_fun1:\nLFB0:\n\tpushq\t%rbp\nLCFI0:\n\tmovq\t%rsp, %rbp\nLCFI1:\n\tmovl\t%edi, -4(%rbp)\n\tcmpl\t$10, -4(%rbp)\n\tjle\tL2\n\tmovl\t$4, %eax\n\tjmp\tL3\nL2:\n\tmovl\t$8, %eax\nL3:\n\tpopq\t%rbp\nLCFI2:\n\tret\nLFE0:\n\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\nEH_frame1:\n\t.set L$set$0,LECIE1-LSCIE1\n\t.long L$set$0\nLSCIE1:\n\t.long\t0\n\t.byte\t0x1\n\t.ascii \"zR\\0\"\n\t.byte\t0x1\n\t.byte\t0x78\n\t.byte\t0x10\n\t.byte\t0x1\n\t.byte\t0x10\n\t.byte\t0xc\n\t.byte\t0x7\n\t.byte\t0x8\n\t.byte\t0x90\n\t.byte\t0x1\n\t.align 3\nLECIE1:\nLSFDE1:\n\t.set L$set$1,LEFDE1-LASFDE1\n\t.long L$set$1\nLASFDE1:\n\t.long\tLASFDE1-EH_frame1\n\t.quad\tLFB0-.\n\t.set L$set$2,LFE0-LFB0\n\t.quad L$set$2\n\t.byte\t0\n\t.byte\t0x4\n\t.set L$set$3,LCFI0-LFB0\n\t.long L$set$3\n\t.byte\t0xe\n\t.byte\t0x10\n\t.byte\t0x86\n\t.byte\t0x2\n\t.byte\t0x4\n\t.set L$set$4,LCFI1-LCFI0\n\t.long L$set$4\n\t.byte\t0xd\n\t.byte\t0x6\n\t.byte\t0x4\n\t.set L$set$5,LCFI2-LCFI1\n\t.long L$set$5\n\t.byte\t0xc\n\t.byte\t0x7\n\t.byte\t0x8\n\t.align 3\nLEFDE1:\n\t.ident\t\"GCC: (Homebrew GCC 9.3.0_1) 9.3.0\"\n\t.subsections_via_symbols\n\n```\n#### 表达浮点型汇编的写法\n\n```asm6502\n\t.text\n\t.p2align 4\n\t.globl _fun1\n_fun1:\nLFB0:\n\taddss\t%xmm1, %xmm0 #浮点数传参用XMM寄存器，加法用addss指令\n\taddss\tlC0(%rip), %xmm0 #把常量2.0加到xmm0上，xmm0保存返回值\n\tret\nLFE0:\n\t.literal4\n\t.align 2\nlC0:\n\t.long\t1073741824\n\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\nEH_frame1:\n\t.set L$set$0,LECIE1-LSCIE1\n\t.long L$set$0\nLSCIE1:\n\t.long\t0\n\t.byte\t0x1\n\t.ascii \"zR\\0\"\n\t.byte\t0x1\n\t.byte\t0x78\n\t.byte\t0x10\n\t.byte\t0x1\n\t.byte\t0x10\n\t.byte\t0xc\n\t.byte\t0x7\n\t.byte\t0x8\n\t.byte\t0x90\n\t.byte\t0x1\n\t.align 3\nLECIE1:\nLSFDE1:\n\t.set L$set$1,LEFDE1-LASFDE1\n\t.long L$set$1\nLASFDE1:\n\t.long\tLASFDE1-EH_frame1\n\t.quad\tLFB0-.\n\t.set L$set$2,LFE0-LFB0\n\t.quad L$set$2\n\t.byte\t0\n\t.align 3\nLEFDE1:\n\t.ident\t\"GCC: (Homebrew GCC 9.3.0_1) 9.3.0\"\n\t.subsections_via_symbols\n\n```\n\n#### 将C代码 生成 汇编代码， 并将汇编代码生成目标文件\n\nC 函数传多个参数的代码：\n\n```c\n#include \u003cstdio.h\u003e\n\nextern int fun1(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8);\n\nint main(int argc, char *argv[])\n{\n    printf(\"fun1: %d \\n\", fun1(1,2,3,4,5,6,7,8));\n    return 0;\n}\n\n```\n\n将生成的汇编代码编译为目标文件，并结合链接器，生成可执行文件或库\n\n```bash\n# 生成链接文件 \nas convention-fun1.s -o convention-fun1.o \n# 生成可执行文件或库\ngcc conv.c convention-fun1.o -o convention\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogram-spiritual%2Fgab","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprogram-spiritual%2Fgab","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogram-spiritual%2Fgab/lists"}