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.)Reading Condition Codes: x86-64TodayJumpingConditional 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 MoveToday“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 ExampleSummary1Carnegie MellonMachine-Level Programming II: Arithmetic & Control15-213: Introduction to Computer Systems5th Lecture, Sep. 7, 2010Carnegie MellonInstructors: Randy Bryant and Dave O’Hallaron2Carnegie MellonTodayComplete addressing mode, address computation (leal)Arithmetic operationsControl: Condition codesConditional branchesWhile loops3Carnegie MellonComplete Memory Addressing ModesMost General FormD(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D]D: Constant “displacement” 1, 2, or 4 bytesRb: Base register: Any of 8 integer registersRi: Index register: Any, except for %espUnlikely you’d use %ebp, eitherS: 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]]4Carnegie MellonAddress 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)5Carnegie MellonAddress Computation Instructionleal Src,DestSrc is address mode expressionSet Dest to address denoted by expressionUsesComputing addresses without a memory referenceE.g., translation of p = &x[i];Computing arithmetic expressions of the form x + k*yk = 1, 2, 4, or 8Exampleint 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:6Carnegie MellonTodayComplete addressing mode, address computation (leal)Arithmetic operationsControl: Condition codesConditional branchesWhile loops7Carnegie MellonSome Arithmetic OperationsTwo Operand Instructions:FormatComputationaddl 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 | SrcWatch out for argument order!No distinction between signed and unsigned int (why?)8Carnegie MellonSome Arithmetic OperationsOne Operand Instructionsincl Dest Dest = Dest + 1decl Dest Dest = Dest 1negl Dest Dest = Destnotl Dest Dest = ~DestSee book for more instructions9Carnegie MellonArithmetic 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 %ebpretBodySetUpFinish10•••16 z12 y8 x4 Rtn Addr0 Old %ebpCarnegie MellonUnderstanding 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;}11•••16 z12 y8 x4 Rtn Addr0 Old %ebpCarnegie MellonUnderstanding 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)12Carnegie MellonObservations about arithInstructions in different order from C codeSome expressions require multiple instructionsSome instructions cover multiple expressionsGet 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 rval;}13Carnegie MellonAnother Exampleint logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval =
View Full Document