Skip to content

Commit e82be71

Browse files
tesujiAmanieu
andcommitted
document how to use memory addresses as operands
Co-authored-by: Amanieu <[email protected]>
1 parent 8d96cc2 commit e82be71

File tree

1 file changed

+19
-0
lines changed
  • src/doc/unstable-book/src/library-features

1 file changed

+19
-0
lines changed

src/doc/unstable-book/src/library-features/asm.md

+19
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,25 @@ The `h` modifier will emit the register name for the high byte of that register
345345

346346
If you use a smaller data type (e.g. `u16`) with an operand and forget the use template modifiers, the compiler will emit a warning and suggest the correct modifier to use.
347347

348+
## Memory address operands
349+
350+
Sometimes assembly instructions require operands passed via memory addresses/memory locations.
351+
You have to manually use the memory address syntax specified by the respectively architectures.
352+
For example, in x86/x86_64 and intel assembly syntax, you should wrap inputs/outputs in `[]`
353+
to indicate they are memory operands:
354+
355+
```rust,allow_fail
356+
# #![feature(asm, llvm_asm)]
357+
# fn load_fpu_control_word(control: u16) {
358+
unsafe {
359+
asm!("fldcw [{}]", in(reg) &control, options(nostack));
360+
361+
// Previously this would have been written with the deprecated `llvm_asm!` like this
362+
llvm_asm!("fldcw $0" :: "m" (control) :: "volatile");
363+
}
364+
# }
365+
```
366+
348367
## Options
349368

350369
By default, an inline assembly block is treated the same way as an external FFI function call with a custom calling convention: it may read/write memory, have observable side effects, etc. However in many cases, it is desirable to give the compiler more information about what the assembly code is actually doing so that it can optimize better.

0 commit comments

Comments
 (0)