University of Washington Machine Programming II: C to assembly � Move instructions, registers, and operands � Complete addressing mode, address computation ( leal) � Arithmetic operations (including some x86 ‐ 64 instructions) � Condition codes � Control, unconditional and conditional branches � While loops 09 April 2012 Machine Programming 1 University of Washington Three Kinds of Instructions � Perform arithmetic function on register or memory data � c = a + b; � Transfer data between memory and register � Load data from memory into register � %reg = Mem[address] � Store register data into memory � Mem[address] = %reg � Transfer control (control flow) � Unconditional jumps to/from procedures � Conditional branches 09 April 2012 Machine Programming 2
University of Washington Moving Data: IA32 %eax %ecx � Moving Data %edx � movx Source , Dest %ebx � x is one of { b, w, l } i f { b l } %esi � movl Source , Dest : %edi Move 4 ‐ byte “long word” %esp � movw Source , Dest : %ebp Move 2 ‐ byte “word” � movb Source Dest : movb Source , Dest : Move 1 ‐ byte “byte” � Lots of these in typical code 09 April 2012 Machine Programming 3 University of Washington Moving Data: IA32 %eax %ecx � Moving Data %edx movl Source , Dest : %ebx � Operand Types � Operand Types %esi � Immediate: Constant integer data � Example: $0x400 , $-533 %edi � Like C constant, but prefixed with ‘$’ %esp � Encoded with 1, 2, or 4 bytes %ebp � Register: One of 8 integer registers � Example: %eax, %edx Example: %eax, %edx � But %esp and %ebp reserved for special use � Others have special uses for particular instructions � Memory: 4 consecutive bytes of memory at address given by register � Simplest example: (%eax) � Various other “address modes” 09 April 2012 Machine Programming 4
University of Washington movl Operand Combinations Source Dest Src,Dest C Analog movl $0x4,%eax Reg Imm movl $-147,(%eax) Mem movl %eax,%edx Reg movl Reg Mem movl %eax,(%edx) movl (%eax),%edx Mem Reg Cannot do memory ‐ memory transfer with a single instruction. How do you copy from a memory location to another then? 09 April 2012 Machine Programming 5 University of Washington movl Operand Combinations Source Dest Src,Dest C Analog Reg movl $0x4,%eax temp = 0x4; Imm Mem movl $-147,(%eax) *p = -147; Reg movl %eax,%edx temp2 = temp1; movl Reg movl %eax,(%edx) *p = temp; Mem Mem Reg movl (%eax),%edx temp = *p; 09 April 2012 Machine Programming 6
University of Washington Memory vs. registers � Why both? � Performance? � Usage difference? 09 April 2012 Machine Programming 7 University of Washington Simple Memory Addressing Modes � Normal (R) Mem[Reg[R]] � Register R specifies memory address movl (%ecx),%eax � Displacement D(R) Mem[Reg[R]+D] � Register R specifies start of memory region � Constant displacement D specifies offset Constant displacement D specifies offset movl 8(%ebp),%edx 09 April 2012 Machine Programming 8
University of Washington Using Simple Addressing Modes swap: pushl %ebp Set movl %esp,%ebp Up p pushl %ebx p void swap(int xp, int yp) void swap(int *xp, int *yp) { movl 12(%ebp),%ecx int t0 = *xp; int t1 = *yp; movl 8(%ebp),%edx *xp = t1; movl (%ecx),%eax Body *yp = t0; movl (%edx),%ebx } movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp Finish popl %ebp ret 09 April 2012 Machine Programming 9 University of Washington Understanding Swap • void swap(int *xp, int *yp) Stack • { • (in memory) int t0 = *xp; Offset int t1 = *yp; int t1 = *yp; 12 yp *xp = t1; *yp = t0; 8 xp } 4 Rtn adr 0 Old % ebp %ebp -4 Old % ebx Register Value %ecx %ecx yp yp movl 12(%ebp),%ecx l 12(% b ) % # # ecx = yp %edx xp movl 8(%ebp),%edx # edx = xp %eax t1 movl (%ecx),%eax # eax = *yp (t1) %ebx t0 movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # *xp = eax movl %ebx,(%ecx) # *yp = ebx 09 April 2012 Machine Programming 10
University of Washington Address Understanding Swap 0x124 123 456 0x120 0x11c %eax 0x118 Offset %edx 0x114 0x114 yp 12 0x120 0x110 %ecx xp 8 0x124 0x10c %ebx 4 Rtn adr 0x108 %esi 0 %ebp 0x104 %edi -4 0x100 %esp movl 12(%ebp),%ecx l 12(% b ) % # # ecx = yp %ebp 0x104 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 09 April 2012 Machine Programming 11 University of Washington Address Understanding Swap 123 0x124 456 0x120 0x11c %eax 0x118 Offset %edx 0x114 0 yp 12 0x120 0x120 %ecx 0x120 0x110 xp 8 0x124 0x10c %ebx 4 Rtn adr 0x108 %esi 0 %ebp 0x104 %edi -4 0x100 %esp movl 12(%ebp),%ecx l 12(% b ) % # ecx = yp # %ebp 0x104 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 09 April 2012 Machine Programming 12
University of Washington Address Understanding Swap 0x124 123 456 0x120 0x11c %eax 0x118 Offset %edx 0x124 0x114 0x114 yp 12 0x120 0x110 %ecx 0x120 xp 8 0x124 0x124 0x10c %ebx 4 Rtn adr 0x108 %esi 0 %ebp 0x104 %edi -4 0x100 %esp movl 12(%ebp),%ecx l 12(% b ) % # # ecx = yp %ebp 0x104 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 09 April 2012 Machine Programming 13 University of Washington Address Understanding Swap 123 0x124 456 456 0x120 0x11c %eax 456 0x118 Offset %edx 0x124 0x114 0 yp 12 0x120 %ecx 0x120 0x110 xp 8 0x124 0x10c %ebx 4 Rtn adr 0x108 %esi 0 %ebp 0x104 %edi -4 0x100 %esp movl 12(%ebp),%ecx l 12(% b ) % # # ecx = yp %ebp 0x104 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 09 April 2012 Machine Programming 14
University of Washington Address Understanding Swap 0x124 123 123 456 0x120 0x11c %eax 456 0x118 Offset %edx 0x124 0x114 0x114 yp 12 0x120 0x110 %ecx 0x120 xp 8 0x124 0x10c %ebx 123 4 Rtn adr 0x108 %esi 0 %ebp 0x104 %edi -4 0x100 %esp movl 12(%ebp),%ecx l 12(% b ) % # # ecx = yp %ebp 0x104 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 09 April 2012 Machine Programming 15 University of Washington Address Understanding Swap 456 0x124 456 0x120 0x11c %eax 456 456 0x118 Offset %edx 0x124 0x114 0 yp 12 0x120 %ecx 0x120 0x110 xp 8 0x124 0x10c %ebx 123 123 4 Rtn adr 0x108 %esi 0 %ebp 0x104 %edi -4 0x100 %esp movl 12(%ebp),%ecx l 12(% b ) % # # ecx = yp %ebp 0x104 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 09 April 2012 Machine Programming 16
University of Washington Address Understanding Swap 0x124 456 123 0x120 0x11c %eax 456 0x118 Offset %edx 0x124 0x114 0x114 yp 12 0x120 0x110 %ecx 0x120 xp 8 0x124 0x10c %ebx 123 123 4 Rtn adr 0x108 %esi 0 %ebp 0x104 %edi -4 0x100 %esp movl 12(%ebp),%ecx l 12(% b ) % # # ecx = yp %ebp 0x104 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 09 April 2012 Machine Programming 17 University of Washington x86 ‐ 64 Integer Registers %rax %r8 %eax %r8d %rbx %r9 %ebx %r9d %rcx %r10 %ecx %r10d %rdx %r11 %edx %r11d %rsi %r12 %esi %r12d %rdi %r13 %edi %r13d %rsp %r14 %esp %r14d %rbp %r15 %ebp %r15d � Extend existing registers. Add 8 new ones. � Make %ebp / %rbp general purpose
University of Washington Instructions � Long word l (4 Bytes) ↔ Quad word q (8 Bytes) � New instructions: � movl → movq � addl → addq � sall → salq � etc. � 32 ‐ bit instructions generate 32 ‐ bit results, � What about the other 32 bits in the register? � Set higher order bits of destination register to 0 � Example: addl University of Washington Swap in 32 ‐ bit Mode swap: void swap(int *xp, int *yp) pushl %ebp { movl %esp,%ebp Setup int t0 = *xp; int t0 = *xp; pushl %ebx int t1 = *yp; *xp = t1; movl 12(%ebp),%ecx *yp = t0; movl 8(%ebp),%edx } movl (%ecx),%eax Body movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp Finish popl %ebp ret
University of Washington Swap in 64 ‐ bit Mode void swap(int *xp, int *yp) swap: { movl (%rdi), %edx int t0 = *xp; movl (%rsi), %eax i t t1 int t1 = *yp; * movl l % %eax, (%rdi) (% di) *xp = t1; movl %edx, (%rsi) *yp = t0; retq } � Operands passed in registers (why useful?) � First ( xp ) in %rdi , second ( yp ) in %rsi � 64 ‐ bit pointers 64 bi i � No stack operations required � 32 ‐ bit data � Data held in registers %eax and %edx � movl operation University of Washington Swap Long Ints in 64 ‐ bit Mode void swap_l swap_l: (long int *xp, long int *yp) movq (%rdi), %rdx { { movq movq (%rsi) (%rsi), %rax %rax long int t0 = *xp; movq %rax, (%rdi) long int t1 = *yp; movq %rdx, (%rsi) *xp = t1; retq *yp = t0; } � 64 bit data � 64 ‐ bit data � Data held in registers %rax and %rdx � mov q operation � “q” stands for quad ‐ word
Recommend
More recommend