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

For a C function hw2_switch with a general structure shown later, gcc generates

ID: 3929374 • Letter: F

Question

For a C function hw2_switch with a general structure shown later, gcc generates assembly code including a jump table, shown as follows.

hw2_switch:
cmpq $7, %rdx
ja .L2
jmp *.L4(,%rdx,8)
.L4:
.quad .L2
.quad .L3
.quad .L5
.quad .L2
.quad .L6
.quad .L2
.quad .L7
.quad .L5
.L3:
leaq (%rsi,%rsi,2), %rax
leaq (%rax,%rax), %rsi
addq (%rdi), %rsi
jmp .L8
.L5:
leaq (%rsi,%rsi,2), %rax
leaq (%rax,%rax), %rsi
salq $3, %rsi
jmp .L8
.L6:
leaq 20(%rsi), %rax
movq %rax, (%rdi)
.L7:
addq %rsi, %rsi
addq (%rdi), %rsi
jmp .L8
.L2:
addq $5, %rsi
.L8:
movq %rsi, (%rdi)
ret

Based on the assembly code above, fill in the blanks below in its corresponding C source code. You may only use the source-level C variables x, m, result, and value: don’t use register names!.

void hw2_switch (long *value, long x, long m)
{
long result;
switch (m) {
case _______: /* Case A */
result = ________________________;
break;
case _______: /* Case B */
case _______: /* Case C */
result = ________________________;
break;
case _______: /* Case D */
*value = ______________________;
/* fall through */
case _______: /* Case E */
result = ____________________;
break;
default:
result = ___________________;
}
*value = result;

Explanation / Answer

void hw2_switch (long *value, long x, long m)

{

long result;

switch (m) {

case -1

result = L9;

break;

case 0 , 7:

case 1:

result = L5,L6;

break;

case 2,4 :

*value =8;

/* fall through */

case 5:

result = L8;

break;

case 3,6:

default:

result = L2;

}

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