For a function with prototype long decode2long x long y long
For a function with prototype:
long decode2(long x, long y, long z);
GCC generates the following assembly code:
1. decode2:
2.subq %rdx, %rsi
3. imultq %rsi, %rdi
4.movq %rsi, %rax
5. salq $63, %rax
6. sarq $63, %rax
7. xorq %rdi, %rax
8. ret
Parameters x, y, and z are passed in registers %rdi, %rsi, and %rdx. The code store the return value in register %rax.
Write C code for decode2 that will have an effect equivalent to the assembly code shown.
Please explain your reasoning.
Solution
I am just writting the equivalent line :
rsi - y, rdx- z , rdi - x and we are condering constant result to store the result value
subq %rdx, %rsi - this means rsi = rsi-rdx and so equivalent c code is y = y-z
imultq %rsi, %rdi - this means rdi = rdi*rsi so equivalent cdoe is x= x*y
movq %rsi, %rax this mean rax = rsi so equivalent c code is result = x
salq $63, %rax this means rax bits will shift 63 bits to left equivalent result >>> 63
sarq $63, %rax this means rax will shift 63 bits right
xorq %rdi, %rax this means rax= rax^rdi hence result = result ^ x
