So for one question in my hw, I have this assembly code that ultimately I have t
ID: 3678606 • Letter: S
Question
So for one question in my hw, I have this assembly code that ultimately I have to implement in C.
/*p1 in %rdi, p2 in %rsi, action in %edx
.L8: MODE_E
movl $27, %eax
ret
.L3: MODE_A
movq (%rsi), %rax
movq (%rdi), %rax
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
.L7: MODE_D
movq (%rsi),%rax
movq %rax, (%rdi)
movl $27, %eax
.L9: DEFAULT
movl $12, %eax
ret
Now, for the code in C this is what I ended up with, could someone check if it is right please? and if not, explain why?
typedef enum {MODE_A, MODE_B, MODE_C, MODE_D, MODE_E} mode_t;
long switch3(long *p1, long *p2, mode_t action)
{
long result =0;
switch (action)
{
case MODE_A:
result = *p2;
temp = *p1 //quite unsure about this part of the assembly
*p2 = p1
case MODE_B:
result = *p1
result += p2
*p1 = result
break
case MODE_C:
*p1 = 59
result = *p2
break
case MODE_D:
result = *p2
*p1 = result
result = 27
break
case MODE_E:
result = 27
break
default:
result = 12
break
}
return result;
}
Explanation / Answer
I made little changes in your existing program 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: result = *p1 + *p2; *p2 = result; break; case MODE_C: *p2 = 59; result = *p1; break; case MODE_D: *p2 = *p1; result = 27; break; case MODE_E: result = 27; break; default: result = 12; break; } return result; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.