Question
Find the C code for the switch statement This problem will give you a chance to reverse engineer a switch statement from machine code. In
Find 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 0x040059e that the jump table begins at address 0x0400698. Using the gdb debugger, we can examine the ten 8-byte words of memory comprising the jump table with the command x/10x 0x400698. gdb prints the following:
0x400698: 0x00000000004005ad 0x00000000004005c3 0x4006a8: 0x00000000004005c3 0x00000000004005c3 0x4006b8: 0x00000000004005a8 0x00000000004005be 0x4006c8: 0x00000000004005a8 0x00000000004005b7 0x000000000040057c <+0>: push %rbp 0x000000000040057d <+1>: mov %rsp,%rbp 0x0000000000400580 <+4>: mov %rdi,-0x18(%rbp) 0x0000000000400584 <+8>: mov %rsi,-0x20(%rbp) 0x0000000000400588 <+12>: mov -0x20(%rbp),%rax 0x000000000040058c <+16>: mov %rax,-0x8(%rbp) 0x0000000000400590 <+20>: mov -0x18(%rbp),%rax 0x0000000000400594 <+24>: sub $0x52,%rax 0x0000000000400598 <+28>: cmp $0x7,%rax 0x000000000040059c <+32>: ja 0x4005c30x000000000040059e <+34>: mov 0x400698(,%rax,8),%rax 0x00000000004005a6 <+42>: jmpq *%rax 0x00000000004005a8 <+44>: subq $0xa,-0x8(%rbp) 0x00000000004005ad <+49>: movq $0x3e,-0x8(%rbp) 0x00000000004005b5 <+57>: jmp 0x4005cb 0x00000000004005b7 <+59>: sarq $0x4,-0x8(%rbp) 0x00000000004005bc <+64>: jmp 0x4005cb 0x00000000004005be <+66>: subq $0x1,-0x8(%rbp) 0x00000000004005c3 <+71>: movq $0x3b,-0x8(%rbp) 0x00000000004005cb <+79>: mov -0x8(%rbp),%rax 0x00000000004005cf <+83>: pop %rbp 0x00000000004005d0 <+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