Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.

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.
University of Washington Announcements Lab 1 due last night Lab 2 release soon HW1 due Monday night 1.
Carnegie Mellon Instructors: Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures : Introduction.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
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.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Y86 Processor State Program Registers
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:
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.
Machine-Level Programming V: Switch Statements Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified slides from.
II:1 x86 Assembly - Control. II:2 Alternate reference source Go to the source: Intel 64 and IA32 
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
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.
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.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
MACHINE-LEVEL PROGRAMMING III: SWITCH STATEMENTS AND IA32 PROCEDURES.
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.
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
X86 Assembly, and C-to-assembly
Conditional Branch Example
Recitation 2 – 2/4/01 Outline Machine Model
Assembly Language Programming V: In-line Assembly Code
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.
Chapter 3 Machine-Level Representation of Programs
Machine-Level Representation of Programs II
Machine-Level Programming II: Control Flow
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 4 Procedures
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming II: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Today Control flow if/while/do while/for/switch
Machine-Level Programming 2 Control Flow
Machine-Level Programming: Introduction
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
X86 Assembly - Control.
Machine-Level Programming II: Control Flow Sept. 12, 2007
CS201- Lecture 8 IA32 Flow Control
Presentation transcript:

Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables

– 2 – CMSC 313, F’09 Switch Statements Implementation Options Jump Table Lookup branch target Constant time Possible when cases are small integer constants Series of conditionals Organize in tree structure Logarithmic performance GCC Picks one based on case structure

– 3 – CMSC 313, F’09 Switch Statement Example Features Multiple case labels Fall through cases Missing cases 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 – CMSC 313, F’09 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; switch(x) { case val_0: Block 0 case val_1: Block 1 case val_n-1: Block n–1 } Switch Form Approx. Translation Jump Table Jump Targets

– 5 – CMSC 313, F’09 Switch Statement Example (IA32) Setup: switch_eg: pushl %ebp# Setup movl %esp, %ebp# Setup pushl %ebx# Setup movl $1, %ebx# w = 1 movl 8(%ebp), %edx# edx = x movl 16(%ebp), %ecx# ecx = z cmpl $6, %edx# x:6 ja.L61# if > goto default jmp *.L62(,%edx,4)# goto JTab[x] long switch_eg (long x, long y, long z) { long w = 1; switch(x) {... } return w; }

– 6 – CMSC 313, F’09 Assembly Setup Explanation Table Structure Each target requires 4 bytes Table base address at.L62 Jumping Direct Jump jmp.L61 Jump target is denoted by label.L61 Indirect Jump jmp *.L62(,%edx,4) Start of jump table denoted by label. L62 Register %edx holds x Must scale by factor of 4 to get offset into table Fetch target from effective Address. L61 + x*4 Only for 0  x  6

– 7 – CMSC 313, F’09 Jump Table.section.rodata.align 4.L62:.long.L61 # x = 0.long.L56 # x = 1.long.L57 # x = 2.long.L58 # x = 3.long.L61 # x = 4.long.L60 # x = 5.long.L60 # x = 6 Table Contents switch(x) { case 1: //.L56 w = y*z; break; case 2: //.L57 w = y/z; /* Fall Through */ case 3: //.L58 w += z; break; case 5: case 6: //.L60 w -= z; break; default: //.L61 w = 2; }

– 8 – CMSC 313, F’09 Code Blocks (Partial).L61: // Default case movl $2, %ebx # w = 2 movl %ebx, %eax # Return w popl %ebx leave ret.L57: // Case 2: movl 12(%ebp), %eax # y cltd # Div prep idivl %ecx # y/z movl %eax, %ebx # w = y/z # Fall through.L58: // Case 3: addl %ecx, %ebx # w+= z movl %ebx, %eax # Return w popl %ebx leave ret switch(x) {... case 2: //.L57 w = y/z; /* Fall Through */ case 3: //.L58 w += z; break;... default: //.L61 w = 2; }

– 9 – CMSC 313, F’09 Code Blocks (Rest).L60: // Cases 5&6: subl %ecx, %ebx # w –= z movl %ebx, %eax # Return w popl %ebx leave ret.L56: // Case 1: movl 12(%ebp), %ebx # w = y imull %ecx, %ebx # w*= z movl %ebx, %eax # Return w popl %ebx leave ret switch(x) { case 1: //.L56 w = y*z; break;... case 5: case 6: //.L60 w -= z; break;... }

– 10 – CMSC 313, F’09 IA32 Object Code Context Label.L61 becomes address 0x Label.L62 becomes address 0x80488dc : : 77 0c ja : ff dc jmp *0x80488dc(,%edx,4) switch_eg:... ja.L61# if > goto default jmp *.L62(,%edx,4)# goto JTab[x] Assembly Code Disassembled Object Code

– 11 – CMSC 313, F’09 IA32 Object Code (cont.) Jump Table Doesn’t show up in disassembled code Can inspect using GDB gdb asm-cntl (gdb) x/7xw 0x80488dc Examine 7 hexadecimal format “words” (4-bytes each) Use command “ help x ” to get format documentation 0x80488dc: 0x x x a 0x x x

– 12 – CMSC 313, F’09 Disassembled Targets : bb mov $0x2,%ebx : 89 d8 mov %ebx,%eax : 5b pop %ebx : c9 leave : c3 ret a: 8b 45 0c mov 0xc(%ebp),%eax d: 99 cltd e: f7 f9 idiv %ecx : 89 c3 mov %eax,%ebx : 01 cb add %ecx,%ebx : 89 d8 mov %ebx,%eax : 5b pop %ebx : c9 leave : c3 ret : 29 cb sub %ecx,%ebx b: 89 d8 mov %ebx,%eax d: 5b pop %ebx e: c9 leave f: c3 ret : 8b 5d 0c mov 0xc(%ebp),%ebx : 0f af d9 imul %ecx,%ebx : 89 d8 mov %ebx,%eax : 5b pop %ebx : c9 leave a: c3 ret

– 13 – CMSC 313, F’09 Matching Disassembled Targets : bb mov : 89 d8 mov : 5b pop : c9 leave : c3 ret a: 8b 45 0c mov d: 99 cltd e: f7 f9 idiv : 89 c3 mov : 01 cb add : 89 d8 mov : 5b pop : c9 leave : c3 ret : 29 cb sub b: 89 d8 mov d: 5b pop e: c9 leave f: c3 ret : 8b 5d 0c mov : 0f af d9 imul : 89 d8 mov : 5b pop : c9 leave a: c3 ret 0x x x a 0x x x

– 14 – CMSC 313, F’09 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; }

– 15 – CMSC 313, F’09 Sparse Switch Code (IA32) 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...

– 16 – CMSC 313, F’09 Sparse Switch Code Structure Organizes cases as binary tree Logarithmic performance <  = > = <> = <> = <> =  =  =  =  =  =

– 17 – CMSC 313, F’09 Summarizing C Control if-then-else do-while while, for switch Assembler Control Conditional jump Conditional move Indirect jump Compiler Must generate assembly code to implement more complex control Standard Techniques IA32 loops converted to do-while form Large switch statements use jump tables Conditions in CISC CISC machines generally have condition code registers

– 18 – CMSC 313, F’09 Quiz Time Write the C that produced the following assembly language code This function takes a single integer argument located at 0x8(%ebp) : 0: 55 push %ebp 1: 31 c0 xor %eax,%eax 3: 89 e5 mov %esp,%ebp 5: 8b 4d 08 mov 0x8(%ebp),%ecx 8: 85 c9 test %ecx,%ecx a: 7e 0c jle 18 c: 31 d2 xor %edx,%edx e: 89 f6 mov %esi,%esi 10: 8d lea (%eax,%edx,2),%eax 13: 42 inc %edx 14: 39 d1 cmp %edx,%ecx 16: 75 f8 jne 10 18: 5d pop %ebp 19: c3 ret

– 19 – CMSC 313, F’09 Quiz Time Write the C that produced the following assembly language code This function takes a single integer argument located at 0x8(%ebp) : 0: 55 push %ebp 1: 31 c0 xor %eax,%eax 3: 89 e5 mov %esp,%ebp 5: 8b 4d 08 mov 0x8(%ebp),%ecx 8: 85 c9 test %ecx,%ecx a: 7e 0c jle 18 c: 31 d2 xor %edx,%edx e: 89 f6 mov %esi,%esi 10: 8d lea (%eax,%edx,2),%eax 13: 42 inc %edx 14: 39 d1 cmp %edx,%ecx 16: 75 f8 jne 10 18: 5d pop %ebp 19: c3 ret