【正文】
gets(buf)。 double v。., treated the same as a 4byte primitive data type ? 12 bytes (long double) ? Windows, Linux: 187。 int i[2]。 int iTn = i*n。 } movl 12(%ebp),%eax i movl 8(%ebp),%edx a imull 20(%ebp),%eax n*i addl 16(%ebp),%eax n*i+j movl (%edx,%eax,4),%eax Mem[a+4*(i*n+j)] Optimizing Dynamic Array Mult. Optimizations ? Performed when set optimization level to O2 Code Motion ? Expression i*n can be puted outside loop Strength Reduction ? Incrementing j has effect of incrementing j*n+k by n Performance ? Compiler can optimize regular access patterns { int j。 /* Compute element i,k of fixed matrix product */ int fix_prod_ele (fix_matrix a, fix_matrix b, int i, int k) { int j。 ? Array of data type T and length L ? Contiguously allocated region of L * sizeof(T) bytes ? Identifier A can be used as a pointer to array element 0 Type T* Viewing as Multidimensional Array Declaration T A[R][C]。 a[i+1] */ void swap_ele (long a[], int i) { swap(amp。a[i], amp。 done: While Version For Version Goto Version Switch Statements Implementation Options ? Series of conditionals ? Organize in tree structure ? Logarithmic performance ? Jump Table ? Lookup branch target ? Constant time ? Possible when cases are small integer constants ? GCC ? Picks one based on case structure Stack Pointer %esp yoo who proc Frame Pointer %ebp Stack ―Top‖ IA32Stack Frames Contents ? Local variables ? Return information ? Temporary space Management ? Space allocated when enter procedure ? “Setup” code ? Deallocated when return ? “Finish” code Pointers ? Stack pointer %esp indicates stack top ? Frame pointer %ebp indicates start of current frame amI IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) ? Parameters for function about to call ? “Argument build” ? Local variables ? If can’t keep in registers ? Saved register context ? Old frame pointer Caller Stack Frame ? Return address ? Pushed by call instruction ? Arguments for this call Stack Pointer (%esp) Frame Pointer (%ebp) Return Addr Saved Registers + Local Variables Argument Build Old %ebp Arguments Caller Frame IA32/Linux Register Usage Integer Registers ? Two have special uses %ebp, %esp ? Three managed as calleesave %ebx, %esi, %edi ? Old values saved on stack prior to using ? Three managed as callersave %eax, %edx, %ecx ? Do what you please, but expect any callee to do so, as well ? Register %eax also stores returned value %eax %edx %ecx %ebx %esi %edi %esp %ebp CallerSave Temporaries CalleeSave Temporaries Special %rax %rbx %rcx %rdx %rsi %rdi %rsp %rbp x8664 Register Conventions %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 Return Value Callee Saved Argument 4 Argument 3 Argument 2 Argument 1 Stack Pointer Callee Saved Argument 5 Argument 6 Callee Saved Used for linking C: Callee Saved Callee Saved Callee Saved Callee Saved x8664 Registers Arguments passed to functions via registers ? If more than 6 integral parameters, then pass rest on stack ? These registers can be used as callersaved as well All References to Stack Frame via Stack Pointer ? Eliminates need to update %ebp Other Registers ? 6+1 callee saved ? 2 or 3 have special uses x8664 Locals in the Red Zone Avoiding Stack Pointer Change ? Can hold all information within small window beyond stack pointer /* Swap, using local array */ void swap_a(long *xp, long *yp) { volatile long loc[2]。 } while (Test) done: “For”? “While” (JumptoMiddle) for (Init。 while (Test ) { Body Update 。 } return result。 int t1 = *yp。 Assume neither d nor f is NaN 匯編與 C語言 movl Operand Combinations Cannot do memorymemory transfer with a single instruction movl Imm Reg Mem Reg Mem Reg Mem Reg Source Dest C Analog movl $0x4,%eax temp = 0x4。 Initialization Floating Point Representation ? Bits to right of ―binary point‖ represent fractional powers of 2 ? Represents rational number: bi bi–1 b2 b1 b0 b–1 b–2 b–3 b–j ? ? ? ? ? ? . 1 2 4 2i–1 2i ? ? ? ? ? ? 1/2 1/4 1/8 2–j bk ?2kk??ji?Numerical Form ? –1s M 2E ?Sign bit s determines whether number is negative or positive ?Significand M normally a fractional value in range [,). ?Exponent E weights value by power of t