Parametric `asm!` registry?

I'm trying to create a "generic" function to read from some registers:


#[inline(always)]
fn read_coproc_reg<T>(reg_name: &str)  ->usize{
    let mut ret: usize;
    unsafe {
        asm!("mrs {}, "+ reg_name, out(reg) ret, options(nostack));
    }
    ret
}

This doesn't work because asm wants string literals. How can I use reg_name as second input parameter?
Thanks,
Federico

I think you could do this as a macro_rules! macro, with a $reg_name parameter generating asm!(concat!("mrs {}, ", $reg_name), ...).

Example for what @cuviper said:

#![feature(asm)]

macro_rules! read_coproc_reg {
    ($name:literal) => {
        unsafe {
            let mut ret: usize;
            asm!(concat!("mov {}, ", $name), out(reg) ret, options(nostack));
            ret
        }
    }
}

fn main() {
    let rsp = read_coproc_reg!("rsp");
    dbg!(rsp);
}
2 Likes

Thanks a lot for your answers! I would have prefered to avoid using macros, but in this case I'm not sure if there is a way around it.