Skip to content

📚 Machine-Level Programming IV: Data

约 226 个字 40 行代码 7 张图片 预计阅读时间 2 分钟 共被读过 1

15-213/15-513: Introduction to Computer Systems
6th Lecture, Sept 12, 2024


🧮 Arrays

One-Dimensional Arrays

  • Declaration: T name[Length];
  • Contiguously allocated region of Length * sizeof(T) bytes.
  • Access:
  • Identifier name acts as a pointer to element 0.
  • Example: val translates to *(val + 4).

Example: Array Allocation

C
#define ZLEN 5
typedef int zip_dig[ZLEN];
zip_dig cmu = {1, 5, 2, 1, 3};

- Memory layout (20-byte block):

Multi-Dimensional (Nested) Arrays

  • Declaration: T A[R][C];
  • Row-Major Order: Elements stored row-wise.
  • Row Access: A[i] starts at A + i * (C * sizeof(T)).
  • Element Access: A[i][j] address = A + (i * C + j) * sizeof(T).

Example: 2D Array

C
int pgh = {
  {1,5,2,0,6}, {1,5,2,1,3}, 
  {1,5,2,1,7}, {1,5,2,2,1}
};

- Memory layout:

Assembly for Element Access

Text Only
# Access pgh[index][dig]
leaq (%rdi, %rdi, 4), %rax   # 5 * index
addl %rax, %rsi              # 5*index + dig
movl pgh(, %rsi, 4), %eax    # Mem[pgh + 4*(5*index + dig)]

Multi-Level Arrays

  • Declaration: Array of pointers to arrays.
    C
    int* univ = {mit, cmu, ucb};
    
  • Memory layout:

Element Access

  • Requires two memory reads:
    Text Only
    salq $2, %rsi              # 4 * digit
    addq univ(,%rdi,8), %rsi   # p = univ[index] + 4*digit
    movl (%rsi), %eax          # return *p
    

🏗️ Structures

Memory Allocation & Alignment

  • Alignment Rules:
  • Primitive type of size B must have address multiple of B.
  • Example:
    C
    struct S1 { char c; int i; double v; };
    

Structure Padding Example

C
struct S2 { double v; int i; char c; } a;

- Memory layout (each element padded to 24 bytes):

Linked List Example

C
long length(struct rec *r) {
  long len = 0L;
  while (r) { len++; r = r->next; }
  return len;
}

- Assembly:
Text Only
.L11:
  addq $1, %rax        # len++
  movq 24(%rdi), %rdi  # r = Mem[r + 24]
  testq %rdi, %rdi
  jne .L11

🎯 Floating Point

Basics

  • Registers: XMM registers (%xmm0, %xmm1, ...) for FP arguments/results.
  • Operations:
    Text Only
    addss %xmm1, %xmm0   # Single-precision add
    addsd %xmm1, %xmm0   # Double-precision add
    

Example: FP Function

C
double dincr(double *p, double v) {
  double x = *p;
  *p = x + v;
  return x;
}

- Assembly:
Text Only
movapd %xmm0, %xmm1   # Copy v
movsd (%rdi), %xmm0   # x = *p
addsd %xmm0, %xmm1    # t = x + v
movsd %xmm1, (%rdi)   # *p = t

📝 Quiz & Examples

Understanding Pointers & Arrays #1

Decl Comp Bad Size A1/A2 Comp Bad Size
int A1 Y N 12 Y N 4
int *A2 Y N 8 Y Y 4

Array Access Example

C
int result = pgh + linear_zip + *(linear_zip + 8) + zip2;
// Result: 9

🔍 Summary

  • Arrays: Contiguous memory, index arithmetic for access.
  • Structures: Compiler-managed padding for alignment.
  • Floating Point: XMM registers and SIMD operations.

⚠️ Key Reminder: Always consider alignment and pointer arithmetic nuances in low-level code!