[Learn Rust programming with Xiaojia] 24. Inline assembly (inline assembly)

Series Article Directory

[Learn Rust programming with Xiaojia] 1. Basics of Rust programming
[Learn Rust programming with Xiaojia] 2. Use of Rust package management tools
[Learn Rust programming with Xiaojia] 3. Basic programming concepts of Rust
[Learn Rust programming with Xiaojia] 】4. Understand the concept of ownership in Rust
【Learn Rust programming from Xiaojia】5. Use structures to associate structured data
【Learn Rust programming from Xiaojia】6. Enumeration and pattern matching
【Learn Rust programming from Xiaojia】7. Use packages (Packages), unit packages (Crates) and modules (Module) to manage projects
[Learn Rust programming with Xiaojia] 8. Common collections
[Learn Rust programming with Xiaojia] 9. Error handling (Error Handling)
[Follow Xiao Jia learns Rust programming] 11. Write automated tests
[Learn Rust programming with Xiao Jia] 12. Build a command line program
[Learn Rust programming with Xiao Jia] 13. Functional language features: iterators and closures
[ Learn Rust Programming from Xiaojia] 14. About Cargo and Crates.io
[Learn Rust Programming from Xiaojia] 15. Smart Pointer (Smart Point)
[Learn Rust Programming from Xiaojia] 16. Fearless Concurrency
[Learn Rust programming with Xiaojia] 17. Object-oriented language features
[Learn Rust programming with Xiaojia] 18. Pattern matching (Patterns and Matching)
[Learn Rust programming with Xiaojia] 19. Advanced features
[Learn Rust programming with Xiaojia] Learn Rust programming] Twenty, advanced extension
[Learn Rust programming with Xiaojia] 21. Network programming
[Learn Rust programming with Xiaojia] 23. Cargo user guide
[Learn Rust programming with Xiaojia] 24. Inline assembly (inline assembly)

foreword

Rust introduced inline assembly support in version 1.59, which is very useful for applications that require low-level control, such as controlling low-level execution, accessing specific machine instructions, and so on.

For the main teaching material, please refer to "The Rust Programming Language".
For the main teaching material, please refer to "Rust For Rustaceans". For the main teaching material, please refer to "
The Rustonomicon ".


1. Inline Assembly (Inline Assembly)

1.1、asm!

Using asm!and global_asm!macros , the assembly language and instructions used in inline assembly depend on the corresponding machine platform, so far Rust supports inline assembly for the following platforms

  • x86 and x86-64
  • ARM
  • AArch64
  • RISC-V
  • LoongArch

If not supported, the asm! macro will trigger an error.

use std::arch::asm;

// 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);

1.2. Parameters of asm macros

These parameters are for the compiler to help allocate registers

  • in: Indicates that the variable value is passed to the register
  • out : Indicates that the value of the register is written to the variable
  • inout: Instant input is also output, this way can guarantee the same register to complete the task
  • lateout: any output that is only written after all input has been consumed
  • inlateout: the output value is not modified until all registers are read;
  • sym: asm can aggregate symbol relocations to read from thread-local data

For the specific content, we will put it in the part of Rust embedded explanation, which is not the focus of our series at present

Summarize

The above content is to explain Rust's inline assembly

Guess you like

Origin blog.csdn.net/fj_Author/article/details/132614268