Slide 1TodayComplete Memory Addressing ModesAddress Computation ExamplesAddress Computation InstructionTodaySome Arithmetic OperationsSome Arithmetic OperationsArithmetic Expression ExampleUnderstanding arithUnderstanding arithObservations about arithAnother ExampleAnother ExampleAnother ExampleAnother ExampleTodayProcessor State (IA32, Partial)Condition Codes (Implicit Setting)Condition Codes (Explicit Setting: Compare)Condition Codes (Explicit Setting: Test)Reading Condition CodesReading Condition Codes (Cont.)TodayJumpingConditional Branch ExampleConditional Branch Example (Cont.)Conditional Branch Example (Cont.)Conditional Branch Example (Cont.)Conditional Branch Example (Cont.)General Conditional Expression TranslationUsing Conditional MovesConditional Move Example: x86-64Bad Cases for Conditional MoveControl transfer and basic blocksControl transfer and basic blocksControl transfer and basic blocksControl transfer and basic blocksControl transfer and basic blocksControl transfer and basic blocksControl transfer and basic blocksControl transfer and basic blocksToday“Do-While” Loop Example“Do-While” Loop CompilationGeneral “Do-While” Translation“While” Loop ExampleGeneral “While” Translation“For” Loop Example“For” Loop Form“For” Loop While Loop“For” Loop … Goto“For” Loop Conversion ExampleSummaryMACHINE-LEVEL PROGRAMMING II: ARITHMETIC & CONTROL2University of Texas at AustinToday•Complete addressing mode, address computation (leal)•Arithmetic operations•Control: Condition codes•Conditional branches•While loops3University of Texas at AustinComplete Memory Addressing Modes•Most General Form•D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D]•D: Constant “displacement” 1, 2, or 4 bytes•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 (why these numbers?)•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]]4University of Texas at AustinAddress Computation ExamplesExpressionAddress ComputationAddress0x8(%edx) 0xf000 + 0x8 0xf008(%edx,%ecx) 0xf000 + 0x100 0xf100(%edx,%ecx,4) 0xf000 + 4*0x100 0xf4000x80(,%edx,2) 2*0xf000 + 0x80 0x1e080%edx 0xf000%ecx 0x0100ExpressionAddress ComputationAddress0x8(%edx)(%edx,%ecx)(%edx,%ecx,4)0x80(,%edx,2)5University of Texas at AustinAddress Computation Instruction•leal Src,Dest•Src is address mode expression•Set Dest to address denoted by expression•Uses•Computing addresses without a memory reference•E.g., translation of p = &x[i];•Computing arithmetic expressions of the form x + k*y•k = 1, 2, 4, or 8•Exampleint mul12(int x){ return x*12;}int mul12(int x){ return x*12;}leal (%eax,%eax,2), %eax ;t <- x+x*2sall $2, %eax ;return t<<2leal (%eax,%eax,2), %eax ;t <- x+x*2sall $2, %eax ;return t<<2Converted to ASM by compiler:6University of Texas at AustinToday•Complete addressing mode, address computation (leal)•Arithmetic operations•Control: Condition codes•Conditional branches•While loops7University of Texas at AustinSome Arithmetic Operations•Two Operand Instructions: Format Computationaddl Src,Dest Dest = Dest + Srcsubl Src,Dest Dest = Dest Srcimull Src,Dest Dest = Dest * Srcsall Src,Dest Dest = Dest << Src Also called shllsarl Src,Dest Dest = Dest >> Src Arithmeticshrl Src,Dest Dest = Dest >> Src Logicalxorl Src,Dest Dest = Dest ^ Srcandl Src,Dest Dest = Dest & Srcorl Src,Dest Dest = Dest | Src•Watch out for argument order!•No distinction between signed and unsigned int (why?)8University of Texas at AustinSome Arithmetic Operations•One Operand Instructionsincl Dest Dest = Dest + 1decl Dest Dest = Dest 1negl Dest Dest = Destnotl Dest Dest = ~Dest•See book for more instructions9University of Texas at AustinArithmetic Expression Exampleint arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}arith:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %ecxmovl 12(%ebp), %edxleal (%edx,%edx,2), %eaxsall $4, %eaxleal 4(%ecx,%eax), %eaxaddl %ecx, %edxaddl 16(%ebp), %edximull %edx, %eaxpopl %ebpretBodySetUpFinish10University of Texas at Austin•••16 z12 y8 x4 Rtn Addr0 Old %ebpUnderstanding arithmovl 8(%ebp), %ecxmovl 12(%ebp), %edxleal (%edx,%edx,2), %eaxsall $4, %eaxleal 4(%ecx,%eax), %eaxaddl %ecx, %edxaddl 16(%ebp), %edximull %edx, %eax%ebpOffsetint arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}11University of Texas at Austin•••16 z12 y8 x4 Rtn Addr0 Old %ebpUnderstanding arith%ebpOffsetStackint arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}movl 8(%ebp), %ecx # ecx = xmovl 12(%ebp), %edx # edx = yleal (%edx,%edx,2), %eax # eax = y*3sall $4, %eax # eax *= 16 (t4)leal 4(%ecx,%eax), %eax # eax = t4 +x+4 (t5)addl %ecx, %edx # edx = x+y (t1)addl 16(%ebp), %edx # edx += z (t2)imull %edx, %eax # eax = t2 * t5 (rval)12University of Texas at AustinObservations about arith•Instructions in different order from C code•Some expressions require multiple instructions•Some instructions cover multiple expressions•Get exact same code when compile:•(x+y+z)*(x+4+48*y)movl 8(%ebp), %ecx # ecx = xmovl 12(%ebp), %edx # edx = yleal (%edx,%edx,2), %eax # eax = y*3sall $4, %eax # eax *= 16 (t4)leal 4(%ecx,%eax), %eax # eax = t4 +x+4 (t5)addl %ecx, %edx # edx = x+y (t1)addl 16(%ebp), %edx # edx += z (t2)imull %edx, %eax # eax = t2 * t5 (rval)int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return
View Full Document