Machine-Level%20Programming%20IV:%20Structured%20Data%20Apr.%2023,%202008 - PowerPoint PPT Presentation

About This Presentation
Title:

Machine-Level%20Programming%20IV:%20Structured%20Data%20Apr.%2023,%202008

Description:

Allocated contiguously. Each element is an array of 5 int's. Allocated contiguously 'Row-Major' ordering of all elements guaranteed #define PCOUNT 4 ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 42
Provided by: randa99
Category:

less

Transcript and Presenter's Notes

Title: Machine-Level%20Programming%20IV:%20Structured%20Data%20Apr.%2023,%202008


1
Machine-Level Programming IVStructured
DataApr. 23, 2008
EECS213
  • Topics
  • Arrays
  • Structures
  • Unions

2
Basic Data Types
  • Integral
  • Stored operated on in general registers
  • Intel GAS Bytes C
  • byte b 1 unsigned char
  • word w 2 unsigned short
  • double word l 4 unsigned int
  • Floating Point
  • Stored operated on in floating point registers
  • Intel GAS Bytes C
  • Single s 4 float
  • Double l 8 double
  • Extended t 10/12 long double

3
Array Allocation
  • Basic Principle
  • T AL
  • Array of data type T and length L
  • Contiguously allocated region of L sizeof(T)
    bytes

char p3
4
Array Access
  • Basic Principle
  • T AL
  • Array of data type T and length L
  • Identifier A can be used as a pointer to array
    element 0
  • Reference Type Value
  • val4 int 3
  • val int x
  • val1 int x 4
  • val2 int x 8
  • val5 int ??
  • (val1) int 5
  • val i int x 4 i

5
Array Example
typedef int zip_dig5 zip_dig cmu 1, 5, 2,
1, 3 zip_dig mit 0, 2, 1, 3, 9 zip_dig
ucb 9, 4, 7, 2, 0
  • Notes
  • Declaration zip_dig cmu equivalent to int
    cmu5
  • Example arrays were allocated in successive 20
    byte blocks
  • Not guaranteed to happen in general

6
Array Accessing Example
  • Computation
  • Register edx contains starting address of array
  • Register eax contains array index
  • Desired digit at 4eax edx
  • Use memory reference (edx,eax,4)

int get_digit (zip_dig z, int dig) return
zdig
  • Memory Reference Code

edx z eax dig movl
(edx,eax,4),eax zdig
7
Referencing Examples
  • Code Does Not Do Any Bounds Checking!
  • Reference Address Value Guaranteed?
  • mit3 36 4 3 48 3
  • mit5 36 4 5 56 9
  • mit-1 36 4-1 32 3
  • cmu15 16 415 76 ??
  • Out of range behavior implementation-dependent
  • No guaranteed relative allocation of different
    arrays

Yes
No
No
No
8
Array Loop Example
int zd2int(zip_dig z) int i int zi 0
for (i 0 i lt 5 i) zi 10 zi
zi return zi
  • Original Source
  • Transformed Version
  • As generated by GCC
  • Eliminate loop variable i
  • Convert array code to pointer code
  • Express in do-while form
  • No need to test at entrance

int zd2int(zip_dig z) int zi 0 int zend
z 4 do zi 10 zi z z
while(z lt zend) return zi
9
Array Loop Implementation
  • Registers
  • ecx z
  • eax zi
  • ebx zend
  • Computations
  • 10zi z implemented as z 2(zi4zi)
  • z increments by 4

int zd2int(zip_dig z) int zi 0 int zend
z 4 do zi 10 zi z z
while(z lt zend) return zi
int zd2int(zip_dig z) int zi 0 int zend
z 4 do zi 10 zi z z
while(z lt zend) return zi
int zd2int(zip_dig z) int zi 0 int zend
z 4 do zi 10 zi z z
while(z lt zend) return zi
int zd2int(zip_dig z) int zi 0 int zend
z 4 do zi 10 zi z z
while(z lt zend) return zi
int zd2int(zip_dig z) int zi 0 int zend
z 4 do zi 10 zi z z
while(z lt zend) return zi
ecx z xorl eax,eax zi 0 leal
16(ecx),ebx zend z4 .L59 leal
(eax,eax,4),edx 5zi movl (ecx),eax
z addl 4,ecx z leal (eax,edx,2),eax
zi z 2(5zi) cmpl ebx,ecx z
zend jle .L59 if lt goto loop
ecx z xorl eax,eax zi 0 leal
16(ecx),ebx zend z4 .L59 leal
(eax,eax,4),edx 5zi movl (ecx),eax
z addl 4,ecx z leal (eax,edx,2),eax
zi z 2(5zi) cmpl ebx,ecx z
zend jle .L59 if lt goto loop
ecx z xorl eax,eax zi 0 leal
16(ecx),ebx zend z4 .L59 leal
(eax,eax,4),edx 5zi movl (ecx),eax
z addl 4,ecx z leal (eax,edx,2),eax
zi z 2(5zi) cmpl ebx,ecx z
zend jle .L59 if lt goto loop
ecx z xorl eax,eax zi 0 leal
16(ecx),ebx zend z4 .L59 leal
(eax,eax,4),edx 5zi movl (ecx),eax
z addl 4,ecx z leal (eax,edx,2),eax
zi z 2(5zi) cmpl ebx,ecx z
zend jle .L59 if lt goto loop
ecx z xorl eax,eax zi 0 leal
16(ecx),ebx zend z4 .L59 leal
(eax,eax,4),edx 5zi movl (ecx),eax
z addl 4,ecx z leal (eax,edx,2),eax
zi z 2(5zi) cmpl ebx,ecx z
zend jle .L59 if lt goto loop
10
Array reference quiz
  • Assume short S, int i
  • edx S0, ecx i
  • For each expression, give its type and the
    assembly code to store its value in eax, if
    pointer, or ax if short

Expression Type Code
S1
S3
Si
S4i1
Si-5
11
Nested Array Example
define PCOUNT 4 zip_dig pghPCOUNT 1, 5,
2, 0, 6, 1, 5, 2, 1, 3 , 1, 5, 2, 1, 7
, 1, 5, 2, 2, 1
  • Declaration zip_dig pgh4 equivalent to int
    pgh45
  • Variable pgh denotes array of 4 elements
  • Allocated contiguously
  • Each element is an array of 5 ints
  • Allocated contiguously
  • Row-Major ordering of all elements guaranteed

12
Nested Array Allocation
  • Declaration
  • T ARC
  • 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

int ARC
4RC Bytes
13
Nested Array Row Access
  • Row Vectors
  • Ai is array of C elements
  • Each element of type T
  • Starting address A i C K

int ARC
  
  
A
AiC4
A(R-1)C4
14
Nested Array Row Access Code
int get_pgh_zip(int index) return
pghindex
  • Row Vector
  • pghindex is array of 5 ints
  • Starting address pgh20index
  • Code
  • Computes and returns address
  • Compute as pgh 4(index4index)

eax index leal (eax,eax,4),eax 5
index leal pgh(,eax,4),eax pgh (20 index)
15
Nested Array Element Access
  • Array Elements
  • Aij is element of type T
  • Address A (i C j) K

A i j
int ARC
Ai
  
  
A i j
  
  
A
AiC4
A(R-1)C4
A(iCj)4
16
Nested Array Element Access Code
  • Array Elements
  • pghindexdig is int
  • Address
  • pgh 20index 4dig
  • Code
  • Computes address
  • pgh 4dig 4(index4index)
  • movl performs memory reference

int get_pgh_digit (int index, int dig)
return pghindexdig
ecx dig eax index leal
0(,ecx,4),edx 4dig leal (eax,eax,4),eax
5index movl pgh(edx,eax,4),eax (pgh
4dig 20index)
17
Strange Referencing Examples
zip_dig pgh4
  • Reference Address Value Guaranteed?
  • pgh33 7620343 148 2
  • pgh25 7620245 136 1
  • pgh2-1 762024-1 112 3
  • pgh4-1 762044-1 152 1
  • pgh019 76200419 152 1
  • pgh0-1 762004-1 72 ??
  • Code does not do any bounds checking
  • Ordering of elements within array guaranteed

Yes
Yes
Yes
Yes
Yes
No
18
Multi-Level Array Example
  • Variable univ denotes array of 3 elements
  • Each element is a pointer
  • 4 bytes
  • Each pointer points to array of ints

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 univUCOUNT mit, cmu,
ucb
19
Element Access in Multi-Level Array
int get_univ_digit (int index, int dig)
return univindexdig
  • Computation
  • Element access MemMemuniv4index4dig
  • Must do two memory reads
  • First get pointer to row array
  • Then access element within array

ecx index eax dig leal
0(,ecx,4),edx 4index movl univ(edx),edx
Memuniv4index movl (edx,eax,4),eax
Mem...4dig
20
Array Element Accesses
  • Similar C references
  • Nested Array
  • Element at
  • Mempgh20index4dig
  • Different address computation
  • Multi-Level Array
  • Element at
  • MemMemuniv4index4dig

int get_pgh_digit (int index, int dig)
return pghindexdig
int get_univ_digit (int index, int dig)
return univindexdig
21
Strange Referencing Examples
  • Reference Address Value Guaranteed?
  • univ23 5643 68 2
  • univ15 1645 36 0
  • univ2-1 564-1 52 9
  • univ3-1 ?? ??
  • univ112 16412 64 7
  • Code does not do any bounds checking
  • Ordering of elements in different arrays not
    guaranteed

Yes
No
No
No
No
22
Using Nested Arrays
define N 16 typedef int fix_matrixNN
  • Strengths
  • C compiler handles doubly subscripted arrays
  • Generates very efficient code
  • Avoids multiply in index computation
  • Limitation
  • Only works if have fixed array size

/ Compute element i,k of fixed matrix product
/ int fix_prod_ele (fix_matrix a, fix_matrix b,
int i, int k) int j int result 0 for
(j 0 j lt N j) result
aijbjk return result
23
Dynamic Nested Arrays
  • Strength
  • Can create matrix of arbitrary size
  • Programming
  • Must do index computation explicitly
  • Performance
  • Accessing single element costly
  • Must do multiplication

int new_var_matrix(int n) return (int )
calloc(sizeof(int), nn)
int var_ele (int a, int i, int j, int n)
return ainj
movl 12(ebp),eax i movl 8(ebp),edx
a imull 20(ebp),eax ni addl
16(ebp),eax nij movl (edx,eax,4),eax
Mema4(inj)
24
Dynamic Array Multiplication
  • Without Optimizations
  • Multiplies
  • 2 for subscripts
  • 1 for data
  • Adds
  • 2 for array indexing
  • 1 for loop index
  • 1 for data

/ Compute element i,k of variable matrix
product / int var_prod_ele (int a, int b,
int i, int k, int n) int j int result
0 for (j 0 j lt n j) result
ainj bjnk return result
25
Optimizing Dynamic Array Mult.
int j int result 0 for (j 0 j lt n
j) result ainj bjnk
return result
  • Optimizations
  • Performed when set optimization level to -O2
  • Code Motion
  • Expression in can be computed outside loop
  • Strength Reduction
  • Incrementing j has effect of incrementing jnk
    by n
  • Performance
  • Compiler can optimize regular access patterns

int j int result 0 int iTn in
int jTnPk k for (j 0 j lt n j)
result aiTnj bjTnPk jTnPk
n return result
26
Structures
  • Concept
  • Contiguously-allocated region of memory
  • Refer to members within structure by names
  • Members may be of different types
  • Accessing Structure Member

struct rec int i int a3 int p
Memory Layout
Assembly
void set_i(struct rec r, int val)
r-gti val
eax val edx r movl eax,(edx)
Memr val
27
Generating Pointer to Struct. Member
r
struct rec int i int a3 int p
i
a
p
0
4
16
r 4 4idx
  • Generating Pointer to Array Element
  • Offset of each structure member determined at
    compile time

int find_a (struct rec r, int idx) return
r-gtaidx
ecx idx edx r leal 0(,ecx,4),eax
4idx leal 4(eax,edx),eax r4idx4
28
Structure Referencing (Cont.)
  • C Code

struct rec int i int a3 int p
void set_p(struct rec r) r-gtp
r-gtar-gti
edx r movl (edx),ecx r-gti leal
0(,ecx,4),eax 4(r-gti) leal
4(edx,eax),eax r44(r-gti) movl
eax,16(edx) Update r-gtp
29
Structure reference quiz
  • Offsets in bytes for
  • p
  • s.x
  • s.y
  • next
  • Total bytes for prob?
  • struct prob
  • int p
  • struct
  • int x
  • int y
  • s
  • struct prob next

30
Structure reference quiz
  • Offsets in bytes for
  • p
  • s.x
  • s.y
  • next
  • Total bytes for prob?
  • struct prob
  • int p
  • struct
  • int x
  • int y
  • s
  • struct prob next

31
Structure reference quiz
  • Offsets in bytes for
  • p 0
  • s.x 4
  • s.y 8
  • next 12
  • Total bytes for prob? 16
  • struct prob
  • int p
  • struct
  • int x
  • int y
  • s
  • struct prob next

32
Structure reference quiz
void sp_init (struct prob sp) sp-gts.x
____7_____ sp-gtp ____8_____ sp-gtnext
____9_____
movl 8(ebp), eax ____________1______________ m
ovl 8(eax), edx ____________2______________ mo
vl edx, 4(eax) ____________3______________ lea
l 4(eax), edx ____________4______________ movl
edx, (eax) ____________5______________ movl
eax, 12(eax) ____________6______________
33
Structure reference quiz
void sp_init (struct prob sp) sp-gts.x
____7_____ sp-gtp ____8_____ sp-gtnext
____9_____
movl 8(ebp), eax put sp in eax movl 8(eax),
edx ____________2______________ movl edx,
4(eax) ____________3______________ leal
4(eax), edx ____________4______________ movl
edx, (eax) ____________5______________ movl
eax, 12(eax) ____________6______________
34
Structure reference quiz
void sp_init (struct prob sp) sp-gts.x
____7_____ sp-gtp ____8_____ sp-gtnext
____9_____
movl 8(ebp), eax put sp in eax movl 8(eax),
edx put sp-gts.y in edx movl edx, 4(eax)
____________3______________ leal 4(eax), edx
____________4______________ movl edx, (eax)
____________5______________ movl eax, 12(eax)
____________6______________
35
Structure reference quiz
void sp_init (struct prob sp) sp-gts.x
____7_____ sp-gtp ____8_____ sp-gtnext
____9_____
movl 8(ebp), eax put sp in eax movl 8(eax),
edx put sp-gts.y in edx movl edx, 4(eax)
put sp-gts.y in sp-gts.x leal 4(eax), edx
____________4______________ movl edx, (eax)
____________5______________ movl eax, 12(eax)
____________6______________
36
Structure reference quiz
void sp_init (struct prob sp) sp-gts.x
____7_____ sp-gtp ____8_____ sp-gtnext
____9_____
movl 8(ebp), eax put sp in eax movl 8(eax),
edx put sp-gts.y in edx movl edx, 4(eax)
put sp-gts.y in sp-gts.x leal 4(eax), edx put
(sp-gts.x) in edx movl edx, (eax)
____________5______________ movl eax, 12(eax)
____________6______________
37
Structure reference quiz
void sp_init (struct prob sp) sp-gts.x
____7_____ sp-gtp ____8_____ sp-gtnext
____9_____
movl 8(ebp), eax put sp in eax movl 8(eax),
edx put sp-gts.y in edx movl edx, 4(eax)
put sp-gts.y in sp-gts.x leal 4(eax), edx put
(sp-gts.x) in edx movl edx, (eax) put
(sp-gts.x) in sp-gtp movl eax, 12(eax)
____________6______________
38
Structure reference quiz
void sp_init (struct prob sp) sp-gts.x
____7_____ sp-gtp ____8_____ sp-gtnext
____9_____
movl 8(ebp), eax put sp in eax movl 8(eax),
edx put sp-gts.y in edx movl edx, 4(eax)
put sp-gts.y in sp-gts.x leal 4(eax), edx put
(sp-gts.y) in edx movl edx, (eax) put
(sp-gts.y) in sp-gtp movl eax, 12(eax) put sp
in sp-gtnext
39
Structure reference quiz
void sp_init (struct prob sp) sp-gts.x
sp-gts.y sp-gtp ____8_____ sp-gtnext
____9_____
movl 8(ebp), eax put sp in eax movl 8(eax),
edx put sp-gts.y in edx movl edx, 4(eax)
put sp-gts.y in sp-gts.x leal 4(eax), edx put
(sp-gts.y) in edx movl edx, (eax) put
(sp-gts.y) in sp-gtp movl eax, 12(eax) put sp
in sp-gtnext
40
Structure reference quiz
void sp_init (struct prob sp) sp-gts.x
sp-gts.y sp-gtp (sp-gts.x) sp-gtnext
____9_____
movl 8(ebp), eax put sp in eax movl 8(eax),
edx put sp-gts.y in edx movl edx, 4(eax)
put sp-gts.y in sp-gts.x leal 4(eax), edx put
(sp-gts.y) in edx movl edx, (eax) put
(sp-gts.y) in sp-gtp movl eax, 12(eax) put sp
in sp-gtnext
41
Structure reference quiz
void sp_init (struct prob sp) sp-gts.x
sp-gts.y sp-gtp (sp-gts.x) sp-gtnext
sp
movl 8(ebp), eax put sp in eax movl 8(eax),
edx put sp-gts.y in edx movl edx, 4(eax)
put sp-gts.y in sp-gts.x leal 4(eax), edx put
(sp-gts.y) in edx movl edx, (eax) put
(sp-gts.y) in sp-gtp movl eax, 12(eax) put sp
in sp-gtnext
Write a Comment
User Comments (0)
About PowerShow.com