Linux C编译器从零开发一

基础程序汇编
test.c
cpp 复制代码
int main() {
  return 42;
}
查看反汇编
bash 复制代码
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.c

int plus(int x, int y) {

return x + y;

}

int main() {

return plus(3, 4);

}

.intel_syntax noprefix

.globl plus, main

plus:

add rsi, rdi

mov rax, rsi

ret

main:

mov rdi, 3

mov rsi, 4

call plus

ret

.globl开头表示函数整个程序可见

call原理

  • call将下一条指令(在本例中)ret的地址推入堆栈
  • call跳转到作为参数给出的地址
自定义编译器编译基础程序
test.c
cpp 复制代码
#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;
}
编译运行
bash 复制代码
cc -o test test.c
./test 42 > test.s
cc -o tmp test.s
./tmp
echo $?
自定义编译器编译加减法程序
test.c
cpp 复制代码
#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;
}
编译
bash 复制代码
cc -o test test.c
./test 12+3+4 > tmp.s
cc -o tmp tmp.s
./tmp
echo $?
./test 1*3 > tmp.s //会提示非法符号
测试
test.sh
bash 复制代码
#!/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 '5+20-4'

echo OK

参考

Compiler Explorer


创作不易,小小的支持一下吧!

相关推荐
Navigator_Z3 小时前
LeetCode //C - 1208. Get Equal Substrings Within Budget
c语言·算法·leetcode
xx~t4 小时前
嵌入式——进程与线程1
linux·c语言·学习·嵌入式·进程与线程
m0_547486665 小时前
《JavaScript核心原理 》全套PPT课件2026
开发语言·javascript·ecmascript
老当益壮梁奶奶5 小时前
Linux软件编程学习笔记(七):线程分离与线程间通信详解
linux·c语言·c++·笔记·学习
MC皮蛋侠客5 小时前
Tauri 2.x 系列(五):调用系统 API——官方插件、Rust crate 与原生能力
开发语言·后端·rust
布莱克6056 小时前
软中断和硬中断的区别
开发语言·操作系统
whcyhhh6 小时前
头歌实践教学平台:数据科学与大数据技术导论(十二)
大数据·开发语言·python·数据清洗
lzhdim7 小时前
13、JavaScript事件循环机制 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
zbyyd7 小时前
Linux 多线程:互斥锁 &amp; 信号量
linux·c语言·开发语言·网络
wuyk5558 小时前
从零吃透Modbus通信|第3章:STM32 Modbus RTU主机
服务器·开发语言·网络·数据结构·stm32·单片机·嵌入式硬件