【问题标题】:Read value of ebx register in Rust with inline assembly ("rbx is used internally by LLVM and cannot be used as an operand for inline asm")使用内联汇编在 Rust 中读取 ebx 寄存器的值(“rbx 在 LLVM 内部使用,不能用作内联 asm 的操作数”)
【发布时间】:2021-06-07 11:51:38
【问题描述】:

我正在编写 Rust 代码,需要获取当前存储在“ebx”寄存器 (x86) 中的值。

我的代码 [0] 如下所示:

#![feature(asm)]

fn main() {
    let ebx: u32;
    unsafe { asm!("", out("ebx") ebx) };
    println!("current value of register ebx is: {}", ebx);
}

rustc 1.54.0-nightly 的错误代码是:

invalid register `ebx`: rbx is used internally by LLVM and cannot be used as an operand for inline asm

我发现的唯一可行的解​​决方法是传统方式:

unsafe {
    llvm_asm!("" : "={ebx}"(ebx) : :);
}

有没有办法通过新的asm! 宏实现所需的功能?

PS:由于我的研究,我发现了这个:https://github.com/rust-lang/rust/pull/84658。但它仍然没有提供一个好的简单的解决方案。

[0]https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=8d0a322236cce1efbebedccc40ddbf71

【问题讨论】:

    标签: rust


    【解决方案1】:

    您可以将其移至另一个寄存器。

    #![feature(asm)]
    
    fn main() {
        let ebx: u32;
        unsafe { asm!("mov {:e}, ebx", out(reg) ebx) };
        println!("current value of register ebx is: {}", ebx);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      相关资源
      最近更新 更多