Lecture 4 Assembly Language and Computer Architecture

Lecture 4 Assembly Language and Computer Architecture

课程内容

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

The Four Stages of Compilation

Sourcebitarray.c main.c
Preprocessclang -E
Preprocessed sourcebitarray.i main.i
Compileclang -S
Assemblybitarray.s main.s
Assembleclang -c
Object Filebitarra.o main.o Libraries
Linkld
Binary Executableeverybit

Assembly Code

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

Disassembling

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

objdump -S exe

Why Assembly?

  • 汇编语言揭示了编译器做了什么和没做什么。
  • bug可能会在低层级上出现。比如说,bug可能只会在用-O3编译的时候出现。另外,编译器可能就是bug的原因。
  • 做优化的时候,可以手动更改编译。
  • 逆向工程(Reverse Engineering)。

Expectations of Students

  • 了解如何使用x86指令实现C语言的结构。
  • 借助手册,可以阅读x86汇编语言。
  • 借助一般的编译器行为了解高层次的性能优化。
  • 可以修改编译器产生的x86汇编语言。
  • 使用**编译器内部函数(compiler intrinsic functions)**来使用C并不能直接支持的汇编指令。
  • 手写汇编语言。

x86-64 ISA Primer

ISA: instruction set architecture

Registers

有两个特别需要注意的:

  • 16个128位的%xmm[0-15]的XMM registers (for SSE).
  • 16个256位%ymm[0-15]的YMM registers (for AVX).

Data Types

Opcode

RFLAGS Register

Direct Addressing Modes

  • 需要一个时钟周期来从寄存器取数,但是需要几百个时钟周期来从内存取数。
  • Instruction-pointer relative: The address is indexed relative to %rip.
movq 172(%rip), %rdi
  • Base Indexed Scale Displacement: Address = Base + Index * Scale + Displacement. Often use in stack frame.
movq 172(%rdi, %rdx, 8), %rax

Assembly Idiom

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

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

Floating-Point And Vector Hardware

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

  • SSE和AVX指令支持单精度和双精度标量的浮点数数学计算。(即float、double)
  • x87指令支持单、双、扩展精度的浮点数数学计算。(即float、double、long double)
  • SSE和AVX也支持向量指令。

SSE

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

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

ssOne single-precision floating-point value
sdOne double-precision floating-point value
psVector of single-precision floating-point values
pdVector of double-precision floating-point values
  • The first letter: s (single) or p (packed).
  • The second letter: s (single-precision) or d (double-precision).

Vector Hardware

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

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

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

  • 一个向量的第i个元素只可以参与与另一个向量的第i个元素。
  • 所有通道对相应的向量的元素执行同样的操作。
  • 根据架构的不同,向量内存可能需要对齐。
  • 一些架构支持通道之间的操作。

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

  • 现代SSE、AVX指令集支持在整数、单精度、双精度的浮点数向量操作。
  • 现代AVX指令集支持在单精度、双精度的浮点数向量操作。
  • AVX2指令集增加了对整数向量的支持。
  • AVX-512 (AVX3) 指令将寄存器长度增加到512位,并且提供了新的向量操作。

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

  • SSE指令使用128位XMM向量寄存器,且可以同时操作至多两个操作数。
  • AVX也可以使用256位的YMM向量寄存器,并且可以操作3个操作数:2个源操作数,1个目的操作数。
Vaddpd %ymm0, %ymm1, %ymm2

Example Opcodes:

SSEAVX/AVX2
Floating-pointaddpdvaddpd
Integerpaddqvpaddq
  • p 区别是整数还是浮点数。
  • v 区别是SSE还是AVX/AVX2.

Overview of Computer Architecture

5-Stage Processor

IF
ID
EX
MA
WB
Fetch Unit
Decode GPRS
ALU
Data Mem.
WB

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

  • 向量硬件
  • 超标量处理
  • 乱序执行
  • 分支预测

其他

  • 教授的话:If you really want to understand something, you need to understand it to the level that’s necessary and the one level below that. It’s not that you’ll necessarily use that one level below it, but that gives you insight as to why that layer is what it is and what’s really going on.
  • GPR: General Purpose Register
A Revised and Updated Edition of the Authoritative Text This revised and updated Third Edition of the classic text guides students through assembly language using a hands-on approach, supporting future computing professionals with the basics they need to understand the mechanics and function of the computer’s inner workings. Through using real instruction sets to write real assembly language programs, students will become acquainted with the basics of computer architecture. 80×86 Assembly Language and Computer Architecture covers the Intel 80×86 using the powerful tools provided by Microsoft Visual Studio, including its 32- and 64-bit assemblers, its versatile debugger, and its ability to link assembly language and C/C++ program segments. The text also includes multiple examples of how individual 80×86 instructions execute, as well as complete programs using these instructions. Hands-on exercises reinforce key concepts and problem-solving skills. Updated to be compatible with Visual Studio 2012, and incorporating over a hundred new exercises, 80×86 Assembly Language and Computer Architecture: Third Edition is accessible and clear enough for beginning students while providing coverage of a rich set of 80×86 instructions and their use in simple assembly language programs. The text will prepare students to program effectively at any level. Key features of the fully revised and updated Third Edition include: Updated to be used with Visual Studio 2012, while remaining compatible with earlier versions Over 100 new exercises and programming exercises Improved, clearer layout with easy-to-read illustrations The same clear and accessibly writing style as previous editions Full suite of ancillary materials, including PowerPoint lecture outlines, Test Bank, and answer keys Suitable as a stand-alone text in an assembly language course or as a supplement in a computer architecture course
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值