|
| 1 | +% Inline Assembly |
| 2 | + |
| 3 | +For extremely low-level manipulations and performance reasons, one |
| 4 | +might wish to control the CPU directly. Rust supports using inline |
| 5 | +assembly to do this via the `asm!` macro. The syntax roughly matches |
| 6 | +that of GCC & Clang: |
| 7 | + |
| 8 | +```ignore |
| 9 | +asm!(assembly template |
| 10 | + : output operands |
| 11 | + : input operands |
| 12 | + : clobbers |
| 13 | + : options |
| 14 | + ); |
| 15 | +``` |
| 16 | + |
| 17 | +Any use of `asm` is feature gated (requires `#![feature(asm)]` on the |
| 18 | +crate to allow) and of course requires an `unsafe` block. |
| 19 | + |
| 20 | +> **Note**: the examples here are given in x86/x86-64 assembly, but |
| 21 | +> all platforms are supported. |
| 22 | +
|
| 23 | +## Assembly template |
| 24 | + |
| 25 | +The `assembly template` is the only required parameter and must be a |
| 26 | +literal string (i.e. `""`) |
| 27 | + |
| 28 | +``` |
| 29 | +#![feature(asm)] |
| 30 | +
|
| 31 | +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 32 | +fn foo() { |
| 33 | + unsafe { |
| 34 | + asm!("NOP"); |
| 35 | + } |
| 36 | +} |
| 37 | +
|
| 38 | +// other platforms |
| 39 | +#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] |
| 40 | +fn foo() { /* ... */ } |
| 41 | +
|
| 42 | +fn main() { |
| 43 | + // ... |
| 44 | + foo(); |
| 45 | + // ... |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +(The `feature(asm)` and `#[cfg]`s are omitted from now on.) |
| 50 | + |
| 51 | +Output operands, input operands, clobbers and options are all optional |
| 52 | +but you must add the right number of `:` if you skip them: |
| 53 | + |
| 54 | +``` |
| 55 | +# #![feature(asm)] |
| 56 | +# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 57 | +# fn main() { unsafe { |
| 58 | +asm!("xor %eax, %eax" |
| 59 | + : |
| 60 | + : |
| 61 | + : "eax" |
| 62 | + ); |
| 63 | +# } } |
| 64 | +``` |
| 65 | + |
| 66 | +Whitespace also doesn't matter: |
| 67 | + |
| 68 | +``` |
| 69 | +# #![feature(asm)] |
| 70 | +# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 71 | +# fn main() { unsafe { |
| 72 | +asm!("xor %eax, %eax" ::: "eax"); |
| 73 | +# } } |
| 74 | +``` |
| 75 | + |
| 76 | +## Operands |
| 77 | + |
| 78 | +Input and output operands follow the same format: `: |
| 79 | +"constraints1"(expr1), "constraints2"(expr2), ..."`. Output operand |
| 80 | +expressions must be mutable lvalues: |
| 81 | + |
| 82 | +``` |
| 83 | +# #![feature(asm)] |
| 84 | +# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 85 | +fn add(a: i32, b: i32) -> i32 { |
| 86 | + let mut c = 0; |
| 87 | + unsafe { |
| 88 | + asm!("add $2, $0" |
| 89 | + : "=r"(c) |
| 90 | + : "0"(a), "r"(b) |
| 91 | + ); |
| 92 | + } |
| 93 | + c |
| 94 | +} |
| 95 | +# #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] |
| 96 | +# fn add(a: i32, b: i32) -> i32 { a + b } |
| 97 | +
|
| 98 | +fn main() { |
| 99 | + assert_eq!(add(3, 14159), 14162) |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Clobbers |
| 104 | + |
| 105 | +Some instructions modify registers which might otherwise have held |
| 106 | +different values so we use the clobbers list to indicate to the |
| 107 | +compiler not to assume any values loaded into those registers will |
| 108 | +stay valid. |
| 109 | + |
| 110 | +``` |
| 111 | +# #![feature(asm)] |
| 112 | +# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 113 | +# fn main() { unsafe { |
| 114 | +// Put the value 0x200 in eax |
| 115 | +asm!("mov $$0x200, %eax" : /* no outputs */ : /* no inputs */ : "eax"); |
| 116 | +# } } |
| 117 | +``` |
| 118 | + |
| 119 | +Input and output registers need not be listed since that information |
| 120 | +is already communicated by the given constraints. Otherwise, any other |
| 121 | +registers used either implicitly or explicitly should be listed. |
| 122 | + |
| 123 | +If the assembly changes the condition code register `cc` should be |
| 124 | +specified as one of the clobbers. Similarly, if the assembly modifies |
| 125 | +memory, `memory` should also be specified. |
| 126 | + |
| 127 | +## Options |
| 128 | + |
| 129 | +The last section, `options` is specific to Rust. The format is comma |
| 130 | +separated literal strings (i.e. `:"foo", "bar", "baz"`). It's used to |
| 131 | +specify some extra info about the inline assembly: |
| 132 | + |
| 133 | +Current valid options are: |
| 134 | + |
| 135 | +1. *volatile* - specifying this is analogous to |
| 136 | + `__asm__ __volatile__ (...)` in gcc/clang. |
| 137 | +2. *alignstack* - certain instructions expect the stack to be |
| 138 | + aligned a certain way (i.e. SSE) and specifying this indicates to |
| 139 | + the compiler to insert its usual stack alignment code |
| 140 | +3. *intel* - use intel syntax instead of the default AT&T. |
| 141 | + |
0 commit comments