700 likes | 856 Views
Machine-Level Representation of Programs Ⅱ. Outline. Data movement Data manipulation Control structure Suggested reading Chap 3.4, 3.5, 3.6. Indexed Addressing Mode Figure 3.3 P137. Most general form Imm ( E b , E i , s) M[Imm+ R[E b ]+ R[E i ]*s]
E N D
Outline • Data movement • Data manipulation • Control structure • Suggested reading • Chap 3.4, 3.5, 3.6
Indexed Addressing Mode Figure 3.3 P137 • Most general form • Imm(Eb, Ei, s) • M[Imm+ R[Eb]+ R[Ei]*s] • Constant “displacement” Imm: 1, 2, or 4 bytes • Base register Eb: Any of 8 integer registers • Index register Ei : Any, except for %esp • S: Scale: 1, 2, 4, or 8
Move Instructions • Format • movl src, dest • src and dest can only be one of the following • Immediate (except dest) • Register • Memory
Move Instructions • Format • The only possible combinations of the (src, dest) are • (immediate, register) • (memory, register) load • (register, register) • (immediate, memory) store • (register, memory) store
Data Movement Example P139 movl $0x4050, %eax immediate register movl %ebp, %esp register register movl (%edx, %ecx), %eax memory register movl $-17, (%esp) immediate memory movl %eax, -12(%ebp) register memory
Data Movement Example P139 Initial value %dh=8d %eax =98765432 • movb %dh, %al %eax=9876548d • movsbl %dh, %eax %eax=ffffff8d (Move sign-extended byte) • movzbl %dh, %eax %eax=0000008d ( Move zero-extended byte)
Stack operations Figure 3.5P140 Increasing address %esp 0x108 Stack “top”
Stack operations pushl %eax 0x108 %esp 0x104 Stack “top”
Stack operations popl %edx %esp 0x108 0x104 Stack “top”
Data Movement Example P141 int exchange(int *xp, int y) { int x = *xp ; /* operator * performs dereferencing */ *xp = y ; return x ; } int a = 4 ; int b = exchange(&a, 3); /* “address of” operator creates a pointer */ printf(“a = %d, b = %d\n”, a, b);
Data Movement Example P142 1 pushl %ebp 2 movl %esp, %ebp 3 movl 8(%ebp), %eax 4 movl 12(%ebp), %edx 5 movl (%eax), %ecx 6 movl %edx, (%eax) 7 movl %ecx, %eax 8 movl %ebp, %esp 9 popl %ebp int exchange(int *xp, int y) { int x = *xp ; *xp = y ; return x ; } Assembly code
• • • Stack Offset y xp Rtn adr %esp Data Movement Example
• • • Stack Offset • • • Stack 12 y Offset 8 xp 4 Rtn adr %esp 0 Old %ebp y xp Rtn adr %esp Data Movement Example 1 pushl %ebp
• • • • • • Stack Stack Offset Offset 12 y 12 y 8 xp 8 xp 4 Rtn adr 4 Rtn adr %ebp %esp 0 Old %ebp 0 Old %ebp %esp Data Movement Example 2 movl %esp, %ebp
• • • Stack Offset 12 y 8 xp 4 Rtn adr %ebp %esp 0 Old %ebp Data Movement Example 3 movl 8(%ebp), %eax 4 movl 12(%ebp), %edx 5 movl (%eax), %ecx 6 movl %edx, (%eax) 7 movl %ecx, %eax
• • • Stack Offset y xp Rtn adr %esp Data Movement Example 8 movl %ebp, %esp 9 popl %ebp
3.5 P143 Arithmetic and Logical Operations Figure 3.7 P144
Examples for Lea Instruction (Practice Problem 3.3 P143) • %eax holds x, %ecx holds y Expression Result leal 6(%eax), %edx 6+x leal (%eax, %ecx), %edx x+y leal (%eax, %ecx, 4), %edx x+4*y leal 7(%eax, %eax, 8), %edx 7+9*x leal 0xA(, %ecx, 4), %edx 10+4*y leal 9(%eax, %ecx, 2), %edx 9+x+2*y
Arithmetic and Logical Operations (Practice Problem 3.4 P145) Instruction Destination Value addl %ecx, (%eax) 0x100 0x100 (1+0xFF) subl %edx, 4(%eax) 0x104 0xA8 (0xAB-0x3) imull $16, (%eax, %edx, 4) 0x10C 0x110 ($16*0x11) incl 8(%eax) 0x108 0x14 (0x13+1) decl %ecx %ecx 0x0 (0x1-1) subl %edx, %eax %eax 0xFD (0x100-0x3)
Assembly Code for Arithmetic ExpressionsFigure 3.8 P146 int arith(int x, int y, int z) { int t1 = x+y; int t2 = z*48; int t3 = t1&0xFFFF; int t4 = t2*t3; return t4; } movl 12(%ebp),%eax Get y movl 16(%ebp),%edx Get z addl 8(%ebp),%eax Compute t1=x+y leal (%edx,%edx,2),%edx Compute 3*z sall $4,%edx Compute t2=48*z=3*16*z andl $0xFFFF,%eax Compute t3=t1&FFFF imull %eax,%edx Compute t4=t2*t3 movl %edx,%eax Set t4 as return val
Examples P148 Initially x at %ebp+8, y at %ebp+12, their full 64-bit product as 8 bytes on top of the stack 1 movl 8(%ebp), %eax 2 imull 12(%ebp) 3 pushl %edx 4 pushl %eax Store x/y and x%y on the stack. 1 movl 8(%ebp), %eax 2 cltd 3 idivl 12(%ebp) 4 pushl %eax 5 pushl %edx
3.6 P148 Control • Two of the most important parts of program execution • Data flow (Accessing and operating data) • Control flow (control the sequence of operations)
Control • Sequential execution is default • The statements in C and • the instructions in assembly code • are executed in the order they appear in the program • Chang the control flow • Control constructs in C • Jump in assembly
FF C0 %eax %ah %al Addresses BF Stack %edx %dh %dl %ecx %ch %cl Data %ebx %bh %bl 80 Heap 7F %esi %edi Instructions %esp 40 DLLs %ebp 3F Heap %eip Data %eflag 08 Text 00 Assembly Programmer’s View
Condition codes • Condition codes • A set of single-bit • Maintained in a condition code register • Describe attributes of the most recently arithmetic or logical operation
Condition codes • EFLAGS • CF: Carry Flag • The most recent operation generated a carry out of the most significant bit • Used to detect overflow for unsigned operations • OF: Overflow Flag • The most recent operation caused a two’s complement overflow — either negative or positive
Condition codes • EFLAGS • ZF: Zero Flag • The most recent operation yielded zero • SF: Sign Flag • The most recent operation yielded a negative value
Setting Conditional Codes • Implicit SettingBy 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)
Conditional Code • lea instruction • has no effect on condition codes • Xorl instruction • The carry and overflow flags are set to 0 • Shift instruction • carry flag is set to the last bit shifted out • Overflow flag is set to 0
Setting Conditional Codes • Explicit Setting by Compare Instruction cmpl Src2,Src1 • cmpl b,a like computing a-bwithout setting destination • CF set if carry out from most significant bit • Used for unsigned comparisons • ZF set if a == b • SF set if (a-b) < 0 • OF set if two’s complement overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)
Setting Conditional Codes • Explicit Setting by Test instruction testl Src2,Src1 • Sets condition codes based on value of Src1&Src2 • Useful to have one of the operands be a mask • testl b,a like computing a&b without setting destination • ZF set when a&b == 0 • SF set when a&b < 0
Accessing Conditional Codes • The condition codes cannot be read directly • One of the most common methods of accessing them is • setting an integer register based on some combination of condition codes • Set commands
Accessing Conditional Codes • after each set command is executed • A single byte to 0 or to 1 is obtained • The descriptions of the different set commands apply to the case • where a comparison instruction has been executed
Accessing Conditional Codes • The destination operand is either • one of the eight single-byte register elements or • a memory location where the single byte is to be stored • To generate a 32-bit result • we must also clear the high-order 24 bits
Accessing Conditional Codes P151 Initially a is in %edx, b is in %eax 1 cmpl %eax, %edx #compare a:b 2 setl %al #set low order by to 0 or 1 3 movzbl %al, %eax #set remaining bytes of %eax to 0
Jump Instructions P152 • Under normal execution • instructions follow each other in the order they are listed • A jump instruction can cause • the execution to switch to a completely new position in the program. • Label • Jump destinations
Jump Instructions P152 1 xorl %eax, %eax Set %eax to 0 2 jmp .L1 Goto .L1 3 movl (%eax), %edx Null pointer dereference 4 .L1: 5 popl %edx
Unconditional jump P153 • Jumps unconditionally • Direct jump: jmp label • jmp .L • Indirect jump: jmp *Operand • jmp *%eax • jmp *(%eax)
Conditional jump • Either jump or continue executing at the next instruction in the code sequence • Depending on some combination of the condition codes • All direct jump
Jump Instructions P154 1 jle .L4 2 .p2align 4,,7 align next instruction to multiple of 8 3 .L5: • movl %edx, %eax • sarl $1, %eax ;Arithmetic right shift • subl %eax, %edx • testl %edx, %edx • jg .L5 9 .L4: 10 movl %edx, %eax
Jump Instructions • PC-relative • Jump target is an offset relative to the address of the instruction just followed jump (pointed by PC) • Absolute address • Jump target is an absolute address
Example for Jump P154 1 8: 7e 11 jle 1b<silly+0x1b> 2 a: 8d b6 00 00 00 00 lea 0x0(%esi), %esi 3 10: 89 d0 movl %edx, %eax dest1: 4 12: c1 f8 01 sarl $1, %eax 5 15: 29 c2 subl %eax, %edx 6 17: 85 d2 testl %edx, %edx • 19: 7f f5 jg 10<silly+0x10> • 1b: 89 d0 movl %edx, %eax dest2: dest1: 11+a = 1b 11+10 dest2: 1b+f5(-b) =10 F5=-11=0X(-b)
Example for Jump P155 1 80483c8: 7e 11 jle 80483db<silly+0x1b> 2 80483ca: 8d b6 00 00 00 00 lea 0x0(%esi), %esi 3 80483d0: 89 d0 movl %edx, %eax dest1: 4 80483d2: c1 f8 01 sarl $1, %eax 5 80483d5: 29 c2 subl %eax, %edx 6 80483d7: 85 d2 testl %edx, %edx • 80483d9: 7f f5 jg 80483d0<silly+0x10> • 80483db: 89 d0 movl %edx, %eax dest2: 11+a = 1b => 11+ca=db 1b+f5(-b) =10 => db+f5(-b)=d0
Translating Conditional Branches P157 t = test-expr ; if ( t ) goto true ; else-statement goto done true: then-statement done: if ( test-expr ) then-statement else else-statement
int absdiff(int x, int y) • { • if (x < y) • return y – x; • else • return x – y; • } • int gotodiff(int x, int y) • { • int rval ; • if (x < y) • goto less • rval = x – y ; • goto done; • less: • rval = y – x; • done: • return rval; • } Translating Conditional Branches P156