Carnegie Mellon MachineLevel Programming II Control Slides adapted

  • Slides: 23
Download presentation
Carnegie Mellon Machine-Level Programming II: Control Slides adapted from Bryant and O’Hallaron 1

Carnegie Mellon Machine-Level Programming II: Control Slides adapted from Bryant and O’Hallaron 1

What have we learnt so far ¢ Abstract view of machine § CPU (CPU

What have we learnt so far ¢ Abstract view of machine § CPU (CPU state: registers, conditional codes, %RIP) § Memory ISA is an API to hardware ¢ Example instructions: ¢ § § § MOV: move data between CPU/memory Arithmetic (ADD, SUB) LEA Various addressing modes: mov %rax, 0 x 8(%rdx) No instructions operate on two memory operands 2

Carnegie Mellon Today: How control flow is done Condition codes ¢ Conditional branches ¢

Carnegie Mellon Today: How control flow is done Condition codes ¢ Conditional branches ¢ Loops ¢ Switch Statements ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3

Carnegie Mellon Processor State (x 86 -64) ¢ Registers %rax %r 8 %rbx %r

Carnegie Mellon Processor State (x 86 -64) ¢ Registers %rax %r 8 %rbx %r 9 %rcx %r 10 %rdx %r 11 %rsi %r 12 %rdi %r 13 %rsp %r 14 %rbp %r 15 %rip ¢ Condition codes (single bit registers) CF ZF SF OF Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4

Carnegie Mellon Condition Codes (implicit setting) ¢ Implicitly set by arithmetic operations Example: addq

Carnegie Mellon Condition Codes (implicit setting) ¢ Implicitly set by arithmetic operations Example: addq Src, Dest t = a + b CF (Carry Flag) set if unsigned overflow ZF (Zero Flag) set if t == 0 SF (Sign Flag) set if t < 0 (as signed) OF (Over. Flow Flag) set if signed overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0) ¢ Not set by leaq ¢ Examples: § a=0 xff. . . ff b=0 x 00. . 01 § a=0 x 80. . . 00 b=0 xff. . ff Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5

Carnegie Mellon Condition Codes (Explicit Setting: Compare) ¢ Explicit set by Compare Instruction §cmpq

Carnegie Mellon Condition Codes (Explicit Setting: Compare) ¢ Explicit set by Compare Instruction §cmpq b, a like computing a-b without setting destination §CF set if unsigned overflow §ZF set if a == b §SF set if (a-b) < 0 (as signed) §OF set if two’s-complement (signed) overflow (a>=0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0) ¢ Examples: § cmpq $0 x 10, %rax (Give an example value of %rax such that CF/ZF/SF/OF set? ) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6

Carnegie Mellon Condition Codes (Explicit Setting: Test) ¢ Explicit set by Test instruction §testq

Carnegie Mellon Condition Codes (Explicit Setting: Test) ¢ Explicit set by Test instruction §testq b, a (like computing a&b without setting destination) §ZF set when a&b == 0 §SF set when a&b < 0 ¢ Examples: § testq %rax, %rax (when is ZF/SF set? ) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 7

Carnegie Mellon Reading Condition Codes ¢ Set. X Instructions § Set low-order byte of

Carnegie Mellon Reading Condition Codes ¢ Set. X Instructions § Set low-order byte of destination register to 0/1 based on condition codes § Does not alter remaining 7 bytes Set. X sete setne sets setns setg Condition ZF ~ZF SF ~(SF^OF)&~ZF setge ~(SF^OF) setle seta setb (SF^OF)|ZF ~CF&~ZF CF Description Equal / Zero Not Equal / Not Zero Negative Nonnegative Greater (Signed) Greater or Equal (Signed) Less or Equal (Signed) Above (unsigned) Below (unsigned) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition cmpq %rbx, %rax setl %al • SF=1 OF=0 %rbx > %rax • SF=0 OF=1 %rax<0, %rbx > 0 8

x 86 -64 Integer Registers %rax %al %r 8 b %rbx %bl %r 9

x 86 -64 Integer Registers %rax %al %r 8 b %rbx %bl %r 9 b %rcx %cl %r 10 b %rdx %dl %r 11 b %rsi %sil %r 12 b %rdi %dil %r 13 b %rsp %spl %r 14 b %rbp %bpl %r 15 b %eax Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 9

Carnegie Mellon Reading Condition Codes (Cont. ) ¢ Set. X Example: int gt (long

Carnegie Mellon Reading Condition Codes (Cont. ) ¢ Set. X Example: int gt (long x, long y) { return x > y; } cmpq %rsi, %rdi setg %al movzbl %al, %eax ret Register Use(s) %rdi Argument x %rsi Argument y %eax Return value # Compare x: y # Set when > # Zero rest of %eax 10

Carnegie Mellon Today Control: Condition codes ¢ If statements ¢ While and For loops

Carnegie Mellon Today Control: Condition codes ¢ If statements ¢ While and For loops ¢ 11

Carnegie Mellon Jumping ¢ j. X Instructions: jump to different part of code j.

Carnegie Mellon Jumping ¢ j. X Instructions: jump to different part of code j. X Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12

Carnegie Mellon Conditional Branch Example ¢ gcc –Og –S compare. c long compare (long

Carnegie Mellon Conditional Branch Example ¢ gcc –Og –S compare. c long compare (long x, long y) { long result; if (x > 10*y) result = 1; else result = 0; return result; } Register Use(s) %rdi Argument x %rsi Argument y %rax Return value compare: leaq addq cmpq jge movl ret. L 3: movl ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition (%rsi, 4), %rax, %rax %rdi, %rax. L 3 10*y-x $1, %eax $0, %eax 13

Carnegie Mellon Equivalent goto code ¢ Machine code is essentially doing control flow with

Carnegie Mellon Equivalent goto code ¢ Machine code is essentially doing control flow with goto statement long compare (long x, long y) { long result; if (x > 10*y) result = 1; else result = 0; return result; } long absdiff_j (long x, long y) { long result; int ntest = 10*y-x; if (ntest >= 0) goto Else; result = 1; return result; Else: result = 0; Done: return result; } Do not write this normally Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14

Carnegie Mellon Today Control: Condition codes ¢ If statements ¢ Loops ¢ Switch Statements

Carnegie Mellon Today Control: Condition codes ¢ If statements ¢ Loops ¢ Switch Statements ¢ 15

Carnegie Mellon “While” Translation #1 ¢ ¢ “Jump-to-middle” translation Used with -Og While version

Carnegie Mellon “While” Translation #1 ¢ ¢ “Jump-to-middle” translation Used with -Og While version while (Test) Body Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Goto Version goto test; loop: Body test: if (Test) goto loop; 16

Carnegie Mellon “While” Translation example #1 ¢ gcc –Og –S log_2. c long log_2(unsigned

Carnegie Mellon “While” Translation example #1 ¢ gcc –Og –S log_2. c long log_2(unsigned long x) { long log = 0; while (x != 0) { x = x >> 1; log++; } return log; } Register Use(s) %rdi Argument x %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition right shift, zero fill log_2: movl jmp $0, %eax. L 2 shrq addq %rdi $1, %rax testq jne ret %rdi, %rdi. L 3: . L 2: 17

Carnegie Mellon “While” Translation #2 While version while (Test) Body ¢ ¢ Used with

Carnegie Mellon “While” Translation #2 While version while (Test) Body ¢ ¢ Used with –O 1 –O 2 Why? § allows optimization of initial condition Do-While Version if (!Test) goto done; do Body while(Test); done: Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Goto Version if (!Test) goto done; loop: Body if (Test) goto loop; done: 18

Carnegie Mellon “While” Translation example #2 ¢ gcc –O 1 –S log_2. c long

Carnegie Mellon “While” Translation example #2 ¢ gcc –O 1 –S log_2. c long log_2(unsigned long x) { long log = 0; while (x != 0) { x = x >> 1; log++; } return log; } Register Use(s) %rdi Argument x %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition log_2: testq je movl %rdi, %rdi. L 4 $0, %eax shrq addq testq jne ret %rdi $1, %rax %rdi, %rdi. L 3 movl ret $0, %eax . L 3: . L 4: 19

Carnegie Mellon “For” Loop translation For Version for (Init; Test; Update ) Body While

Carnegie Mellon “For” Loop translation For Version for (Init; Test; Update ) Body While Version Init; while (Test ) { Body Update; } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 20

Carnegie Mellon “Loop” Translation example ¢ gcc –Og –S log_2. c int sum(int n)

Carnegie Mellon “Loop” Translation example ¢ gcc –Og –S log_2. c int sum(int n) { int sum = 0; for (int i=0; i<n; i++){ sum += i; } return sum; } sum: movl jmp $0, %edx $0, %eax. L 5 addl %edx, %eax $1, %edx . L 6: . L 5: cmpl %edi, %edx jl. L 6 rep ret Register Use(s) %rdi Argument x %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 21

Carnegie Mellon Summary ¢ Today § Control: Condition codes § If statements § While

Carnegie Mellon Summary ¢ Today § Control: Condition codes § If statements § While and for statements ¢ Not discussed § Switch statements Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 22

Binary Puzzle mystery: movl jmp. L 10: testl je addl. L 9: addl. L

Binary Puzzle mystery: movl jmp. L 10: testl je addl. L 9: addl. L 8: cmpl jle ret $0, %edx $0, %eax $1, %ecx. L 8 %ecx, %edi. L 9 $1, %eax %ecx, %ecx $1, %edx ¢ Register Use(s) %rdi Argument x %rsi Argument y %rax Return value Function signature? § one, two or more parameters? type? § return type? ¢ Is there a loop? What’s termination condition? $31, %edx. L 10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 23