Lecture4-ia32-arrays-structs

# Lecture4-ia32-arrays - Announcements Exercise 2 due Sep 8 Recommend you do this concurrently with project 1 not after CS 3214 Computer Systems

This preview shows pages 1–3. Sign up to view the full content.

1 CS 3214 Computer Systems Godmar Back Lecture 4 Announcements • Exercise 2 due Sep 8 – Recommend you do this concurrently with project 1, not after • Project 1 due Sep 7 • Should have teamed up now Make sure you’re on piazza Make sure you re on piazza CS 3214 Fall 2011 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 2 id ht w [ 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 CS 3214 Fall 2011 Array Allocation • Basic Principle T A[ L ]; – Array of data type T and length L – Contiguously allocated region of L * sizeof( T ) bytes char string[12]; xx + 12 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 double a[4]; x + 32 x + 24 x x + 8 x + 16 char *p[3]; x x + 4 x + 8 CS 3214 Fall 2011 Array Access • Basic Principle T A[ L ]; – Array of data type T and length L – Identifier A can be used as a pointer to array element 0 • Reference Type Value 1 5 2 1 3 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 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 + 4 i CS 3214 Fall 2011 Array Example typedef int zip_dig[5]; zip_dig a = { 1, 5, 2, 1, 3 }; zip_dig b = { 0, 2, 1, 3, 9 }; zip_dig c = { 9, 4, 7, 2, 0 }; zip_dig a; 1 5 2 1 3 16 20 24 28 32 36 Notes Declaration “ zip_dig c ” equivalent to “ int c[5] Example arrays were allocated in successive 20 byte blocks Not guaranteed to happen in general zip_dig b; 0 2 1 3 9 36 40 44 48 52 56 zip_dig c; 9 4 7 2 0 56 60 64 68 72 76 CS 3214 Fall 2011

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Array Accessing Example int get_digit (zip_dig z, int dig) { return z[dig]; } Computation ± Register %edx contains starting address of array ± Register %eax contains array index ± Desired digit at 4*%eax + %edx ± Use memory reference • Memory Reference Code # %edx = z # %eax = dig movl (%edx,%eax,4),%eax # z[dig] Use memory reference (%edx,%eax,4) CS 3214 Fall 2011 Referencing Examples zip_dig a; 1 5 2 1 3 16 20 24 28 32 36 zip_dig b; 0 2 1 3 9 36 40 44 48 52 56 zip_dig c; 9 4 7 2 0 56 60 64 68 72 76 • Code Does Not Do Any Bounds Checking! Reference Address Value Guaranteed? b[3] 36 + 4* 3 = 48 3 b[5] 36 + 4* 5 = 56 9 b[-1] 36 + 4*-1 = 32 3 a[15] 16 + 4*15 = 76 ?? – Out of range behavior implementation-dependent • No guaranteed relative allocation of different arrays Yes No No No CS 3214 Fall 2011 int zd2int(zip_dig z) { int i; int zi = 0; for (i = 0; i < 5; i++) { zi = 10 * zi + z[i]; } return zi; } Array Loop Example • Original Source Transformed Version int zd2int(zip_dig z) { int zi = 0; int *zend = z + 4; do { zi = 10 * zi + *z; z++; } while(z <= zend); return zi; } ± As generated by GCC ± Eliminate loop variable i ± Convert array code to pointer code ± Express in do-while form z No need to test at entrance CS 3214 Fall 2011 Array Loop Implementation • Registers %ecx z %eax zi %ebx zend • Computations 10*zi + *z implemented as *z + 2*(zi+4*zi) z++ increments by 4 int zd2int(zip_dig z) { int zi = 0; int *zend = z + 4; do { zi = 10 * zi + *z; z++; } while(z <= zend); # %ecx = z xorl %eax,%eax # zi = 0 leal 16(%ecx),%ebx # zend = z+4 .L59: leal (%eax,%eax,4),%edx # 5*zi movl (%ecx),%eax # *z addl \$4,%ecx # z++
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/31/2011 for the course CS 3214 taught by Professor Staff during the Fall '11 term at Virginia Tech.

### Page1 / 10

Lecture4-ia32-arrays - Announcements Exercise 2 due Sep 8 Recommend you do this concurrently with project 1 not after CS 3214 Computer Systems

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online