Skip to content

Commit f0b862d

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

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/inline-assembly.md

+29-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,35 @@ 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+
#![feature(asm)]
21+
// Multiply x by 6 using shifts and adds
22+
let mut x: u64 = 4;
23+
unsafe {
24+
asm!(
25+
"mov {tmp}, {x}",
26+
"shl {tmp}, 1",
27+
"shl {x}, 2",
28+
"add {x}, {tmp}",
29+
x = inout(reg) x,
30+
tmp = out(reg) _,
31+
);
32+
}
33+
assert_eq!(x, 4 * 6);
34+
```
35+
36+
## Syntax
37+
938
The following ABNF specifies the general syntax:
1039

1140
```text
@@ -21,13 +50,6 @@ asm := "asm!(" format_string *("," format_string) *("," [ident "="] operand) *("
2150
global_asm := "global_asm!(" format_string *("," format_string) *("," [ident "="] operand) *("," options) [","] ")"
2251
```
2352

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.
3153

3254
## Scope
3355

0 commit comments

Comments
 (0)