Filip Balaz
Filip Balaz

Reputation: 11

How can I call a Rust function from assembly?

I am looking for like C calling via function call [function]. I am linking asm and Rust in one binary via ld.

Upvotes: 0

Views: 1099

Answers (1)

Shepmaster
Shepmaster

Reputation: 430841

If you know how to call a C function from assembly (I don't, offhand), then this will help you get a Rust function that can be called like a C function:

// foo.rs
#[no_mangle]
pub extern fn increment(a: i32) -> i32 {
    a + 1
}

Compile it as a dynamic or static lib:

$ rustc --crate-type dylib foo.rs
$ nm libfoo.dylib  | grep increment
0000000000000c70 t __ZN9increment10__rust_abiE
0000000000000c30 T _increment

Now you have a library that can be called like a C library. You should read the entire FFI Guide to better understand the tradeoffs and pitfalls of calling Rust from outside of Rust.

Upvotes: 5

Related Questions