Docsity

Machine-Level Programming III Switching Statement And IA32 Procedures, Lecture Slide - Computer Science, Slides of Computer System Design and Architecture

Partial preview of the text

Download Machine-Level Programming III Switching Statement And IA32 Procedures, Lecture Slide - Computer Science and more Slides Computer System Design and Architecture in PDF only on Docsity!

Instructors:

Randy Bryant and Dave O’Hallaron

Machine-­‐Level Programming III:

Switch Statements and IA32 Procedures

15-­‐213: Introduc;on to Computer Systems

th

Lecture, Sep. 9, 2010

Today

 Switch statements

 IA 32 Procedures

 Stack Structure  Calling Conven;ons  Illustra;ons of Recursion & Pointers

Jump Table Structure

Code Block 0

Targ0:

Code Block 1

Targ1:

Code Block 2

Targ2:

Code Block n–

Targn-1:

Targ Targ Targ Targn-

jtab:

target = JTab[x]; goto *target; switch(x) { case val_0: Block 0 case val_1: Block 1

  • • • case val_n-1: Block n– }

Switch Form

Approximate Transla;on

Jump Table Jump Targets

Switch Statement Example (IA32)

Setup:

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 movl 8(%ebp), %eax # %eax = x cmpl $6, %eax # Compare x: ja .L2 # If unsigned > goto default jmp *.L7(,%eax,4) # Goto *JTab[x]

What range of

values takes

default?

Note that w not

ini;alized here

Assembly Setup ExplanaBon

 Table Structure

 Each target requires 4 bytes  Base address at .L

 Jumping

 Direct: jmp .L  Jump target is denoted by label .L  Indirect: jmp .L7(,%eax,4)  Start of jump table: .L  Must scale by factor of 4 (labels have 32-­‐bits = 4 Bytes on IA32)  Fetch target from effec;ve Address .L7 + eax

 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

Jump Table

Jump table

switch(x) { case 1: // .L w = yz; break; case 2: // .L w = y/z; / Fall Through */ case 3: // .L w += z; break; case 5: case 6: // .L w -= z; break; default: // .L w = 2; }

Code Blocks (ParBal)

.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 movl 16(%ebp), %eax # z imull 12(%ebp), %eax # w = yz jmp .L8 # Goto done switch(x) { case 1: // .L w = yz; break;

... case 3: // .L w += z; break; ... default: // .L w = 2; }

Code Blocks (Rest)

.L4: # x == 2 movl 12(%ebp), %edx movl %edx, %eax sarl $31, %edx idivl 16(%ebp) # w = y/z .L9: # merge: addl 16(%ebp), %eax # w += z jmp .L8 # goto done .L6: # x == 5, 6 movl $1, %eax # w = 1 subl 16(%ebp), %eax # w = 1-z switch(x) {

... case 2: // .L w = y/z; /* Fall Through */ merge: // .L w += z; break; case 5: case 6: // .L w -= z; break; }

x86-­‐64 Switch ImplementaBon

.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 switch(x) { case 1: // .L w = y*z; break;

... }

IA32 Object Code

 Setup

 Label .L2 becomes address 0x  Label .L7 becomes address 0x 08048410 <switch_eg>:

... 8048419: 77 07 ja 8048422 <switch_eg+0x12> 804841b: ff 24 85 60 86 04 08 jmp *0x8048660(,%eax,4) switch_eg: ... ja .L2 # If unsigned > goto default jmp *.L7(,%eax,4) # Goto *JTab[x]

Assembly Code

Disassembled Object Code

IA32 Object Code (cont.)

 Deciphering Jump Table

0x8048660: 0x08048422 0x08048432 0x0804843b 0x 0x8048670: 0x08048422 0x0804844b 0x0804844b Address Value x 0x8048660 0x8048422 0 0x8048664 0x8048432 1 0x8048668 0x804843b 2 0x804866c 0x8048429 3 0x8048670 0x8048422 4 0x8048674 0x804844b 5 0x8048678 0x804844b 6

Disassembled Targets

8048422: b8 02 00 00 00 mov $0x2,%eax 8048427: eb 2a jmp 8048453 <switch_eg+0x43> 8048429: b8 01 00 00 00 mov $0x1,%eax 804842e: 66 90 xchg %ax,%ax # noop 8048430: eb 14 jmp 8048446 <switch_eg+0x36> 8048432: 8b 45 10 mov 0x10(%ebp),%eax 8048435: 0f af 45 0c imul 0xc(%ebp),%eax 8048439: eb 18 jmp 8048453 <switch_eg+0x43> 804843b: 8b 55 0c mov 0xc(%ebp),%edx 804843e: 89 d0 mov %edx,%eax 8048440: c1 fa 1f sar $0x1f,%edx 8048443: f7 7d 10 idivl 0x10(%ebp) 8048446: 03 45 10 add 0x10(%ebp),%eax 8048449: eb 08 jmp 8048453 <switch_eg+0x43> 804844b: b8 01 00 00 00 mov $0x1,%eax 8048450: 2b 45 10 sub 0x10(%ebp),%eax 8048453: 5d pop %ebp 8048454: c3 ret

Summarizing

 C Control

 if-­‐then-­‐else  do-­‐while  while, for  switch

 Assembler Control

 Condi;onal jump  Condi;onal 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

Today

 Switch statements

 IA 32 Procedures

 Stack Structure  Calling Conven;ons  Illustra;ons of Recursion & Pointers