Machine-Level Programming IV - Data

Class: CSCE-312


Notes:

Today

Array

Array Allocation

Pasted image 20251007135204.png|550

Array Access

Reference	Type	Value
val[4]	    int	    3
val	        int *	x
val+1	    int *	x + 4    
&val[2]	    int *	x + 8   
val[5]	    int	    ??
*(val+1)	int	    5          
val + i	    int *	x + 4i

Array Example

Pasted image 20251007135801.png|500

Notes:

Array Accessing Example

Pasted image 20251007140014.png|500

IA32

  # %rdi = z
  # %rsi = digit
movl (%rdi,%rsi,4), %eax  # z[digit]

Array Loop Example

C code:

void zincr(zip_dig z) {
  size_t i;
  for (i = 0; i < ZLEN; i++)
    z[i]++;
}

Assembly code:

# %rdi = z
	movl    $0, %eax          #   i = 0
	jmp     .L3               #   goto middle
.L4:                        # loop:
	addl    $1, (%rdi,%rax,4) #   z[i]++
	addq    $1, %rax          #   i++
.L3:                        # middle
	cmpq    $4, %rax          #   i:4 (because array size is 5)
	jbe     .L4               #   if <=, goto loop
	rep; ret

Multidimensional (Nested) Arrays

Pasted image 20251016125103.png|450

Nested Array Example

Pasted image 20251016125257.png|450

Notes:

Nested Array Row Access

Pasted image 20251016125512.png|450

Nested Array Row Access Code

Pasted image 20251016133401.png|400

C code:

int *get_pgh_zip(int index)
{
  return pgh[index];
}

Assembly code for A + i * (C * K):

# %rdi = index
	leaq (%rdi,%rdi,4),%rax	  # 5 * index
	leaq pgh(,%rax,4),%rax	  # pgh + (20 * index)

Notes:

Nested Array Element Access

Pasted image 20251016134002.png|500

Nested Array Element Access Code

Pasted image 20251016133401.png|400

C code:

int get_pgh_digit
  (int index, int dig)
{
  return pgh[index][dig];
}

Assembly code for A + i * (C * K) + j * K:

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

Notes:

Multi-Level Array Example

zip_dig cmu = { 1, 5, 2, 1, 3 };
zip_dig mit = { 0, 2, 1, 3, 9 };
zip_dig ucb = { 9, 4, 7, 2, 0 };

#define UCOUNT 3
int *univ[UCOUNT] = {mit, cmu, ucb};

Pasted image 20251016134434.png|500

Element Access in Multi-Level Array

C code:

int get_univ_digit
  (size_t index, size_t digit)
{
  return univ[index][digit];
}

Assembly code:

salq    $2, %rsi              # 4*digit
addq    univ(,%rdi,8), %rsi   # p = univ[index] + 4*digit
movl    (%rsi), %eax          # return *p
ret

Pasted image 20251016134623.png|400

NXN Matrix Code

Pasted image 20251016135046.png|350

16 X 16 Matrix Access

C code:

/* Get element a[i][j] */
int fix_ele(fix_matrix a, size_t i, size_t j) {
  return a[i][j];
}

Assembly code:

# a in %rdi, i in %rsi, j in %rdx
salq    $6, %rsi             # 64*i              -> (C * K)
addq    %rsi, %rdi           # a + 64*i          -> (A + i * (C * K))
movl    (%rdi,%rdx,4), %eax  # M[a + 64*i + 4*j] -> (A + i * (C * K) + j * K)
ret

n X n Matrix Access

C code:

/* Get element a[i][j] */
int var_ele(size_t n, int a[n][n], size_t i, size_t j) {
  return a[i][j];
}

Assembly code:

  # n in %rdi, a in %rsi, i in %rdx, j in %rcx
  imulq   %rdx, %rdi           # n*i
  leaq    (%rsi,%rdi,4), %rax  # a + 4*n*i
  movl    (%rax,%rcx,4), %eax  # a + 4*n*i + 4*j
  ret

Structures

Structure Representation

Pasted image 20251021125012.png|450

Note:

Generating Pointer to Structure Member

Pasted image 20251021125914.png|450

C code:

int *get_ap
 (struct rec *r, size_t idx)
{
  return &r->a[idx];
}

Assembly code:

  # r in %rdi, idx in %rsi  
  leaq  (%rdi,%rsi,4), %rax
  ret

Following Linked List

C code:

struct rec {
    int a[4];
    int i;
    struct rec *next;
};

void set_val
  (struct rec *r, int val)
{
  while (r) {
    int i = r->i;
    r->a[i] = val;
    r = r->next;
  }
}

Assembly code:

.L11:                         # loop:
  movslq  16(%rdi), %rax      #   i = M[r+16]	  
  movl    %esi, (%rdi,%rax,4) #   M[r+4*i] = val
  movq    24(%rdi), %rdi      #   r = M[r+24]
  testq   %rdi, %rdi          #   Test r
  jne     .L11                #   if !=0 goto loop

But this assembly code is not quite right!

Structures & Alignment

Alignment Principles

Specific Cases of Alignment (x86-64)

Example question:

Satisfying Alignment with Structures

"We have to satisfy alignment within structures and between structures"

Meeting Overall Alignment Requirement

Arrays of Structures

Pasted image 20251021132714.png|500 Pasted image 20251021132724.png|150

Accessing Array Elements

Pasted image 20251021132820.png|500 Pasted image 20251021132831.png|150

Saving Space

Floating Point

Floating point refers to how computers represent and process real numbers (numbers with decimals) — like 3.14159 or -0.0001 — rather than integers.

Because floating-point math is more complex (it involves exponents, rounding, precision, etc.), CPUs historically used special hardware and instructions to handle it efficiently.

Background

Fun facts:

Programming with SSE3

XMM Registers

Scalar & SIMD Operations

Note: the actual representation of floating point numbers, as we saw with int's, is much more complex, way may look at it later.

FP Basics

Pasted image 20251021135417.png|550

FP Memory Referencing

Pasted image 20251021135720.png|450

Other Aspects of FP Code

Summary

...

There are some exercises on the last few slides of Machine-Level Programming IV - Data