lecture7

lecture7 - Machine-Level Programming IV: Structured Data...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Machine-Level Programming IV: Structured Data Machine-Level Programming IV: Structured Data Topics Topics & Arrays & Structs 2 Basic Data Types Basic Data Types Integral 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 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 Array Allocation Basic Principle Basic Principle T A[ L ]; Array of data type T and length L Contiguously allocated region of L * sizeof( T ) bytes char string[12]; x x + 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 4 Array Access Array Access Basic Principle 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 Reference Type Type Value 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 + 4 i 1 5 2 1 3 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 5 Array Example Array Example Notes Notes & Declaration zip_dig cmu equivalent to int cmu[5] & Example arrays were allocated in successive 20 byte blocks Not guaranteed to happen in general typedef int zip_dig[5]; zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig mit = { 0, 2, 1, 3, 9 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; zip_dig cmu; 1 5 2 1 3 16 20 24 28 32 36 zip_dig mit; 2 1 3 9 36 40 44 48 52 56 zip_dig ucb; 9 4 7 2 56 60 64 68 72 76 6 Array Accessing Example Array Accessing Example Memory Reference Code Memory Reference Code int get_digit (zip_dig z, int dig) { return z[dig]; } # %edx = z # %eax = dig movl (%edx,%eax,4),%eax # z[dig] Computation Computation & Register %edx contains starting address of array & Register %eax contains array index & Desired digit at 4*%eax + %edx & Use memory reference (%edx,%eax,4) 7 Referencing Examples Referencing Examples Code Does Not Do Any Bounds Checking! Code Does Not Do Any Bounds Checking! Reference Reference Address Address Value Value Guaranteed? Guaranteed? mit[3] 36 + 4* 3 = 48 3 mit[5] 36 + 4* 5 = 56 9 mit[-1] 36 + 4*-1 = 32 3 cmu[15] 16 + 4*15 = 76 ?? & Out of range behavior implementation-dependent No guaranteed relative allocation of different arrays zip_dig cmu; 1 5 2 1 3 16 20 24 28 32 36 zip_dig mit; 2 1 3 9 36 40 44 48 52 56 zip_dig ucb; 9 4 7 2 56 60 64 68 72 76 Yes Yes No No No No No No 8 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 Array Loop Example Original Source Original Source int zd2int(zip_dig z) { int zi = 0; int *zend = z + 4; do { zi = 10 * zi + *z; z++; } while(z <= zend); return zi; } Transformed Version Transformed Version...
View Full Document

This note was uploaded on 04/13/2008 for the course CS 211 taught by Professor Chakraborty during the Spring '08 term at Rutgers.

Page1 / 50

lecture7 - Machine-Level Programming IV: Structured Data...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online