int t3 x4 int t4 y 48 int t5 t3 t4 int rval t2 t5 return rval movl 8ebpeax eax

Int t3 x4 int t4 y 48 int t5 t3 t4 int rval t2 t5

This preview shows page 65 - 76 out of 160 pages.

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
Image of page 65
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
Image of page 66
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
Image of page 67
Rutgers University David Menendez 68 Mystery Function What does the following piece of code do? A. Add two variables B. Subtract two variables C. Swap two variables D. No idea movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx)
Image of page 68
iClicker Quiz 1 .globl foo .type foo, @function foo: pushl %ebp movl %esp, %ebp movl 16(%ebp), %eax imull 12(%ebp), %eax addl 8(%ebp), %eax popl %ebp ret A: A function that takes two arguments B: A function that takes three arguments C: A function that takes four arguments D: A function that takes no arguments
Image of page 69
What does this function do? .globl foo .type foo, @function foo: pushl %ebp movl %esp, %ebp movl 16(%ebp), %eax imull 12(%ebp), %eax addl 8(%ebp), %eax popl %ebp ret
Image of page 70
Control Flow/Conditionals How do we represent conditionals in assembly? A conditional branch can implement all control flow constructs in higher level language Examples: if/then, while, for A unconditional branch for constructs like break/ continue
Image of page 71
Rutgers University David Menendez 72 Condition Codes Single Bit Registers CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag Can be set either implicitly or explicitly . Implicitly by almost all logic and arithmetic operations Explicitly by specific comparison operations Not Set by leal instruction Intended for use in address computation only
Image of page 72
Rutgers University David Menendez 73 Jumping jX Instructions Jump to different part of code depending on condition codes jX Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned)
Image of page 73
Rutgers University David Menendez 74 Condition Codes Implicitly Set By Arithmetic Operations addl Src , Dest C analog: t = a + b CF set if carry out from most significant bit Used to detect unsigned overflow ZF set if t == 0 SF set if t < 0 OF set if two’s complement overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)
Image of page 74
Rutgers University David Menendez 75 Setting Condition Codes (cont.)
Image of page 75
Image of page 76

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture