Machine-Level Programming 5 Structured Data - PowerPoint PPT Presentation

About This Presentation
Title:

Machine-Level Programming 5 Structured Data

Description:

Machine-Level Programming 5 Structured Data Topics Arrays – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 30
Provided by: RandalEBry9
Category:

less

Transcript and Presenter's Notes

Title: Machine-Level Programming 5 Structured Data


1
Machine-Level Programming 5Structured Data
  • Topics
  • Arrays

2
Basic Data Types
  • Integral
  • Stored operated on in general registers
  • Signed vs. unsigned depends on instructions used
  • 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/16 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 string12
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
  • Type T
  • Reference Type Value
  • val4
  • val
  • val1
  • val2
  • val5
  • (val1)
  • val i

int 3
int x
int x 4
int x 8
int ????
int 5
int x 4i
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 umbc 2, 1, 2, 5, 0
  • Notes
  • Declaration zip_dig umbc equivalent to int
    umbc5
  • 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
  • IA32 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
  • mit5
  • mit-1
  • cmu15

Yes
3
36 43 48
No
36 45 56
2
No
36 4(-1) 32
3
No
16 415 76
??
Out of range behavior implementation-dependent No
guaranteed relative allocation of different
arrays
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 (IA32)
  • 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 movl
(ecx),eax z leal (eax,eax,4),edx
5zi leal (eax,edx,2),eax zi z
2(5zi) addl 4,ecx z cmpl ebx,ecx z
zend jle .L59 if lt goto loop
10
Array Code Example
  • int main ( )
  • int i, sum, array4
  • for (i 0 i lt 4 i )
  • arrayi i ltlt 2
  • sum sumArray( array )
  • printf( "Array sum d\n", sum)
  • return 0
  • int sumArray( int a )
  • return a1 a3

11
sumArray
  • int sumArray( int a )
  • return a 1 a 3

08048390 ltsumArraygt 8048390 55
push ebp setup 8048391 89 e5
mov esp,ebp setup 8048393
8b 55 08 mov 0x8(ebp),edx edx
a 8048396 5d pop ebp
finish up 8048397 8b 42 0c
mov 0xc(edx),eax a 3 804839a
8b 4a 04 mov 0x4(edx),ecx a 1
804839d 01 c8 add
ecx,eax return sum 804839f c3
ret
12
main
  • int main ( )
  • int i, sum, aa4
  • for (i 0 i lt 4 i )
  • aai i ltlt 2 // aai i 4
  • sum sumArray( aa )
  • printf( "Array sum d\n", sum)
  • return 0

13
main
  • 080483a0 ltmaingt
  • 80483a0 31 d2 xor
    edx,edx i 0
  • missing setup code
  • 80483b0 83 ec 24 sub
    0x24,esp big stack
  • 80483b3 8d 4d ec lea
    0xffffffec(ebp),ecx aa at ebp - 0x14
  • 80483b6 8d 76 00 lea
    0x0(esi),esi filler
  • 80483b9 8d bc 27 00 00 00 00 lea
    0x0(edi),edi filler
  • L1 word boundary
  • 80483c0 8d 04 95 00 00 00 00 lea
    0x0(,edx,4),eax i 4
  • 80483c7 89 04 91 mov
    eax,(ecx,edx,4) a i
  • 80483ca 42 inc edx
    i
  • 80483cb 83 fa 04 cmp
    0x4,edx compare i 4
  • 80483ce 75 f0 jne 80483c0
    ltmain0x20gt loop L1 ??
  • 80483d0 89 0c 24 mov
    ecx,(esp) push param
  • 80483d3 e8 b8 ff ff ff call 8048390
    ltsumArraygt
  • 80483d8 c7 04 24 d8 84 04 08 movl
    0x80484d8,(esp) for printf
  • 80483df 89 44 24 04 mov
    eax,0x4(esp) for printf
  • 80483e3 e8 d4 fe ff ff call 80482bc
    ltprintf_at_pltgt
  • 80483e8 83 c4 24 add
    0x24,esp resest esp

14
Nested Array Example
define PCOUNT 4 zip_dig mdPCOUNT 2, 1,
1, 6, 2, 2, 1, 2, 2, 8 , 2, 1, 0, 4, 2
, 2, 1, 2, 1, 0
  • Declaration zip_dig md4 equivalent to int
    md45
  • Variable md denotes array of 4 elements
  • Allocated contiguously
  • Each element is an array of 5 ints
  • Allocated contiguously
  • Row-Major ordering of all elements guaranteed

15
Viewing as Multidimensional Array
  • Declaration
  • T ARC
  • 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

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

int ARC
  
  
A
AiC4
A(R-1)C4
17
Nested Array Row Access Code
int get_md_zip(int index) return mdindex
  • Row Vector
  • md is a 2-d array of ints, 5 ints per row
  • mdindex is array of 5 ints
  • Starting address of mdindex is md 20index
  • IA32 Code
  • Computes and returns address
  • Compute as md 4(index 4index)

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

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

int get_md_digit (int index, int dig)
return mdindexdig
ecx dig eax index leal
0(,ecx,4),edx 4dig leal (eax,eax,4),eax
5index movl md(edx,eax,4),eax (md
4dig 20index)
20
Strange Referencing Examples
  • Reference Address Value Guaranteed?
  • md33 7620343 148 1
  • md25 7620245 136 2
  • md2-1 762024-1 112 8
  • md4-1 762044-1 152 0
  • md019 76200419 152 0
  • md0-1 762004-1 72 ??
  • Code does not do any bounds checking
  • Ordering of elements within array guaranteed

Yes
Yes
Yes
Yes
Yes
No
21
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 umbc 2, 1, 2, 5, 0

define UCOUNT 3 int univUCOUNT mit, cmu,
umbc
22
Element Access in Multi-Level Array
int get_univ_digit (int index, int dig)
return univindexdig
  • Computation (IA32)
  • Element access MemMemuniv 4index 4dig
  • 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
Memuniv 4index movl (edx,eax,4),eax
Mem... 4dig
23
Array Element Accesses
  • Similar C references
  • Nested Array
  • Element at
  • Memmd20index4dig
  • Different address computation
  • Multi-Level Array
  • Element at
  • MemMemuniv4index4dig

int get_md_digit (int index, int dig)
return mdindexdig
int get_univ_digit (int index, int dig)
return univindexdig
cmu
cmu
1
5
2
1
3
1
5
2
1
3
univ
univ
16
20
24
28
32
36
16
20
24
28
32
36
160
160
mit
36
36
mit
0
2
1
3
9
0
2
1
3
9
164
164
16
16
36
40
44
48
52
56
36
40
44
48
52
56
ucb
umbc
56
168
56
168
56
60
64
68
72
76
24
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
25
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
26
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 ain j
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)
27
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
28
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
29
Summary
  • Arrays in C
  • Contiguous allocation of memory
  • Pointer to first element
  • No bounds checking
  • 2-D Arrays in C
  • nested -- int grades 4 6
  • Nr rows, columns fixed
  • Dynamic -- int grades 6
  • Nr rows dynamic, nr columns fixed
  • Double subscripts work in both cases
  • Compiler generates different code
Write a Comment
User Comments (0)
About PowerShow.com