Llvm_asm is not available

#![feature(asm)]
fn main() {
    println!("hellow word");
    let result: u32;
    unsafe {
        llvm_asm!("mov eax, 07H;
              mov ecx, 0;
              cpuid;"
              : "={ebx}"(result)
              :
              : "eax, ebx, ecx, edx"
              : "intel"
        );
    }
}

error[E0658]: use of unstable library feature 'llvm_asm': prefer using the new asm! syntax instead
 --> src/main.rs:6:9
  |
6 |         llvm_asm!("mov eax, 07H;
  |         ^^^^^^^^
  |
  = note: see issue #70173 <https://github.com/rust-lang/rust/issues/70173> for more information

what should I do !

To use the llvm_asm! you need the llvm_asm feature.

https://doc.rust-lang.org/unstable-book/library-features/llvm-asm.html

I want to know how to implement according to github Implement new asm! syntax from RFC 2850 by Amanieu · Pull Request #69171 · rust-lang/rust · GitHub

#![feature(asm)]

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn foo() {
    unsafe {
        llvm_asm!("NOP");
    }
}


fn main() {
    println!("hellow word");
    foo();
    // let result: u32;
    // unsafe {
    //     llvm_asm!("mov eax, 07H;
    //           mov ecx, 0;
    //           cpuid;"
    //           : "={ebx}"(result)
    //           :
    //           : "eax, ebx, ecx, edx"
    //           : "intel"
    //     );
    // }
}

I modified it according to the documentation. It still seems the same problem. My rust is 1.48. It seems to be updated.

Is there a corresponding demo? I still don’t understand

If you want to use the old syntax, use feature(llvm_asm) and the llvm_asm! macro:

#![feature(llvm_asm)]

fn foo() {
    unsafe {
        llvm_asm!("NOP");
    }
}

Or, if you want to use the new asm syntax, then use feature(asm) and the asm! macro instead:

#![feature(asm)]

fn foo() {
    unsafe {
        asm!("nop");
    }
}
1 Like
#![feature(llvm_asm)]

fn foo() {
    unsafe {
        llvm_asm!("NOP");
    }
}


fn main() {
    println!("hellow word");
    foo();
    // let result: u32;
    // unsafe {
    //     llvm_asm!("mov eax, 07H;
    //           mov ecx, 0;
    //           cpuid;"
    //           : "={ebx}"(result)
    //           :
    //           : "eax, ebx, ecx, edx"
    //           : "intel"
    //     );
    // }
}

error[E0554]: `#![feature]` may not be used on the stable release channel
 --> src/main.rs:1:1
  |
1 | #![feature(llvm_asm)]
  | ^^^^^^^^^^^^^^^^^^^^^


PS: What is your version number

#![feature(asm)]
fn main() {
    println!("hellow word");
    let result: u32;
    unsafe {
        asm!("mov eax, 07H;
              mov ecx, 0;
              cpuid;"
              : "={ebx}"(result)
              :
              : "eax, ebx, ecx, edx"
              : "intel"
        );
    }
}
error: the legacy LLVM-style asm! syntax is no longer supported
  --> src/main.rs:6:9
   |
6  |           asm!("mov eax, 07H;
   |           ^---
   |           |
   |  _________help: replace with: `llvm_asm!`
   | |
7  | |               mov ecx, 0;
8  | |               cpuid;"
9  | |               : "={ebx}"(result)
...  |
12 | |               : "intel"
13 | |         );
   | |__________^
   |
   = note: consider migrating to the new asm! syntax specified in RFC 2873
   = note: alternatively, switch to llvm_asm! to keep your code working as it is


Both llvm_asm! and asm! are experimental features, so you will need to use the nightly toolchain. I'm using Rust 1.51.0-nightly.

Your code is using the old “LLVM-style” syntax. In recent versions of Rust, you must use llvm_asm! for this syntax. Also, the clobbers line is not formatted correctly. Here's a version of your code that compiles in the current nightly compiler:

#![feature(llvm_asm)]

fn main() {
    let result: u32;
    unsafe {
        llvm_asm!("mov eax, 07H;
              mov ecx, 0;
              cpuid;"
              : "={ebx}"(result)
              :
              : "eax", "ebx", "ecx", "edx"
              : "intel"
        );
    }
}

Playground

2 Likes

It looks like this code was copied from this Stack Overflow question. There is some useful information in the comments on Stack Overflow:

1 Like

I checked this link

But I want to express that after Rust 1.40, I upgraded asm to llvm to 1.48 and abandoned the brand new method adopted by llvm_asm. I will not use the new method. Is there a demo?

Are you looking for example code for the new, non-LLVM syntax? There are several examples in RFC 2873. Here's one of them:

#![feature(asm)]

fn main() {
    let i: u64 = 3;
    let o: u64;
    unsafe {
        asm!(
            "mov {0}, {1}",
            "add {0}, {number}",
            out(reg) o,
            in(reg) i,
            number = const 5,
        );
    }
    assert_eq!(o, 8);
}

Playground

Thank you very much, I found that I can't run with 1.49 or 1.50 when I debug on the platform you provided.
STABLE --> 1.49

My rust version 1.49, system Mac 11.0.1

That's right, you need to use the nightly toolchain. You can use rustup to install and run the nightly toolchain.

Well, is ASM still in the experimental stage in Rust? The system Rust can stabilize as soon as possible, I am very optimistic about Rust

Yes, inline assembler is still experimental. If you need some ASM code that works with the stable toolchain, you’ll need to build it into a system library using some other tool. You can then call into that library with Rust’s FFI features.

You can get updates about asm!’s stabilization by following its tracking issue.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.