CMU Computer Systems: Machine-Level Programming (Data)

142 阅读2分钟

Data

  • Arrays

    • One-dimensional
    • Multi-dimensional
    • Multi-level
  • Structures

    • Allocation
    • Access
    • Alignment
  • Floating Point

Array Allocation

  • Basic Principle

    • T A[L]
    • Array of data type T and length L
    • Contiguously allocated region of L * sizeof (T) bytes in memory
  • Reference

    • &A[L]
    • *A

Multidimensional (Nested) Arrays

  • Declaration

    • T A[R][C]
    • 2D array of data type T
    • R rows, C columns
    • Type T element requires K bytes
  • Array Size

    • R * C * K bytes
  • Arrangement

    • Row-Major Ordering

Nested Array Row Access

  • Row Vectors

    • A[i] is array of C elements
    • Each element of type T requires K bytes
    • Starting address A + i*(C*K)

Nested Array Element Access

  • Array Elements

    • A[i][j] is element of type T, which requires L bytes
    • Address A + i * (C* K) + j * K = A + (i * C + j) * K

Element Access in Multi-Level Array

  • Computation

    • Element access Mem[Mem[univ+8index]+4digit]

    • Must do two memory reads

      • First get pointer to row array
      • Then access element within array

N x N Matrix

  • Fixed dimensions

    • Know value of N at compile time
  • Variable dimensions, explicit indexing

    • Traditional way to implement dynamic arrays
  • Variable dimensions, implicit indexing

    • Now supported by gcc

Structure Representation

  • Structure represented as block of memory

    • Big enough to hold all of the fields
  • Fields ordered according to declaration

    • Even if another ordering could yield a more compact representation
  • Compiler determines overall size + positions of fields

    • Machine-level program has no understanding of the structures in the source code

Alignment Principles

  • Aligned Data

    • Primitive data type requires K bytes
    • Address must be multiple of K
    • Required on some machines; advised on x86-64
  • Motivation for Aligning Data

    • Memory accessed by (aligned) chunks of 4 or 8 bytes (system dependent)

      • Inefficient to load or store datum that spans quad word boundaries
      • Virtual memory trickier when datum spans 2 pages
  • Compiler

    • Inserts gaps in structure to ensure correct alignment of fields

Specific Cases of Alignment (x86-64)

  • 1 byte: char, …

    • no restrictions on address
  • 2 bytes: short, …

    • lowest 1 bit of address must be 0 2 _2 2​
  • 4 bytes: int, float, …

    • lowest 2 bits of address must be 00 2 _2 2​
  • 8 bytes: double, long, char *, …

    • lowest 3 bits of address must be 000 2 _2 2​
  • 16 bytes: long double

    • lowest 4 bits of address must be 0000 2 _2 2​

Satisfying Alignment with Structures

  • Within structure:

    • Must satisfy each element’s alignment requirement
  • Overall structure placement

    • Each structure has alignment requirement K

      • K = Largest alignment of any element
    • Initial address & structure length must be multiples of K

Floating Point Basics

  • Arguments passed in %xmm0, %xmm1, …
  • Result returned in %xmm0
  • All XMM registers caller-saved

FP Memory Referencing

  • Integer (and pointer) arguments passed in regular registers
  • FP values passed in XMM registers
  • Different mov instructions to move between XMM registers, and between memory and XMM registers

Other Aspects of FP code

  • Lots of instructions

    • Different operations, different formats, …
  • Floating-point comparisons

    • Instructions ucomiss and ucomisd
    • Set condition codes CF, ZF, and PF
  • Using constant values

    • Set XMM0 register to 0 with instruction xorpd %xmm0, %xmm0
    • Others loaded from memory