系统级程序设计 - 机器层面编程 1: 基础

History of Intel processors and architectures

  • Complex instruction set computer (CISC)
    – Many different instructions with many different formats
    – But, only small subset encountered with Linux programs
  • Hard to match performance of Reduced Instruction Set Computers
    (RISC)
    – But, Intel has done just that!
    – In terms of speed. Less so for low power.

C, assembly, machine code

Definitions:

  • Architecture: The part of a processor design that one needs to understand or write assembly/machine code.
  • Microarchitecture: Implementation of the architecture

Assembly/Machine Code View

在这里插入图片描述
Programmer - Visible State

  1. PC: Program counter
    –Address of next instruction
    –Called “RIP”
  2. Register file
    –Heavily used program data
  3. Condition codes
    – Store status information about most recent
    arithmetic or logical operation
    – Used for conditional branching
  4. Memory
    – Byte addressable array
    – Code and user data
    – Stack to support procedures

Turning C into Object Code

在这里插入图片描述

Compiling Into Assembly

gcc -Og -S Test.c

Data Types

  • “Integer” data of 1, 2, 4, or 8 bytes
    – Data values
    – Addresses (untyped pointers)
  • Floating point data of 4, 8, or 10 bytes
  • Code: Byte sequences encoding series of instructions
  • No aggregate types such as arrays or structures
    – Just contiguously allocated bytes in memory

Operations

  • 在寄存器或者内存的数据上做运算操作
  • 传输数据在内存和寄存器之间
  • 传输控制
    – 无条件跳转
    – 条件分支

Object Code

Assembler

  • Translates .s into .o
  • Binary encoding of each instruction
  • Nearly-complete image of executable code
  • Missing linkages between code in different files

Linker

  • Resolves references between files
  • Combines with static run-time libraries
    – E.g.,codeformalloc,printf
  • Some libraries are dynamically linked
    – Linking occurs when program begins execution

Assembly Basics: Registers, operands, move

X86-64 Integer Register

在这里插入图片描述

Moving Data

movq Source,Dest:
Operand Types

  1. Immediate: Constant integer data
  • Example: $0x400, $-533
  • Like C constant, but prefixed with ‘$’
  • Encoded with 1, 2, or 4 bytes
  1. Register: One of 16 integer registers
  • Example: %rax, %r13
  • But %rsp reserved for special use
  • Others have special uses for particular instructions
  1. Memory: 8 consecutive bytes of memory at address given by register
  • Simplest example: (%rax)
  • Various other “address modes”

movq Operand Combinations

不能做内存到内存到转换通过将单一指令
在这里插入图片描述

Simple Memory Addressing Modes

在这里插入图片描述

Example

在这里插入图片描述

Complete Memory Addressing Modes

Most General Form

D(Rb,Ri,S)
Mem[Reg[Rb]+S*Reg[Ri]+ D]
D: Constant “displacement” 1, 2, or 4 bytes
Rb: Base register: Any of 16 integer registers
Ri: Index register: Any, except for %rsp
S: Scale: 1, 2, 4, or 8 (why these numbers?)

Special Cases

(Rb,Ri)==>Mem[Reg[Rb]+Reg[Ri]]

D(Rb,Ri) ==>Mem[Reg[Rb]+Reg[Ri]+D]

(Rb,Ri,S)==> Mem[Reg[Rb]+S*Reg[Ri]]

Arithmetic & logical operations

Address Computation Instruction

leaq Src, Dst

  • Src is address mode expression
  • Set Dst to address denoted by expression

Uses

  • Computing addresses without a memory reference
    – E.g., translation of p = &x[i];
  • Computing arithmetic expressions of the form x + k*y
    – k = 1, 2, 4, or 8
    在这里插入图片描述

Some Arithmetic Operations

Two Operand Instructions:

在这里插入图片描述

Arithmetic Expression Example

在这里插入图片描述
在这里插入图片描述

Summary

History of Intel processors and architectures

Evolutionary design leads to many quirks and artifacts

C, assembly, machine code

  • New forms of visible state: program counter, registers …
  • Compiler must transform statements, expressions, procedures into low-level instruction sequences

Assembly Basics: Registers, operands, move

  • The x86-64 move instructions cover wide range of data movement forms

Arithmetic

  • C compiler will figure out different instruction combinations to carry out computation
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值