Complete the following C code that corresponds to the following assembly code: C
ID: 3678874 • Letter: C
Question
Complete the following C code that corresponds to the following assembly code:
C code:
long swich3(long *p1, long p*2, mode_t action){
long result = 0;
switch(action){
case MODE_A:
case MODE_B:
case MODE_C:
case MODE_D:
case MODE_E:
default:
}
return result;
}
assembly code:
/* note that p1 in %rdi, p2 in %rsi, action in %edx */
.L8:
/*MODE_E */
movl $27, %eax
ret
.L3
/* MODE_A */
movq (%rsi), %rax
movq (%rdi), %rdx
movq %rdx, (%rsi)
.L5:
/* MODE_B */
movq (%rdi), %rax
addq (%rsi), %rax
movq %rax, (%rdi)
ret
.L6
/* MODE_C */
movq $59, (%rdi)
movq (%rsi), %rax
ret
.L7:
/* MODE_D */
movq (%rsi), %rax
movq %rax, (%rdi)
movl $27, %eax
ret
.L9:
/*deault*/
movl $12, %eax
ret
Explanation / Answer
***Try this c code corresponding the assembly code***
typedef enum
{
MODE_A,
MODE_B,
MODE_C,
MODE_D,
MODE_E
}
mode_t;
int switch3(int *p1, int *p2, mode_t action)
{
int result = 0;
switch(action)
{
case MODE_A:
result = *p1;
*p1 = *p2;
break;
case MODE_B:
*p2 += *p1;
result = *p2;
break;
case MODE_C:
*p2 = 15;
result = *p1;
break;
case MODE_D:
*p2 = *p1;
/* Fall Through */
case MODE_E:
result = 17;
break;
default:Printf("Invalid option");
result = -1;
}
return result;
}
Hope this will help to you!!!
Thank you!!!!