Carnegie MellonIntroduction to Computer Systems15-213/18-243, spring 20096thLecture, Jan. 29thInstructors:Gregory Kesden and Markus PüschelCarnegie MellonLast Time: Machine Programming, Basics History of Intel processors and architectures C, assembly, machine code Assembly (IA32): Registers Operands Move (what’s the l in movl?)movl $0x4,%eaxmovl %eax,%edxmovl (%eax),%edx%eax%ecx%edx%ebx%esi%edi%esp%ebpCarnegie MellonToday Complete addressing mode, address computation (leal) Arithmetic operations x86-64 Control: Condition codes Conditional branches While loopsCarnegie MellonComplete Memory Addressing Modes Most General FormD(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]]Carnegie MellonAddress Computation Examples%edx%ecx0xf0000x100Expression Address Computation Address0x8(%edx) 0xf000 + 0x8 0xf008(%edx,%ecx) 0xf000 + 0x100 0xf100(%edx,%ecx,4) 0xf000 + 4*0x100 0xf4000x80(,%edx,2) 2*0xf000 + 0x80 0x1e080will disappearblackboard?Carnegie MellonAddress Computation Examples%edx%ecx0xf0000x100Expression Address Computation Address0x8(%edx) 0xf000 + 0x8 0xf008(%edx,%ecx) 0xf000 + 0x100 0xf100(%edx,%ecx,4) 0xf000 + 4*0x100 0xf4000x80(,%edx,2) 2*0xf000 + 0x80 0x1e080Carnegie MellonAddress 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 ExampleCarnegie MellonToday Complete addressing mode, address computation (leal) Arithmetic operations x86-64 Control: Condition codes Conditional branches While loopsCarnegie MellonSome 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 No distinction between signed and unsigned int (why?)Carnegie MellonSome Arithmetic Operations One Operand Instructionsincl Dest Dest = Dest + 1decl Dest Dest = Dest - 1negl Dest Dest = -Destnotl Dest Dest = ~Dest See book for more instructionsCarnegie MellonUsing leal for Arithmetic Expressionsint 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),%eaxmovl 12(%ebp),%edxleal (%edx,%eax),%ecxleal (%edx,%edx,2),%edxsall $4,%edxaddl 16(%ebp),%ecxleal 4(%edx,%eax),%eaximull %ecx,%eaxmovl %ebp,%esppopl %ebpretBodySetUpFinishCarnegie MellonUnderstanding arithint 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),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)yxRtn adrOld %ebp%ebp0 4 8 12 OffsetStack•••z16 will disappearblackboard?Carnegie MellonUnderstanding arithint 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),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)yxRtn adrOld %ebp%ebp0 4 8 12 OffsetStack•••z16Carnegie MellonUnderstanding arithint 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),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)yxRtn adrOld %ebp%ebp0 4 8 12 OffsetStack•••z16Carnegie MellonUnderstanding arithint 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),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)yxRtn adrOld %ebp%ebp0 4 8 12 OffsetStack•••z16Carnegie MellonUnderstanding arithint 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),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)yxRtn adrOld %ebp%ebp0 4 8 12 OffsetStack•••z16Carnegie 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;}logical:pushl %ebpmovl %esp,%ebpmovl 8(%ebp),%eaxxorl 12(%ebp),%eaxsarl $17,%eaxandl $8185,%eaxmovl %ebp,%esppopl %ebpretBodySetUpFinishmovl 8(%ebp),%eax # eax = xxorl 12(%ebp),%eax # eax = x^ysarl $17,%eax # eax = t1>>17andl $8185,%eax # eax = t2 & 8185Carnegie 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;}logical:pushl %ebpmovl %esp,%ebpmovl 8(%ebp),%eaxxorl 12(%ebp),%eaxsarl $17,%eaxandl $8185,%eaxmovl %ebp,%esppopl %ebpretBodySetUpFinishmovl 8(%ebp),%eax eax = xxorl 12(%ebp),%eax eax = x^y (t1)sarl $17,%eax eax = t1>>17 (t2)andl $8185,%eax eax = t2 & 8185Carnegie MellonAnother Exampleint logical(int x, int y){int t1 = x^y;int t2 = t1 >>
View Full Document