Add content of two registers to get address of operand movl eab esi eax Copy

This preview shows page 33 - 44 out of 75 pages.

Add content of two registers to get address of operand movl (%eab, %esi), %eax Copy value at (address = eab + esi) into eax  movl 8(%eab, %esi),%eax Copy value at (address = 8 + eab + esi) into eax Useful for dealing with arrays If you need to walk through the elements of an array Use one register to hold base address, one to hold index E.g., implement C array access in a for loop 33 Rutgers University 34 34 Address Computation Examples %edx %ecx 0xf000 0x100 Expression Computation Address 0x8(%edx) 0xf000 + 0x8 0xf008 (%edx,%ecx) 0xf000 + 0x100 0xf100 (%edx,%ecx,4) 0xf000 + 4*0x100 0xf400 0x80(,%edx,2) 2*0xf000 + 0x80 0x1e080 Rutgers University 35 movl  Operand Combinations Cannot do memory-memory transfers with single  instruction movl Imm Reg Mem Reg Mem Reg Mem Reg Source Destination movl \$0x4,%eax movl \$-147,(%eax) movl %eax,%edx movl %eax,(%edx) movl (%eax),%edx C Analog temp = 0x4; *p = -147; temp2 = temp1; *p = temp; temp = *p; Rutgers University 36 Stack Operations By convention, %esp is used to maintain a stack in memory Used to support C function calls %esp contains the address of top of stack Instructions to push (pop) content onto (off of) the stack pushl %eax esp = esp – 4 Memory[esp] = eax popl %ebx ebx = Memory[esp] esp = esp + 4 Where does the stack start? Rutgers University 37 Mystery Function (Q): 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) Rutgers University 38 Using Simple Addressing Modes void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Body Set Up Finish Rutgers University 39 Understanding Swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } 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 Stack Register Variable %ecx yp %edx xp %eax t1 %ebx t0 yp xp Rtn adr Old % ebp %ebp 0 4 8 12 Offset Old % ebx -4 Rutgers University 40 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 0x104 Rutgers University 41 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 0x120 0x104 Rutgers University 42 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 Rutgers University  #### You've reached the end of your free preview.

Want to read all 75 pages?

• Spring '08
• Chakraborty
• 1920, 1922, 1916, 1918, 1925, Rutgers University
• • • 