-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode2.c
39 lines (32 loc) · 966 Bytes
/
decode2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
/**
* gcc generates the following assembly code:
* 1 decode2:
* 2 subq %rdx, %rsi # y = y - z
* 3 imulq %rsi, %rdi # x *= y
* 4 movq %rsi, %rax # ret = y
* 5 salq $63, %rax # ret << 63
* 6 sarq $63, %rax # ret >> 63
* 7 xorq %rdi, %rax # ret ^= x
* 8 ret
* Parameters x, y, and z are passed in registers %rdi, %rsi, and %rdx.
* The code stores the return value in register %rax.
*
*/
long decode2(long x, long y, long z)
{
// Subtraction: subq %rdx, %rsi
y = y - z;
// Multiplication: imulq %rsi, %rdi
x = x * y;
// Store y in %rax: movq %rsi, %rax
long result = y;
// Shift left: salq $63, %rax
result = result << 63;
// Shift right: sarq $63, %rax
result = result >> 63;
// XOR: xorq %rdi, %rax
result = result ^ x;
// Return the final result stored in %rax
return result;
}