编译手艺人

术之尽头

源文件

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

int add(int x,int y) {
return x + y;
}

int main(int argc,char** arg) {

printf("%d + %d = %d",1,2,add(1,2));
return 0;
}

汇编

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	.section	__TEXT,__text,regular,pure_instructions
.build_version macos, 13, 0 sdk_version 13, 3
.globl _add ## -- Begin function add
.p2align 4, 0x90
_add: ## @add
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl -4(%rbp), %eax
addl -8(%rbp), %eax
popq %rbp
retq
.cfi_endproc
## -- End function
.globl _main ## -- Begin function main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
subq $16, %rsp
movl $0, -4(%rbp)
movl %edi, -8(%rbp)
movq %rsi, -16(%rbp)
movl $1, %edi
movl $2, %esi
callq _add
movl %eax, %ecx
leaq L_.str(%rip), %rdi
movl $1, %esi
movl $2, %edx
movb $0, %al
callq _printf
xorl %eax, %eax
addq $16, %rsp
popq %rbp
retq
.cfi_endproc
## -- End function
.section __TEXT,__cstring,cstring_literals
L_.str: ## @.str
.asciz "%d + %d = %d"

.subsections_via_symbols

逐行代码解析

  1. .section __TEXT,__text,regular,pure_instructions: 定义一个新的段(section),名为__TEXT,__text,用于存放正文(代码);regularpure_instructions是段的属性,表示该段包含普通的指令集和纯指令(无数据)。

  2. .build_version macos, 13, 0 sdk_version 13, 3: 声明编译的版本信息,表示此程序的目标操作系统版本为 macOS 13.0,使用的 SDK 版本为 13.3。

  3. .globl _add: 声明全局符号 _add,用于表示函数add的入口。

  4. .p2align 4, 0x90: 确保下一个指令的地址在16字节对齐位置上。

  5. _add:: 函数add的标签,表示函数的入口。

  6. .cfi_startproc: 声明函数的开始,用于生成调试信息。

  7. pushq %rbp: 将寄存器%rbp的值压入栈中,保存之前的栈帧指针。

  8. .cfi_def_cfa_offset 16: 定义栈帧指针偏移,将栈帧大小设置为16字节。

  9. .cfi_offset %rbp, -16: 定义寄存器%rbp的位置在栈的偏移量为-16。

  10. movq %rsp, %rbp: 将当前栈指针%rsp的值赋给栈帧指针%rbp,建立新的栈帧。

  11. .cfi_def_cfa_register %rbp: 定义新的栈帧指针寄存器为%rbp

  12. movl %edi, -4(%rbp): 将函数的第一个参数(整型)移动到栈帧中的位置,偏移量为-4。

  13. movl %esi, -8(%rbp): 将函数的第二个参数(整型)移动到栈帧中的位置,偏移量为-8。

  14. movl -4(%rbp), %eax: 将栈帧中的第一个参数加载到通用寄存器%eax中。

  15. addl -8(%rbp), %eax: 将栈帧中的第二个参数与%eax寄存器的值相加。

  16. popq %rbp: 弹出保存的栈帧指针。

  17. retq: 返回函数,将栈帧指针弹出到程序计数器,并跳转到调用函数的位置。

  18. .cfi_endproc: 声明函数的结束,停止生成调试信息。

  19. .globl _main: 声明全局符号 _main,用于表示函数main的入口。

  20. .p2align 4, 0x90: 确保下一个指令的地址在16字节对齐位置上。

  21. _main:: 函数main的标签,表示函数的入口。

  22. .cfi_startproc: 声明函数的开始,用于生成调试信息。

  23. pushq %rbp: 将寄存器%rbp的值压入栈中,保存之前的栈帧指针。

  24. .cfi_def_cfa_offset 16: 定义栈帧指针偏移,将栈帧大小设置为16字节。

  25. .cfi_offset %rbp, -16: 定义寄存器%rbp的位置在栈的偏移量为-16。

  26. movq %rsp, %rbp: 将当前栈指针%rsp的值赋给栈帧指针%rbp,建立新的栈帧。

  27. .cfi_def_cfa_register %rbp: 定义新的栈帧指针寄存器为%rbp

  28. subq $16, %rsp: 在栈上分配16字节的空间,为本地变量的使用而预留。

  29. movl $0, -4(%rbp): 初始化栈帧中的一个本地变量,将值0存储在偏移量为-4的位置。

  30. movl %edi, -8(%rbp): 将函数的第一个参数(整型)移动到栈帧中的位置,偏移量为-8。

  31. movq %rsi, -16(%rbp): 将函数的第二个参数(整型)移动到栈帧中的位置,偏移量为-16。

  32. movl $1, %edi: 将值1存储在寄存器%edi中,准备作为第一个参数传递给_add函数。

  33. movl $2, %esi: 将值2存储在寄存器%esi中,准备作为第二个参数传递给_add函数。

  34. callq _add: 调用函数_add,将参数传递给它,并开始执行该函数。

  35. movl %eax, %ecx: 将_add函数的返回值(存储在寄存器%eax中)移动到寄存器%ecx中,准备作为参数传递给printf函数。

  36. leaq L_.str(%rip), %rdi: 将字符串L_.str的地址(存储在相对寻址中)加载到寄存器%rdi中,作为printf函数的第一个参数。

  37. movl $1, %esi: 将值1存储在寄存器%esi中,准备作为printf函数的第二个参数。

  38. movl $2, %edx: 将值2存储在寄存器%edx中,准备作为printf函数的第三个参数。

  39. movb $0, %al: 将值0存储在寄存器%al中,准备作为printf函数的第四个参数。

  40. callq _printf: 调用printf函数,将之前准备好的参数传递给它,开始执行打印操作。

  41. xorl %eax, %eax: 将寄存器%eax与自身进行异或操作,将结果存储回%eax,相当于将其清零。

  42. addq $16, %rsp: 释放之前预留的16字节的栈空间。

  43. popq %rbp: 弹出保存的栈帧指针。

  44. retq: 返回函数,将栈帧指针弹出到程序计数器,并跳转到程序的结束点。

  45. .cfi_endproc: 声明函数的结束,停止生成调试信息。

  46. .section __TEXT,__cstring,cstring_literals: 定义一个新的段(section),名为__TEXT,__cstring,用于存放c字符串字面量。

  47. L_.str: .asciz "%d + %d = %d": 定义一个c字符串字面量"%d + %d = %d",并使用标签L_.str进行引用。

  48. .subsections_via_symbols: 告诉编译器按照符号(symbol)来生成子段(subsections)。

以上就是汇编代码的逐行解释,希望能帮助你理解代码的功能和执行流程。

前言

以下所有内容来自我对<<CSS世界>>的摘录和总结,感谢原作者辛苦著书。

1. 拥抱流布局

  1. 布局(layout)
    1. 响应式与媒体查询
    2. flexible box layout
    3. grid layout
  2. 视觉表现
    1. corner、shadow、progressive
    2. transform
    3. fitler
    4. animatation

2. 术语

1
2
3
4
5
.track-item {
width: 480px;
height : 640px;
color : 'red';
}
  1. 属性
    heightcolor就是属性。

类型 说明
int 属于number
number 浮点数
percent 50%
length 99px
color #999
  1. 关键字
    transparent

  2. 变量
    currentColor

  3. 长度单位

<number> + 长度单位 = <length>

  1. 功能符
    rgba(0,0,0,5)
    url('css-world.png')

  2. 属性值
    1px solid rgb(0,0,0) <- 值+关键字+功能符

  3. 声明
    color : transparent

  4. 声明块

1
2
3
4
{
width : 10%,
color : 'red'
}
  1. 规则或规则集
1
2
3
4
5
.track-item {
width: 480px;
height : 640px;
color : 'red';
}
  1. 选择器
选择器 符号 优先级
类选择器 ‘.’ 默认
ID选择器 ‘#’
属性选择器 ‘[]’
伪类选择器 ‘:’
伪元素选择器 ‘::before’

3. 布局–从block开始

3.1 block-level element

元素 display
<div> block
<li> list-item
<table> block

一个水平流上只能单独显示一个元素,多个块级元素则换行显示。
正是由于块级元素具有换行特性,因此理论上它都可以配合clear属性
来清除浮动带来的影响。

简化layout解释

a. 不考虑list-item的情况下

盒子 职责
block-level box 结构
inline box 内容

b. 考虑list-item当前情况下

盒子 职责
block-level box 主块级盒子
inline box 内容
list-item 附加盒子

c. inline-block加入战场

盒子 职责
block-level box 主块级盒子
inline box 内容
list-item 附加盒子
inline-block 容器盒子

d. 可视化盒模型

前言

本文是6. From Substitution to Environments的实验记录。

计划

  • Define a environment
  • Implement lookup in environment

codes

  1. Define a environment as list.
1
2
3
(define-type Binding
[bind (name : symbol) (val : number)])
(define-type-alias Env (listof Binding))
  1. Implement lookup in environment
1
2
3
4
5
6
7
8
; DONE : Define lookup
(define (lookup [for : symbol] [env : Env]) : number
(cond
[(empty? env) (error 'lookup "name not found")]
[else (cond
[(symbol=? for (bind-name (first env)))
(bind-val (first env))]
[else (lookup for (rest env))])]))
  1. Substitution VS Env(1st version)
1
2
3
4
5
6
7
8
9
10
11
; Error case in our implementation of Env 1st.
; Our test case equal to
; (define (f1 x) (f2 4))
; (define (f2 y) (+ x y))
; (f1 3)
; If we take a look, we will find error binding that x
; is not binded in f2's definition.
(interp (appC 'f1 (numC 3))
mt-env
(list (fdC 'f1 'x (appC 'f2 (numC 4)))
(fdC 'f2 'y (plusC (idC 'x) (idC 'y)))))
  1. detect error occus
1
2
3
4
5
; env cross scope
<appC-interp-bind-in-env>
(extend-env (bind (fdC-arg fd)
(interp a env fds))
env)
  1. Corret last error binding
1
2
3
4
5
; change env to mt-env
<appC-interp-bind-in-env>
(extend-env (bind (fdC-arg fd)
(interp a env fds))
mt-env)
  1. Scope in 3 terms
    6.1. Is it bound?
    6.2. where?
    6.3. Does it has parent scope?

系列文章

本系列文章旨在从零实现一个完整的解释器:

  1. 0x06 - 环境实现 - 从代换到环境
  2. 0x07 - 闭包与可变量 - 支持赋值和闭包

查看完整系列 →

0%: 从0开始的Hexo流程图探险

15%: Graphviz?Dot?这都是什么🧎‍♀️?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
digraph demo4 {
label=<<B>Graphviz基本组成结构</B>>;

labelloc=t; // 标题位置
bgcolor=white; // 背景透明

node[shape=box]; // 结点形状为方形
//edge[style=bold];

// 独立出现的为结点或属性声明, 中括号前为结点名称
graphviz[label="Graphviz"];

subgraph{
layout[label="Layouts"];
script[label="Script Files"];
api[label="APIs"];
rank=same;
}

graphviz -> layout;
graphviz -> script;
graphviz -> api;

// 设置子图
api ->
subgraph{
layout_etc[label="......"];
}

script ->
subgraph{
element[label="Elements"];
attribute[label="Attributes"];
rank=same;
}

layout ->
subgraph{
layout_dot[label="dot"];
layout_neato[label="neato"];
}

element ->
subgraph{
ele_graph[label="Graph"];
ele_node[label="Node"];
ele_edge[label="Edge"];
}
}
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;
First Term
This is the definition of the first term.
Second Term
: This is one definition of the second term.
This is another definition of the second term.

从零开始的算法历险 – 0x01

1. 从两数之和(2-Sum)开始

1.1 问题描述

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

1.2 问题分析

1.3 实现代码

1. 基本使用实例

1.1 一个C程序的内部调用图

如图1.1.a 所示


如图1.1.b 所示

2. 从基本结构开始探险

2.1 Node

如图 2.1.a 所示

1
2
3
4
5

diagraph G {
main [label="我是一个☝️节点,名字叫做main"]
}

0%