Lecture9 - Assembly Programming Abhishek Bhattacharjee Topics Introduction to Assembly Programming x86 assembly Using leal for Arithmetic Expressions

Lecture9 - Assembly Programming Abhishek Bhattacharjee...

This preview shows page 1 - 9 out of 26 pages.

Abhishek Bhattacharjee Topics square6 Introduction to Assembly Programming circle6 x86 assembly Assembly Programming
Rutgers University 2 Abhishek Bhattacharjee Using leal for Arithmetic Expressions 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 %ebp movl %esp,%ebp movl 8(%ebp),%eax movl 12(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall \$4,%edx addl 16(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax movl %ebp,%esp popl %ebp ret Body Set Up Finish
Rutgers University 3 Abhishek Bhattacharjee Understanding arith 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),%eax # eax = x movl 12(%ebp),%edx # edx = y leal (%edx,%eax),%ecx # ecx = x+y (t1) leal (%edx,%edx,2),%edx # edx = 3*y sall \$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) y x Rtn adr Old % ebp %ebp 0 4 8 12 Offset Stack z 16
Rutgers University 4 Abhishek Bhattacharjee Understanding arith 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; } # eax = x movl 8(%ebp),%eax # edx = y movl 12(%ebp),%edx # ecx = x+y (t1) leal (%edx,%eax),%ecx # edx = 3*y leal (%edx,%edx,2),%edx # edx = 48*y (t4) sall \$4,%edx # ecx = z+t1 (t2) addl 16(%ebp),%ecx # eax = 4+t4+x (t5) leal 4(%edx,%eax),%eax # eax = t5*t2 (rval) imull %ecx,%eax
Rutgers University 5 Abhishek Bhattacharjee Another Example int 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 %ebp movl %esp,%ebp movl 8(%ebp),%eax xorl 12(%ebp),%eax sarl \$17,%eax andl \$8185,%eax movl %ebp,%esp popl %ebp ret Body Set Up Finish movl 8(%ebp),%eax eax = x xorl 12(%ebp),%eax eax = x^y (t1) sarl \$17,%eax eax = t1>>17 (t2) andl \$8185,%eax eax = t2 & 8185 2 13 = 8192, 2 13 – 7 = 8185
Rutgers University Abhishek Bhattacharjee 6 Condition Codes Single Bit Registers CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag Can be set either implicitly or explicitly . square6 Implicitly by almost all logic and arithmetic operations square6 Explicitly by specific comparison operations Not Set by leal instruction square6 Intended for use in address computation only
Rutgers University Abhishek Bhattacharjee 7 Condition Codes Implicitly Set By Arithmetic Operations addl Src , Dest C analog: t = a + b square6 CF set if carry out from most significant bit circle6 Used to detect unsigned overflow square6 ZF set if t == 0 square6 SF set if t < 0 square6 OF set if two’s complement overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)
Rutgers University Abhishek Bhattacharjee 8 Setting Condition Codes (cont.)

You've reached the end of your free preview.

Want to read all 26 pages?

• Fall '08
• Staff
• X86, Control flow, Switch statement, Branch table, Abhishek Bhattacharjee