Skip to content

Commit

Permalink
Add example of inline assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu committed Dec 2, 2021
1 parent 480f6d4 commit 2c969f4
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/inline-assembly.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ It can be used to embed handwritten assembly in the assembly output generated by
[`asm!`]: ../core/arch/macro.asm.html
[`global_asm!`]: ../core/arch/macro.global_asm.html

Support for inline assembly is stable on the following architectures:
- x86 and x86-64
- ARM
- AArch64
- RISC-V

The compiler will emit an error if `asm!` is used on an unsupported target.

## Example

```rust
// Multiply x by 6 using shifts and adds
let mut x: u64 = 4;
unsafe {
asm!(
"mov {tmp}, {x}",
"shl {tmp}, 1",
"shl {x}, 2",
"add {x}, {tmp}",
x = inout(reg) x,
tmp = out(reg) _,
);
}
assert_eq!(x, 4 * 6);
```

## Syntax

The following ABNF specifies the general syntax:

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

Support for inline assembly is stable on the following architectures:
- x86 and x86-64
- ARM
- AArch64
- RISC-V

The compiler will emit an error if `asm!` is used on an unsupported target.

## Scope

Expand Down

0 comments on commit 2c969f4

Please sign in to comment.