-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathasm.c
221 lines (209 loc) · 5.49 KB
/
asm.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "common.h"
int main(void) {
#if defined(__i386__) || defined(__x86_64__)
puts("__i386__ || __x86_64__");
/* # Basic asm vs extended asm
*
* There are two types of asm: basic and extended.
*
* The basic one does not have a colon after the string.
*
* Basic is strictly less powerful: it can only deal with literal commands,
* so you basically (badum tish) never want to use it.
*
* All other examples in this section are extended asm.
*
* https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Extended-Asm.html
*/
{
#ifdef __i386__
__asm__ ("push %eax; mov $1, %eax; pop %eax;");
#else
__asm__ ("push %rax; mov $1, %rax; pop %rax;");
#endif
}
/* # m constraint
*
* Memory.
*
* Instructs GCC to keep value of given expressions into RAM.
*
* This is the most basic way to get/set values of C variables in assembly code.
*/
{
/* OK */
{
uint32_t in = 1;
uint32_t out = 0;
__asm__ (
"movl %1, %%eax;"
"inc %%eax;"
"movl %%eax, %0"
: "=m" (out) /* Outputs. '=' means written to. */
: "m" (in) /* Inputs. No '='. */
: "%eax" /* Clobbers: tell GCC that eax has been is modified. */
);
assert(out == in + 1);
}
#if 0
/* ERROR: memory input 1 is not directly addressable */
{
uint32_t out = 0;
__asm__ (
"movl %1, %%eax;"
"inc %%eax;"
"movl %%eax, %0"
: "=m" (out)
: "m" (1)
: "%eax"
);
assert(out == 1 + 1);
}
#endif
}
/* Multiple inputs. */
{
uint32_t in0 = 1;
uint32_t in1 = 2;
uint32_t out = 0;
__asm__ (
"movl %1, %%eax;"
"movl %2, %%ebx;"
"addl %%ebx, %%eax;"
"movl %%eax, %0"
: "=m" (out)
: "m" (in0),
"m" (in1)
: "%eax"
);
assert(out == in0 + in1);
}
/* The input memory can be the same as the output memory.
*
* io++
*
* We must mark it as `+` which means that the memory is used for both read and write.
*/
{
uint32_t io = 0;
__asm__ (
"movl %0, %%eax;"
"inc %%eax;"
"movl %%eax, %0;"
: "+m" (io) /* + means both read and written to. */
: /* No input. */
: "%eax"
);
assert(io == 1);
}
/* Float example. */
{
float in = 1.0;
float out = 0.0;
/* out = -in */
__asm__ (
"flds %1;"
"fchs;"
"fstps %0;"
: "=m" (out)
: "m" (in)
);
assert(out == -1.0);
}
/* # Register constraints
*
* https://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html
*
* Tell GCC to automatically read memory into registers or write registers into memory
*
* This is more precise and complicated than using `m`:
*
* - r: gcc chooses any free register
* - a: %eax
* - b: %ebx
* - c: %ecx
* - d: %edx
* - S: %esi
* - D: %edi
* - 0: matching register
*/
/* # r register constraint
*
* GCC will automatically put the value of `in` from RAM into a register for us
* and `out` from a register into ram at the end
*
* GCC just makes sure they are written from/to memory before/after the operations.
*
* This is great, as it:
*
* - makes our assembly shorter: no memory moves nor clobbers are needed
* - allows GCC to optimize further
*/
{
const uint32_t in0 = 0;
uint32_t in = in0;
uint32_t out = 0;
__asm__ (
"incl %1;"
"movl %1, %0;"
"incl %0;"
: "=r" (out)
: "r" (in)
);
assert(in == in0);
assert(out == in0 + 2);
}
/* # 0 matching constraint
*
* Specifies that an input maps to the same as a given output.
*
* https://stackoverflow.com/questions/48381184/can-i-modify-input-operands-in-gcc-inline-assembly/48381252#48381252
*
* - vs '+': allows biding two different variables
* - vs 'a': allows referring to an 'r', which is automatically allocated by GCC
*/
{
const uint32_t in0 = 1;
uint32_t in = in0;
uint32_t out = 0;
__asm__ (
"incl %0"
: "=r" (out)
: "0" (in)
);
assert(in == in0);
assert(out == in0 + 1);
}
/* # a register constraint
*
* Forces it to be put into eax.
*
* Clobber done automatically for us.
*
* Just use 'r' whenever you can.
*/
{
uint32_t x = 0;
__asm__ (
"incl %%eax"
: "=a" (x)
: "a" (x)
);
assert(x == 1);
}
/* # Register variables
*
* http://stackoverflow.com/questions/2114163/reading-a-register-value-into-a-c-variable
*
* https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Explicit-Reg-Vars.html
*/
{
register uint32_t eax __asm__ ("eax");
__asm__ ("mov $1, %%eax;" : : : "%eax");
assert(eax == 1);
__asm__ ("mov $2, %%eax;" : : : "%eax");
assert(eax == 2);
}
#endif
return EXIT_SUCCESS;
}