Skip to content

Commit 2c969f4

Browse files
committed
Add example of inline assembly
1 parent 480f6d4 commit 2c969f4

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/inline-assembly.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,34 @@ It can be used to embed handwritten assembly in the assembly output generated by
66
[`asm!`]: ../core/arch/macro.asm.html
77
[`global_asm!`]: ../core/arch/macro.global_asm.html
88

9+
Support for inline assembly is stable on the following architectures:
10+
- x86 and x86-64
11+
- ARM
12+
- AArch64
13+
- RISC-V
14+
15+
The compiler will emit an error if `asm!` is used on an unsupported target.
16+
17+
## Example
18+
19+
```rust
20+
// Multiply x by 6 using shifts and adds
21+
let mut x: u64 = 4;
22+
unsafe {
23+
asm!(
24+
"mov {tmp}, {x}",
25+
"shl {tmp}, 1",
26+
"shl {x}, 2",
27+
"add {x}, {tmp}",
28+
x = inout(reg) x,
29+
tmp = out(reg) _,
30+
);
31+
}
32+
assert_eq!(x, 4 * 6);
33+
```
34+
35+
## Syntax
36+
937
The following ABNF specifies the general syntax:
1038

1139
```text
@@ -21,13 +49,6 @@ asm := "asm!(" format_string *("," format_string) *("," [ident "="] operand) *("
2149
global_asm := "global_asm!(" format_string *("," format_string) *("," [ident "="] operand) *("," options) [","] ")"
2250
```
2351

24-
Support for inline assembly is stable on the following architectures:
25-
- x86 and x86-64
26-
- ARM
27-
- AArch64
28-
- RISC-V
29-
30-
The compiler will emit an error if `asm!` is used on an unsupported target.
3152

3253
## Scope
3354

0 commit comments

Comments
 (0)