基础程序汇编test.cint main() { return 42; }查看反汇编cc -o test test.c objdump -d -M intel test 0000000000001129 main: 1129: f3 0f 1e fa endbr64 112d: 55 push rbp 112e: 48 89 e5 mov rbp,rsp 1131: b8 2a 00 00 00 mov eax,0x2a 1136: 5d pop rbp 1137: c3 ret函数参数汇编test.cint plus(int x, int y) {return x y;}int main() {return plus(3, 4);}.intel_syntax noprefix.globl plus, mainplus:add rsi, rdimov rax, rsiretmain:mov rdi, 3mov rsi, 4call plusret.globl开头表示函数整个程序可见call原理call将下一条指令在本例中ret的地址推入堆栈call跳转到作为参数给出的地址自定义编译器编译基础程序test.c#include stdio.h #include stdlib.h int main(int argc, char **argv) { if (argc ! 2) { fprintf(stderr, Please input a Number:\n); return 1; } printf(.intel_syntax noprefix\n); printf(.globl main\n); printf(main:\n); printf( mov rax, %d\n, atoi(argv[1])); printf( ret\n); return 0; }编译运行cc -o test test.c ./test 42 test.s cc -o tmp test.s ./tmp echo $?自定义编译器编译加减法程序test.c#include stdio.h #include stdlib.h int main(int argc, char **argv) { if (argc ! 2) { fprintf(stderr, Please input a Number\n); return 1; } char *p argv[1]; printf(.intel_syntax noprefix\n); printf(.globl main\n); printf(main:\n); printf( mov rax, %ld\n, strtol(p, p, 10)); // strtol读取数字读完自动跳到下个符号 while (*p) { if (*p ) { p; printf( add rax, %ld\n, strtol(p, p, 10)); continue; } if (*p -) { p; printf( sub rax, %ld\n, strtol(p, p, 10)); continue; } fprintf(stderr, illegal sign %c\n, *p); return 1; } printf( ret\n); return 0; }编译cc -o test test.c ./test 1234 tmp.s cc -o tmp tmp.s ./tmp echo $? ./test 1*3 tmp.s //会提示非法符号测试test.sh#!/bin/bash assert() { expected$1 input$2 ./chibicc $input tmp.s || exit gcc -static -o tmp tmp.s ./tmp actual$? if [ $actual $expected ]; then echo $input $actual else echo $input $expected expected, but got $actual exit 1 fi } assert 0 0 assert 42 42 assert 21 520-4 echo OK参考Compiler Explorer创作不易小小的支持一下吧