cs33-machine_programming_data_structures

cs33-machine_programming_data_structures - Some notes...

Info iconThis preview shows pages 1–10. 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

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: Some notes adopted from Bryant and O’Hallaron Machine-Level Programming: Structured Data Chapter 3 of B&O 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 quad word q 8 [ unsigned ] long int (x86-64) •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 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]; 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 IA32 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 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 Array Example • 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 Array Accessing Example 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 – Register %edx contains starting address of array – Register %eax contains array index – Desired digit at 4*%eax + %edx – Use memory reference (%edx,%eax,4) Referencing Examples Code Does Not Do Any Bounds Checking! Reference Address Guaranteed? Value 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 No No No 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 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 – 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 # %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++ leal (%eax,%edx,2),%eax # zi = *z + 2*(5*zi) cmpl %ebx,%ecx...
View Full Document

This note was uploaded on 05/11/2011 for the course CS 33 taught by Professor Rohr during the Spring '08 term at UCLA.

Page1 / 44

cs33-machine_programming_data_structures - Some notes...

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

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