For a C function hw2switch with a general structure shown la

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;

Solution

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;

}

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, %rd
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, %rd

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site