Question
Fnd the C code for the switch statement. This problem will give you a chance to reverse engineer a switch statement from machine code. In
Fnd the C code for the switch statement. This problem will give you a chance to reverse engineer a switch statement from machine code. In the following procedure, the body of the switch statement has been removed:
long switch_ prob(long x, long n) { long result = n; switch(x) { /* The code for the cases goes here */ } return result; }
The disassembled machine code for the procedure is shown below. Recall parameter x is passed in register %rdi and parameter n is passed in register %rsi. The jump table resides in a different area of memory. We can see from the mov at address 0x040065e that the jump table begins at address 0x0400798. Using the gdb debugger, we can examine the ten 8-byte words of memory comprising the jump table with the command x/10x 0x400798. gdb prints the following:
(gdb) x/10x 0x400798 0x400798: 0x000000000040066f 0x0000000000400683 0x4007a8: 0x0000000000400683 0x0000000000400683 0x4007b8: 0x0000000000400668 0x000000000040067e 0x4007c8: 0x0000000000400668 0x0000000000400683 0x4007d8: 0x0000000000400679 0x00646c2520646c25 0x000000000040063c <+0>: push %rbp 0x000000000040063d <+1>: mov %rsp,%rbp 0x0000000000400640 <+4>: mov %rdi,-0x18(%rbp) 0x0000000000400644 <+8>: mov %rsi,-0x20(%rbp) 0x0000000000400648 <+12>: mov -0x20(%rbp),%rax 0x000000000040064c <+16>: mov %rax,-0x8(%rbp) 0x0000000000400650 <+20>: mov -0x18(%rbp),%rax 0x0000000000400654 <+24>: sub $0x1f,%rax 0x0000000000400658 <+28>: cmp $0x8,%rax 0x000000000040065c <+32>: ja 0x4006830x000000000040065e <+34>: mov 0x400798(,%rax,8),%rax 0x0000000000400666 <+42>: jmpq *%rax 0x0000000000400668 <+44>: addq $0x2,-0x8(%rbp) 0x000000000040066d <+49>: jmp 0x40068b 0x000000000040066f <+51>: movq $0x16,-0x8(%rbp) 0x0000000000400677 <+59>: jmp 0x40068b 0x0000000000400679 <+61>: shlq $0x2,-0x8(%rbp) 0x000000000040067e <+66>: addq $0x1,-0x8(%rbp) 0x0000000000400683 <+71>: movq $0x19,-0x8(%rbp) 0x000000000040068b <+79>: mov -0x8(%rbp),%rax 0x000000000040068f <+83>: pop %rbp 0x0000000000400690 <+84>: retq
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started