summaryrefslogtreecommitdiffstats
path: root/md/writeup/gcc_inline_assembly.md
blob: 808268c53669da7bdd5adabc589ed33c13b277cf (plain) (blame)
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
title: GCC inline assembly

# GCC inline assembly

Here is attempt to make best notes about gcc inline asm. As this is just small
operator that is for long time, i dont thing there is some full guide how to use
it lets do it here for wisdom of internet.

## Inline assembly syntax

```c
asm [volatile] ( 
        AssemblerTemplate
        : OutputOperands
        [ : InputOperands
        [ : Clobbers ] ]
)
```

### Output operands

| Option | Note |
|---|---|
| "=r" | Write output to register |

### Input operands

| Option | Note |
|---|---|
| "r" | Input to register |

### Clobbers 

Clobbers used to tell GCC that registers are used. 

| Clobber option | Note |
|---|---|
| "0" | map to register "0" same register |

### Modifiers

|Modifier|Specifies|
|---|---|
| = | Write-only operand, usually used for all output operands |
| + | Read-write operand, must be listed as an output operand |
| & | A register that should be used for output only |

### Table of register names

Table of AMD64 register names

```text
+---+--------------------+  
| r |    Register(s)     |  
+---+--------------------+  
| a |   %eax, %ax, %al   |  
| b |   %ebx, %bx, %bl   |  
| c |   %ecx, %cx, %cl   |  
| d |   %edx, %dx, %dl   |  
| S |   %esi, %si        |  
| D |   %edi, %di        |  
+---+--------------------+  
```

|Register|Name|
|---|---|
| rax | %%rax |
| rbx | %%rbx |
| rcx | %%rcx |
| rdx | %%rdx |
| rdi | %%rdi |
| rsi | %%rsi |
| rbp | %%rbp |
| rsp | %%rsp |
| r8  | %%r8  |
| r9  | %%r9  |
| r10 | %%r10 |
| r11 | %%r11 |
| r12 | %%r12 |
| r13 | %%r13 |
| r14 | %%r14 |
| r15 | %%r15 |

## Syscall calling convention

<!--### Intel i386 Linux-->

### Intel AMD64 Linux

| Param | Register |
| --- | --- | 
| 1th            | __rdi__ |
| 2th            | __rsi__ |
| 3th            | __rdx__ |
| 4th            | __rcx__ |
| 5th            | __r8__  |
| 6th            | __r9__  |
| syscall number | __rax__ |

## Example

### AMD64 Add two numbers

```c
int32_t a=1,b=2,c=-1;
asm(	
	"movl %1, %0\n\t"
	"addl %2, %0\n\t"
	:"=r"(c)
	:"r"(a),"r"(b)
	:"0");
```
__a__,__b__ - use regisers and save result __c__ to register, 
make to use for __c__ same register by mentioning "0" in clobber register

__Output__
```text
	movl %edx, %edx
	addl %ecx, %edx
```

```c
int32_t a=1,b=2,c=-1;
asm(	
	"movl %1, %0\n\t"
	"addl %2, %0\n\t"
	:"=r"(c)
	:"g"(a),"g"(b)
	:"0");
```

__Output__
```text
	movl -4(%rbp), %edx
	addl -8(%rbp), %edx
```

### AMD64 Call linux syscall mmap with inline asm

```c
long sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, unsigned long fd, unsigned long off)
{
	long ret;
	asm(
		"mov %p6, %%r9\n\t"
		"mov %p5, %%r8\n\t"
		"mov %p4, %%r10\n\t"
		"mov %p3, %%rdx\n\t"
		"mov %p2, %%rsi\n\t"
		"mov %p1, %%rdi\n\t"
		"mov $9, %%rax\n\t"
		"syscall\n\t"
		:"=a"(ret)
		:[p1]"m"(addr),[p2]"m"(len),[p3]"m"(prot),[p4]"m"(flags),[p5]"m"(fd),[p6]"m"(off));
	return ret;
}
```
Put result of execution to __ret__, all paramters in memory

### Intel random number with RDRAND

```c
uint64_t get_hw_rand()
{
	uint64_t ret;
	int i=0;
	const int timeout = 10;
	while (i<timeout)
	{
		asm("rdrand %0\n\t"
			:"=a"(ret)::);
		if (ret) break;
		i++;
	}
	return ret;
}
```

<!--

### Intel i386 BSD
### Intel AMD64 BSD
### ARM
### MIPS

-->

## Switching on of intel/att syntax

Inline assembler for GCC by default uses AT&T syntax. There is possible to turn on/off intel syntax.

```c
asm(".intel_syntax noprefix");
asm("mov eax, 1");
asm(".att_syntax prefix");
```

## Asm goto



## Links

1. https://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
2. https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
3. https://en.wikipedia.org/wiki/Inline_assembler
4. http://www.ethernut.de/en/documents/arm-inline-asm.html
5. http://wiki.osdev.org/Inline_Assembly
6. http://asm.sourceforge.net/articles/rmiyagi-inline-asm.txt
7. http://wiki.osdev.org/Inline_Assembly/Examples
8. http://wiki.osdev.org/Calling_Conventions