【正文】
e???(infinity) ? Operation that overflows ? Both positive and negative ? exp = 111…1, frac ? 000…0 ? NotaNumber (NaN) s exp frac E Value 0 0000 000 6 0 0 0000 001 6 1/8*1/64 = 1/512 0 0000 010 6 2/8*1/64 = 2/512 … 0 0000 110 6 6/8*1/64 = 6/512 0 0000 111 6 7/8*1/64 = 7/512 0 0001 000 6 8/8*1/64 = 8/512 0 0001 001 6 9/8*1/64 = 9/512 … 0 0110 110 1 14/8*1/2 = 14/16 0 0110 111 1 15/8*1/2 = 15/16 0 0111 000 0 8/8*1 = 1 0 0111 001 0 9/8*1 = 9/8 0 0111 010 0 10/8*1 = 10/8 … 0 1110 110 7 14/8*128 = 224 0 1110 111 7 15/8*128 = 240 0 1111 000 n/a inf closest to zero largest denorm smallest norm closest to 1 below closest to 1 above largest norm Denormalized numbers Normalized numbers RoundToEven Binary Fractional Numbers ? ―Even‖ when least significant bit is 0 ? Half way when bits to right of rounding position = 100…2 Examples ? Round to nearest 1/4 (2 bits right of binary point) Value Binary Rounded Action Rounded Value 2 3/32 (1/2—down) 2 2 3/16 (1/2—up) 2 1/4 2 7/8 (1/2—up) 3 2 5/8 (1/2—down) 2 1/2 Floating Point in C C Guarantees Two Levels float single precision double double precision Conversions ? Casting between int, float, and double changes numeric values ? Double or float to int ? Truncates fractional part ? Like rounding toward zero ? Not defined when out of range or NaN 187。 Generally sets to Tmin or Tmax ? int to double ? Exact conversion, as long as int has ≤ 53 bit word size ? int to float ? Will round according to rounding mode Floating Point Puzzles ? For each of the following C expressions, either: ?Argue that it is true for all argument values ?Explain why not true ? x == (int)(float) x ? x == (int)(double) x ? f == (float)(double) f ? d == (float) d ? f == (f)。 ? 2/3 == 2/ ? d ??? ((d*2) ) ? d f ??? f d ? d * d = ? (d+f)d == f int x = …。 float f = …。 double d = …。 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。 movl $147,(%eax) *p = 147。 movl %eax,%edx temp2 = temp1。 movl %eax,(%edx) *p = temp。 movl (%eax),%edx temp = *p。 Src,Dest Indexed Addressing Modes Most General Form D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] ? D: Constant “displacement” ? Rb: Base register: Any of 8 integer registers ? Ri: Index register: Any, except for %esp ?Unlikely you’d use %ebp, either ? S: Scale: 1, 2, 4, or 8 Special Cases (Rb,Ri) Mem[Reg[Rb]+Reg[Ri]] D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D] (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]] Address Computation Instruction leal Src,Dest ?Src is address mode expression ?Set Dest to address denoted by expression Uses ?Computing addresses without a memory reference ? ., translation of p = amp。x[i]。 ?Computing arithmetic expressions of the form x + k*y ? k = 1, 2, 4, or 8. %rax %rdx %rcx %rbx %rsi %rdi %rsp %rbp x8664 General Purpose Registers ? Extend existing registers. Add 8 new ones. ? Make %ebp/%rbp general purpose %eax %edx %ecx %ebx %esi %edi %esp %ebp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %r8d %r9d %r10d %r11d %r12d %r13d %r14d %r15d Swap in 64bit Mode ? Operands passed in registers ? First (xp) in %rdi, second (yp) in %rsi ? 64bit pointers ? No stack operations required ? 32bit data ? Data held in registers %eax and %edx ? movl operation void swap(int *xp, int *yp) { int t0 = *xp。 int t1 = *yp。 *xp = t1。 *yp = t0。 } swap: movl (%rdi), %edx movl (%rsi), %eax movl %eax, (%rdi) movl %edx, (%rsi) retq Reading Condition Codes SetX Condit ion Descri pti onsete ZF Equa l / Ze rosetne ~ZF N ot E qua l / N o t Zerosets SF N eg a t iv esetns ~SF N onnegat iv esetg ~(SF^OF)amp。~ZF Gr eate r (Signed)setge ~(SF^OF) Gr eate r or Equal (Signed )setl (SF^OF) Le ss (Signed )setle (SF^OF)|ZF Le ss or Equal (Signe d)seta ~CFamp。~ZF A bov e ( uns igned )setb CF B elo w (unsigned)SetX Instructions ? Set single byte based on binations of condition codes Conditional Branch Example int absdiff( int x, int y) { int result。 if (x y) { result = xy。 } else { result = yx。 } return result。 } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7