Docsity

Data Structures in Assembly: Arrays, Structs, Alignment Principles, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structures in Assembly: Arrays, Structs, Alignment Principles and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures in Assembly

¢ Arrays

§ One-­‐dimensional

§ Mul/-­‐dimensional (nested)

§ Mul/-­‐level

¢ Structs

§ Alignment

¢ Unions

struct rec { int i; int a[3]; int* p; };

Structures

¢ Characteris;cs

§ Con/guously-­‐allocated region of memory

§ Refer to members within structure by names

§ Members may be of different types

Memory Layout

i a p

struct rec { int i; int a[3]; int* p; };

IA32 Assembly

%eax = val

%edx = r

movl %eax,0(%edx) # Mem[r+0] = val void set_i(struct rec* r, int val) { r->i = val; }

Structures

¢ Accessing Structure Member

§ Given an instance of the struct, we can use

the. operator, just like Java:

§ struct rec r1; r1.i = val;

§ What if we have a pointer to a struct: struct rec* r = &r1;

§ Using * and. operators:

§ Or, use -> operator for short:

§ Pointer indicates first byte of structure; access members with offsets

(*r).i = val;

r->i = val;

%ecx = idx

%edx = r

leal 0(,%ecx,4),%eax # 4idx leal 4(%eax,%edx),%eax # r+4idx+ int* find_address_of_elem (struct rec* r, int idx) { return &r->a[idx]; }

Genera;ng Pointer to Structure Member

¢ Genera;ng Pointer to

Array Element

§ Offset of each structure

member determined

at compile /me

struct rec { int i; int a[3]; int* p; };

i a p

r r+4+4*idx

&(r->a[idx])

%ecx = idx

%edx = r

movl 4(%eax,%edx,4),%eax # Mem[ r+4idx+4 ] int find_address_of_elem (struct rec* r, int idx) { return &r->a[idx]; }

Accessing to Structure Member

¢ Reading Array Element

§ Offset of each structure

member s)ll determined

at compile /me

struct rec { int i; int a[3]; int* p; };

i a p

r r+4+4*idx

Structures & Alignment

¢ Unaligned Data

¢ How would it look like if data items were aligned (address

mul.ple of type size)?

c v i

p p+1 p+9 p+ struct S1 { char c; double v; int i; } * p;

Alignment Principles

¢ Aligned Data

§ Primi/ve data type requires K bytes

§ Address must be mul/ple of K

¢ Aligned data is required on some machines; it is advised

on IA

§ Treated differently by IA32 Linux, x86-­‐64 Linux, Windows, Mac OS X, …

¢ What is the mo;va;on for alignment?

Alignment Principles

¢ Aligned Data

§ Primi/ve data type requires K bytes

§ Address must be mul/ple of K

¢ Aligned data is required on some machines; it is advised

on IA

§ Treated differently by IA32 Linux, x86-­‐64 Linux, Windows, Mac OS X, …

¢ Mo;va;on for Aligning Data

§ Physical memory is accessed by aligned chunks of 4 or 8 bytes (system-­‐

dependent)

§ Inefficient to load or store datum that spans these boundaries

§ Also, virtual memory is very tricky when datum spans two pages (later…)

¢ Compiler

§ Inserts padding in structure to ensure correct alignment of fields

§ sizeof() should be used to get true size of structs

Saving Space

¢ Put large data types first:

¢ Effect (example x86-­‐64, both have K=8)

p+0 p+

c 7 bytes v i

p+16 p+ struct S1 { char c; double v; int i; } * p; struct S2 { double v; int i; char c; } * q; q+

v i

q+8 q+

c But actually…

q+

Struct Alignment Principles

¢ Size must be a mul;ple of the largest primi;ve type inside.

p+0 p+

c 7 bytes v i

p+16 p+ q+

v i

q+8 q+

c 3 bytes

q+

K = 8 so size mod 8 = 0

Unions

¢ Allocated according to largest element

¢ Can only use one member at a ;me

union U { char c; int i[2]; double v; } *up; struct S { char c; int i[2]; double v; } *sp; c 3 bytes^ i[0] i[1] 4 bytes^ v sp+0 sp+4 sp+8 sp+16 sp+ c i[0] i[1] v up+0 up+4 up+

What Are Unions Good For?

¢ Unions allow the same region of memory to be referenced as

different types

§ Different “views” of the same memory loca/on

§ Can be used to circumvent C’s type system (bad idea)

¢ Beger idea: use a struct inside a union to access some

memory loca;on either as a whole or by its parts

Summary

¢ Arrays in C

§ Con/guous alloca/ons of memory

§ No bounds checking

§ Can usually be treated like a pointer to first element

¢ Structures

§ Allocate bytes in order declared

§ Pad in middle and at end to sa/sfy alignment

¢ Unions

§ Provide different views of the same memory loca/on