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.
16 Any use of `asm` is feature gated (requires `#![feature(asm)]` on the
17 crate to allow) and of course requires an `unsafe` block.
19 > **Note**: the examples here are given in x86/x86-64 assembly, but
20 > all platforms are supported.
24 The `assembly template` is the only required parameter and must be a
25 literal string (i.e. `""`)
30 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
38 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
39 fn foo() { /* ... */ }
48 (The `feature(asm)` and `#[cfg]`s are omitted from now on.)
50 Output operands, input operands, clobbers and options are all optional
51 but you must add the right number of `:` if you skip them:
55 # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
56 # fn main() { unsafe {
65 Whitespace also doesn't matter:
69 # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
70 # fn main() { unsafe {
71 asm!("xor %eax, %eax" ::: "{eax}");
77 Input and output operands follow the same format: `:
78 "constraints1"(expr1), "constraints2"(expr2), ..."`. Output operand
79 expressions must be mutable lvalues, or not yet assigned:
83 # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
84 fn add(a: i32, b: i32) -> i32 {
94 # #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
95 # fn add(a: i32, b: i32) -> i32 { a + b }
98 assert_eq!(add(3, 14159), 14162)
102 If you would like to use real operands in this position, however,
103 you are required to put curly braces `{}` around the register that
104 you want, and you are required to put the specific size of the
105 operand. This is useful for very low level programming, where
106 which register you use is important:
110 # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
111 # unsafe fn read_byte_in(port: u16) -> u8 {
113 asm!("in %dx, %al" : "={al}"(result) : "{dx}"(port));
120 Some instructions modify registers which might otherwise have held
121 different values so we use the clobbers list to indicate to the
122 compiler not to assume any values loaded into those registers will
127 # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
128 # fn main() { unsafe {
129 // Put the value 0x200 in eax
130 asm!("mov $$0x200, %eax" : /* no outputs */ : /* no inputs */ : "{eax}");
134 Input and output registers need not be listed since that information
135 is already communicated by the given constraints. Otherwise, any other
136 registers used either implicitly or explicitly should be listed.
138 If the assembly changes the condition code register `cc` should be
139 specified as one of the clobbers. Similarly, if the assembly modifies
140 memory, `memory` should also be specified.
144 The last section, `options` is specific to Rust. The format is comma
145 separated literal strings (i.e. `:"foo", "bar", "baz"`). It's used to
146 specify some extra info about the inline assembly:
148 Current valid options are:
150 1. *volatile* - specifying this is analogous to
151 `__asm__ __volatile__ (...)` in gcc/clang.
152 2. *alignstack* - certain instructions expect the stack to be
153 aligned a certain way (i.e. SSE) and specifying this indicates to
154 the compiler to insert its usual stack alignment code
155 3. *intel* - use intel syntax instead of the default AT&T.
159 # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
163 asm!("mov eax, 2" : "={eax}"(result) : : : "intel")
165 println!("eax is currently {}", result);
171 The current implementation of the `asm!` macro is a direct binding to [LLVM's
172 inline assembler expressions][llvm-docs], so be sure to check out [their
173 documentation as well][llvm-docs] for more information about clobbers,
176 [llvm-docs]: http://llvm.org/docs/LangRef.html#inline-assembler-expressions