edx xp movl ecxeax eax yp t1 movl edxebx ebx xp t0 movl eaxedx xp eax movl

Edx xp movl ecxeax eax yp t1 movl edxebx ebx xp t0

This preview shows page 52 - 63 out of 126 pages.

# edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx 0x120 0x124 Rtn adr %ebp 0 4 8 12 Offset -4 456 123 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x120 123 0x104
Image of page 52
Swap in x86-64: 64-bit Registers rax eax rcx ecx rdx edx rbx ebx rsp esp rbp ebp rsi esi rdi edi r8 r9 r10 r11 r12 r13 r14 r15
Image of page 53
Swap in x86-64 bit void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: movl (%rdi), %edx movl (%rsi), %eax movl %eax, (%rdi) movl %edx, (%rsi) retq Arguments passed in registers n First, xp in rdi and yp in rsi n 64-bit pointers, data values are 32-bit ints, so uses eax/edx No stack operations What happens with long int?
Image of page 54
Rutgers University Santosh Nagarakatte 55 Address Computation Instruction leal: compute address using addressing mode without accessing memory leal src, dest n src is address mode expression n Set dest to address specified by src Use n Computing address without doing memory reference l E.g., translation of p = &x[i]; Example: n leal 7(%edx, %edx, 4), %eax l eax = 4*edx + edx + 7 = 5*edx + 7
Image of page 55
Rutgers University Santosh Nagarakatte 56 Some Arithmetic Operations Instruction Computation addl Src , Dest Dest = Dest + Src subl Src , Dest Dest = Dest - Src imull Src , Dest Dest = Dest * Src sall Src , Dest Dest = Dest << Src (left shift) sarl Src , Dest Dest = Dest >> Src (right shift) xorl Src , Dest Dest = Dest ^ Src andl Src , Dest Dest = Dest & Src orl Src , Dest Dest = Dest | Src
Image of page 56
Rutgers University Santosh Nagarakatte 57 Some Arithmetic Operations Instruction Computation incl Dest Dest = Dest + 1 decl Dest Dest = Dest - 1 negl Dest Dest = - Dest notl Dest Dest = ~ Dest
Image of page 57
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
Image of page 58
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
Image of page 59
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 60
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 61
Control Flow/Conditionals How do we represent conditionals in assembly?
Image of page 62
Image of page 63

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture