To int uw5 example arrays were allocated in

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: = { 9, 4, 7, 2, 0 }; 1 zip_dig cmu; 16 20 9 zip_dig uw ; 36 56   2 24 8 40 9 zip_dig ucb;   5 28 1 44 4 60 1 32 9 48 7 64 3 5 52 2 68 36 56 0 72 76 DeclaraMon “zip_dig uw” equivalent to “int uw[5]” Example arrays were allocated in successive 20 byte blocks   Not guaranteed to happen in general Arrays University of Washington Array Accessing Example 9 zip_dig uw; 36 8 40 1 44 int get_digit (zip_dig z, int dig) { return z[dig]; } 48 5 52 # z[dig] Arrays 56 Register %edx contains starMng address of array   Register %eax contains array index   Desired digit at 4*%eax + %edx   Use memory reference (%edx,%eax,4)   IA32 # %edx = z # %eax = dig movl (%edx,%eax,4),%eax 9 University of Washington Referencing Examples 1 zip_dig cmu; 16 zip_dig 20 9 uw; 36 Reference uw[3] uw[6] uw[-1] cmu[15] 24 40 56 2 8 9 zip_dig ucb;   5 1 4 64 + + + + 4* 3 4* 6 4*-1 4*15 32 48 48 60 32 76 5 2 68 9 4 3 ?? 36 52 Value = = = = 3 9 7 Address 36 36 36 16 28 44 60 1 56 0 72 76 Guaranteed? Yes No No No   No bounds checking   LocaNon of each separate array in memory is not guaranteed Arrays University of Washington Array Loop Example int zd2int(zip_dig z) { int i; int zi = 0; for (i = 0; i < 5; i++) { zi = 10 * zi + z[i]; } return zi; } Arrays University of Washington Array Loop Example   Original   Transformed   Eliminate loop variable i, use pointer zend instead   Convert array code to pointer code   Pointer arithmeNc on z   Express in do ­while form (no test at entrance) int zd2int(zip_dig z) { int i; int zi = 0; for (i = 0; i < 5; i++) { zi = 10 * zi + z[i]; } return zi; } int zd2int(zip_dig z) { int zi = 0; int *zend = z + 4; do { zi = 10 * zi + *z; z++; } while (z <= zend); return zi; } Arrays University of Washington Array Loop ImplementaMon (IA32)   Registers %ecx z %eax zi %ebx zend ...
View Full Document

This document was uploaded on 04/04/2014.

Ask a homework question - tutors are online