Skip to the content.

Lecture 4 Assembly Language and Computer Architecture

课程内容

这节课主要是将了汇编语言、计算机架构的基础部分。

The Four Stages of Compilation

| | | |-|-| | Source | bitarray.c main.c | | Preprocess | clang -E | | Preprocessed source | bitarray.i main.i | | Compile | clang -S | | Assembly | bitarray.s main.s | | Assemble | clang -c | | Object File | bitarra.o main.o Libraries | | Link | ld | | Binary Executable | everybit |

Assembly Code

汇编语言提供了一个方便机器码的符号化标识。

Disassembling

编译时使用-g标志。使用命令:

objdump -S exe

Why Assembly?

Expectations of Students

x86-64 ISA Primer

ISA: instruction set architecture

Registers

有两个特别需要注意的:

Data Types

Opcode

RFLAGS Register

Direct Addressing Modes

  1. 看寄存器是否是0.
    test %rcx, %rcx
    
  2. No-operation (no-op) instructions
    data16 data16 data16 now %cs:0x0(%rax,%rax,1)
    

    指令本身并不做任何事情。只是优化一些指令的内存(如代码大小、对齐等等)。

Floating-Point And Vector Hardware

现代x86-64架构通过一些不同的指令集支持标量(非向量)浮点数数学计算。

SSE

编译器更喜欢使用SSE指令而不是x87指令,因为SSE指令编译、优化更简单。

SSE使用XMM寄存器和浮点数类型。

   
ss One single-precision floating-point value
sd One double-precision floating-point value
ps Vector of single-precision floating-point values
pd Vector of double-precision floating-point values

Vector Hardware

现代微处理器一般结合向量硬件来处理数据,使用的是single-instruction stream, multiple-data stream (SIMD) 方式。

每一个向量寄存器存放k个标量整数或浮点数。向量单元存放k个向量通道(Vector Lane),每一个包含标量整数或浮点数硬件。所有向量通道在一个时钟周期内使用同样的指令和控制信号。其中,k是向量宽度。

向量指令一般是elementwise运行的:

现代x86-64架构支持多个向量指令集。

AVX和AVX2指令集增加了SSE的功能:

Example Opcodes:

  SSE AVX/AVX2
Floating-point addpd vaddpd
Integer paddq vpaddq

Overview of Computer Architecture

5-Stage Processor

graph LR;
A[IF] --> B[ID] --> C[EX] --> D[MA] --> E[WB]
graph LR;
A[Fetch Unit] --> B [Decode -> GPRS] --> C[ALU] --> D[Data Mem.] --> E[WB]

现代处理器的一些设计特点

其他