Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This problem involves reverse engineering of a switch statement from assembly co

ID: 3601229 • Letter: T

Question

This problem involves reverse engineering of a switch statement from assembly code. (It could also be done from disassembled object code with a bit more work, including using the GDB debugger to inspect the content of the jump table.) In the following procedure, the body of the suitch statement has been removed: long svitch prob(long x, long n) long result x svitch(n) / Fill in code here / return result; Following is the assembly code for the procedure (compiled with -01). Remember that param eters x and n will be passed in registers %rdiand %rsi, respectively. file "switch prob2-soln.c'" text globl svitch prob type witch-prob, function svitch_prob: LFBO: cfi_startproc subq $60, %rsi cnpq $5, %rsi ja .L2 section .rodata align 8 align 4 L7: quad .L3 quad .L2 quad .L3 quad L quad L5 quad .L6 text leaq 0(.xrd1,8), ret %rax movq %rdi, %rax sarq $3, %rax ret novq %rdi, '.rax salq $4, %rax subq %rdi, %rax movq %rax, Xrdi inu1q%rdi, %rdi L2: %rax leaq 75(%rdi), ret cfi endproc LFEO: size switch-prob, "-switch-prob ident "GCC: (Ubuntu/Linaro 4.6,3-1ubuntu5) 4.6.3" section .note.GNU-stack,",progbits Fill in the body of the switch statement with C code that will have the same behavior as the assembly code.

Explanation / Answer

switch.c

long switch_prob(long x, long n)
{
long result = x;
switch (n)
{
case 0x3c:
case 0x3e:
result = 0x08 * x;
break;
case 0x3f:
result = result >> 0x03;
break;
case 0x40:
result = result << 4;
x = result - x;
case 0x41:
x = x * x;
default:
result = x + 75;
break;
}
return result;
}

int main() {
long x = switch_prob(241,132);
long y = switch_prob(927,103);
long z = switch_prob(x,y);
return switch_prob(x,z/40);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote