Machine-Level Programming V: Switch Statements Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified slides from.

Slides:



Advertisements
Similar presentations
University of Amsterdam Computer Systems – Control in C Arnoud Visser 1 Computer Systems New to C?
Advertisements

Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Carnegie Mellon Instructors: Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures : Introduction.
Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Machine-Level Programming II: Control Flow Sept. 12, 2002 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II Control Flow Sept. 13, 2001 Topics Condition Codes –Setting –Testing Control Flow –If-then-else –Varieties of Loops –Switch.
Machine-Level Programming II: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
– 1 – Homework 1 Count 25 Mean 73.8 Std Min 22 Max 96 Mode 66 Median80.5.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Lee CSCE 312 TAMU 1 Based on slides provided by Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures Instructor:
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
University of Washington x86 Programming III The Hardware/Software Interface CSE351 Winter 2013.
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
II:1 x86 Assembly - Control. II:2 Alternate reference source Go to the source: Intel 64 and IA32 
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
University of Washington Today Lab 2 due next Monday! Finish-up control flow Switch statements 1.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Carnegie Mellon Machine-Level Programming III: Switch Statements, 1-D Array and IA32 Procedures Lecture, Mar. 7, 2013 These slides are from
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic and Control Lecture, Feb. 28, 2012 These slides are from website which.
1 Machine-Level Programming IV: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2015 Systems book chapter 3* * Modified.
תרגול 5 תכנות באסמבלי, המשך
MACHINE-LEVEL PROGRAMMING III: SWITCH STATEMENTS AND IA32 PROCEDURES.
Machine-Level Programming II Control Flow Sept. 10, 1998 Topics Control Flow –Varieties of Loops –Switch Statements class06.ppt “The course that.
Lecture 7 Flow of Control Topics Finish Lecture 6 slides Lab 02 = datalab comments Assembly Language flow of control Test 1 – a week from wednesday February.
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
Machine-Level Programming II Control Flow Sept. 14, 2000 Topics Condition Codes –Setting –Testing Control Flow –If-then-else –Varieties of Loops –Switch.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements class06.ppt.
Machine-Level Programming 2 Control Flow
Recitation 2 – 2/4/01 Outline Machine Model
Machine-Level Programming II Control Flow Sept. 9, 1999
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Machine-Level Programming II: Control
Machine-Level Representation of Programs II
Machine-Level Programming II: Control Flow
x86-64 Programming III & The Stack CSE 351 Winter 2018
Carnegie Mellon Machine-Level Programming II: Control : Introduction to Computer Systems 6th Lecture, Sept. 13, 2018.
Machine-Level Programming III: Switch Statements and IA32 Procedures
Instructors: Majd Sakr and Khaled Harras
Assembly Programming IV CSE 351 Spring 2017
x86-64 Programming III & The Stack CSE 351 Winter 2018
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
Carnegie Mellon Wrap-up of Machine-Level Programming II: Control : Introduction to Computer Systems Sept. 18, 2018.
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Machine-Level Programming 2 Control Flow
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
Machine-Level Programming II: Control Flow
Machine-Level Programming 2 Control Flow
x86-64 Programming III & The Stack CSE 351 Autumn 2017
Machine-Level Representation of Programs III
Today Control flow if/while/do while/for/switch
Machine-Level Programming 2 Control Flow
x86 Programming III CSE 351 Autumn 2016
X86 Assembly - Control.
Machine-Level Programming II: Control Flow Sept. 12, 2007
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
CS201- Lecture 8 IA32 Flow Control
Credits and Disclaimers
Presentation transcript:

Machine-Level Programming V: Switch Statements Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified slides from the book “Computer Systems: a Programmer’s Perspective”, Randy Bryant & David O’Hallaron, 2011

2 Today Switch statements IA 32 Procedures  Stack Structure  Calling Conventions  Illustrations of Recursion & Pointers

3 Switch Statement Example Multiple case labels  Here: 5 & 6 Fall through cases  Here: 2 Missing cases  Here: 4 long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; /* Fall Through */ case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; } long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; /* Fall Through */ case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; }

4 Switch Implementation Options  Series of conditionals (if statements)  Good if few cases  Slow if many  Jump Table  Lookup branch target  Avoids conditionals  Possible when cases are small integer constants  Advantage: can do k-way branch in O(1) operations  GCC  Picks one based on case structure

5 Jump Table Structure Code Block 0 Targ0: Code Block 1 Targ1: Code Block 2 Targ2: Code Block n–1 Targn-1: Targ0 Targ1 Targ2 Targn-1 jtab: target = JTab[x]; goto *target; target = JTab[x]; goto *target; switch(x) { case val_0: Block 0 case val_1: Block 1 case val_n-1: Block n–1 } switch(x) { case val_0: Block 0 case val_1: Block 1 case val_n-1: Block n–1 } Switch Form Approximate Translation Jump Table Jump Targets

6 Switch Statement Example (IA32) Setup: long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... } return w; } long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... } return w; } switch_eg: pushl%ebp# Setup movl%esp, %ebp# Setup movl8(%ebp), %eax# %eax = x cmpl$6, %eax# Compare x:6 ja.L2 # If unsigned > goto default jmp*.L7(,%eax,4)# Goto *JTab[x] What range of values takes default? Note that w not initialized here

7 Switch Statement Example (IA32) long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... } return w; } long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... } return w; } Indirect jump Jump table.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6 Setup: switch_eg: pushl%ebp# Setup movl%esp, %ebp# Setup movl8(%ebp), %eax# eax = x cmpl$6, %eax# Compare x:6 ja.L2 # If unsigned > goto default jmp*.L7(,%eax,4)# Goto *JTab[x]

8 Assembly Setup Explanation Table Structure  Each target requires 4 bytes  Base address at.L7 Jumping cmpl$6, %eax# Compare x:6 ja.L2 # If unsigned > goto default  Jump target is denoted by label.L2  When does ja jump? Above (unsigned): ~CF&~ZF CF == 0 & ZF == 0 CF == 1 & ZF == 0

9 Assembly Setup Explanation Table Structure  Each target requires 4 bytes  Base address at.L7 Jumping  Direct: jmp.L2  Jump target is denoted by label.L2  Indirect: jmp *.L7(,%eax,4)  Start of jump table:.L7  Must scale by factor of 4 (labels have 32-bits = 4 Bytes on IA32)  Fetch target from effective Address.L7 + eax*4  Only for 0 ≤ x ≤ 6 Jump table.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6

10.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6 Jump Table Jump table switch(x) { case 1: //.L3 w = y*z; break; case 2: //.L4 w = y/z; /* Fall Through */ case 3: //.L5 w += z; break; case 5: case 6: //.L6 w -= z; break; default: //.L2 w = 2; } switch(x) { case 1: //.L3 w = y*z; break; case 2: //.L4 w = y/z; /* Fall Through */ case 3: //.L5 w += z; break; case 5: case 6: //.L6 w -= z; break; default: //.L2 w = 2; }

11 Handling Fall-Through long w = 1;... switch(x) {... case 2: w = y/z; /* Fall Through */ case 3: w += z; break;... } long w = 1;... switch(x) {... case 2: w = y/z; /* Fall Through */ case 3: w += z; break;... } case 3: w = 1; goto merge; case 3: w = 1; goto merge; case 2: w = y/z; case 2: w = y/z; merge: w += z; merge: w += z; Why set w to 1 here? Don’t need to in case 2!

12 Code Blocks (Partial).L2:# Default movl$2, %eax# w = 2 jmp.L8# Goto done.L5:# x == 3 movl$1, %eax# w = 1 jmp.L9# Goto merge.L3:# x == 1 movl16(%ebp), %eax # z imull12(%ebp), %eax # w = y*z jmp.L8# Goto done.L2:# Default movl$2, %eax# w = 2 jmp.L8# Goto done.L5:# x == 3 movl$1, %eax# w = 1 jmp.L9# Goto merge.L3:# x == 1 movl16(%ebp), %eax # z imull12(%ebp), %eax # w = y*z jmp.L8# Goto done switch(x) { case 1: //.L3 w = y*z; break;... case 3: //.L5 w += z; break;... default: //.L2 w = 2; } switch(x) { case 1: //.L3 w = y*z; break;... case 3: //.L5 w += z; break;... default: //.L2 w = 2; }

13 Code Blocks (Rest).L4:# x == 2 movl12(%ebp), %edx movl%edx, %eax sarl$31, %edx idivl16(%ebp)# w = y/z.L9:# merge: addl16(%ebp), %eax # w += z jmp.L8# goto done.L6:# x == 5, 6 movl$1, %eax # w = 1 subl16(%ebp), %eax # w = 1-z.L4:# x == 2 movl12(%ebp), %edx movl%edx, %eax sarl$31, %edx idivl16(%ebp)# w = y/z.L9:# merge: addl16(%ebp), %eax # w += z jmp.L8# goto done.L6:# x == 5, 6 movl$1, %eax # w = 1 subl16(%ebp), %eax # w = 1-z switch(x) {... case 2: //.L4 w = y/z; /* Fall Through */ merge: //.L9 w += z; break; case 5: case 6: //.L6 w -= z; break; } switch(x) {... case 2: //.L4 w = y/z; /* Fall Through */ merge: //.L9 w += z; break; case 5: case 6: //.L6 w -= z; break; }

14 Switch Code (Finish) Noteworthy Features  Jump table avoids sequencing through cases  Constant time, rather than linear  Use jump table to handle holes and duplicate tags  Use program sequencing to handle fall-through  Don’t initialize w = 1 unless really need it return w;.L8:# done: popl%ebp ret.L8:# done: popl%ebp ret

15 Switch Puzzle  What value returned when x is invalid? Answer  We do an unsigned test: ja  Thus would go to default  also works when x is negative (first bit is 1 so its unsigned value is large)

16 x86-64 Switch Implementation.section.rodata.align 8.L7:.quad.L2# x = 0.quad.L3# x = 1.quad.L4# x = 2.quad.L5# x = 3.quad.L2# x = 4.quad.L6# X = 5.quad.L6# x = 6.section.rodata.align 8.L7:.quad.L2# x = 0.quad.L3# x = 1.quad.L4# x = 2.quad.L5# x = 3.quad.L2# x = 4.quad.L6# X = 5.quad.L6# x = 6 Jump Table Same general idea, adapted to 64-bit code Table entries 64 bits (pointers) Cases use revised code.L3: movq%rdx, %rax imulq%rsi, %rax ret.L3: movq%rdx, %rax imulq%rsi, %rax ret switch(x) { case 1: //.L3 w = y*z; break;... } switch(x) { case 1: //.L3 w = y*z; break;... }

17 IA32 Object Code Setup  Label.L2 becomes address 0x  Label.L7 becomes address 0x : :77 07 ja b:ff jmp *0x (,%eax,4) : :77 07 ja b:ff jmp *0x (,%eax,4) switch_eg:... ja.L2 # If unsigned > goto default jmp*.L7(,%eax,4)# Goto *JTab[x] switch_eg:... ja.L2 # If unsigned > goto default jmp*.L7(,%eax,4)# Goto *JTab[x] Assembly Code Disassembled Object Code

18 IA32 Object Code (cont.) Jump Table  Doesn’t show up in disassembled code  Can inspect using GDB gdb switch (gdb) x/7xw 0x  Examine 7 hexadecimal format “words” (4-bytes each)  Use command “ help x ” to get format documentation 0x :0x x x b0x x :0x x b0x b

19 IA32 Object Code (cont.) Deciphering Jump Table 0x :0x x x b0x x :0x x b0x b AddressValuex 0x x x x x x804843b 2 0x804866c0x x x x x804844b 5 0x x804844b 6

20 Disassembled Targets :b mov $0x2,%eax :eb 2a jmp :b mov $0x1,%eax e:66 90 xchg %ax,%ax # noop :eb 14 jmp :8b mov 0x10(%ebp),%eax :0f af 45 0c imul 0xc(%ebp),%eax :eb 18 jmp b:8b 55 0c mov 0xc(%ebp),%edx e:89 d0 mov %edx,%eax :c1 fa 1f sar $0x1f,%edx :f7 7d 10 idivl 0x10(%ebp) : add 0x10(%ebp),%eax :eb 08 jmp b:b mov $0x1,%eax :2b sub 0x10(%ebp),%eax :5d pop %ebp :c3 ret :b mov $0x2,%eax :eb 2a jmp :b mov $0x1,%eax e:66 90 xchg %ax,%ax # noop :eb 14 jmp :8b mov 0x10(%ebp),%eax :0f af 45 0c imul 0xc(%ebp),%eax :eb 18 jmp b:8b 55 0c mov 0xc(%ebp),%edx e:89 d0 mov %edx,%eax :c1 fa 1f sar $0x1f,%edx :f7 7d 10 idivl 0x10(%ebp) : add 0x10(%ebp),%eax :eb 08 jmp b:b mov $0x1,%eax :2b sub 0x10(%ebp),%eax :5d pop %ebp :c3 ret

21 Matching Disassembled Targets :mov $0x2,%eax :jmp :mov $0x1,%eax e:xchg %ax,%ax :jmp :mov 0x10(%ebp),%eax :imul 0xc(%ebp),%eax :jmp b:mov 0xc(%ebp),%edx e:mov %edx,%eax :sar $0x1f,%edx :idivl 0x10(%ebp) :add 0x10(%ebp),%eax :jmp b:mov $0x1,%eax :sub 0x10(%ebp),%eax :pop %ebp :ret :mov $0x2,%eax :jmp :mov $0x1,%eax e:xchg %ax,%ax :jmp :mov 0x10(%ebp),%eax :imul 0xc(%ebp),%eax :jmp b:mov 0xc(%ebp),%edx e:mov %edx,%eax :sar $0x1f,%edx :idivl 0x10(%ebp) :add 0x10(%ebp),%eax :jmp b:mov $0x1,%eax :sub 0x10(%ebp),%eax :pop %ebp :ret Value 0x x x804843b 0x x x804844b

22 Sparse Switch Example  Not practical to use jump table  Would require 1000 entries  Obvious translation into if- then-else would have max. of 9 tests /* Return x/111 if x is multiple && <= otherwise */ int div111(int x) { switch(x) { case 0: return 0; case 111: return 1; case 222: return 2; case 333: return 3; case 444: return 4; case 555: return 5; case 666: return 6; case 777: return 7; case 888: return 8; case 999: return 9; default: return -1; }

23 Sparse Switch Code  Compares x to possible case values  Jumps different places depending on outcomes movl 8(%ebp),%eax# get x cmpl $444,%eax# x:444 je L8 jg L16 cmpl $111,%eax# x:111 je L5 jg L17 testl %eax,%eax# x:0 je L4 jmp L14... L5: movl $1,%eax jmp L19 L6: movl $2,%eax jmp L19 L7: movl $3,%eax jmp L19 L8: movl $4,%eax jmp L19...

24 Summarizing C Control  if-then-else  do-while  while, for  switch Assembler Control  Conditional jump  Conditional move  Indirect jump  Compiler generates code sequence to implement more complex control Standard Techniques  Loops converted to do-while form  Large switch statements use jump tables  Sparse switch statements may use decision trees