Offset 4 123 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104

Offset 4 123 456 address 0x124 0x120 0x11c 0x118

This preview shows page 53 - 65 out of 148 pages.

Offset -4 123 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x120 0x104
Image of page 53
Understanding Swap movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # 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 123 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x124 0x120 0x104
Image of page 54
Understanding Swap movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # 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 123 456 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x120 0x104
Image of page 55
Understanding Swap movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # 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 123 456 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 56
Understanding Swap movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # 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 456 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 57
Understanding Swap movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # 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 58
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 59
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 60
Rutgers University Santosh Nagarakatte 61 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 61
Rutgers University Santosh Nagarakatte 62 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 62
Rutgers University Santosh Nagarakatte 63 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 63
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
Image of page 64
Image of page 65

You've reached the end of your free preview.

Want to read all 148 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture