Simple inline assembly addition example (#24)

* requires 1.54.0
This commit is contained in:
Scott Mabin 2021-08-17 10:08:22 +01:00 committed by GitHub
parent ac5f032cd9
commit 990470bedd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -1,8 +1,11 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(asm)]
#[cfg(not(feature = "std"))]
use core::panic::PanicInfo;
/// Create aliases for FFI types for esp32c3, which doesn't have std.
#[cfg(not(feature = "std"))]
mod ffi {
@ -31,6 +34,19 @@ pub extern "C" fn add_in_rust(x: i32, y: i32) -> i32 {
x + y
}
#[no_mangle]
pub extern "C" fn add_in_rust_inline_asm(mut x: i32, y: i32) -> i32 {
unsafe {
sys::validate_param_in_c(0, x);
sys::validate_param_in_c(1, y);
}
unsafe {
// more detail available: https://doc.rust-lang.org/beta/unstable-book/library-features/asm.html
asm!("add {0}, {0}, {1}", inout(reg) x, in(reg) y);
}
x
}
#[cfg(not(feature = "std"))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {

View File

@ -40,6 +40,11 @@ void app_main(void)
int sum = add_in_rust(x, y);
printf("Rust calculated %d + %d = %d\n\n", x, y, sum);
x = 9;
y = 10;
sum = add_in_rust_inline_asm(x, y);
printf("Rust calculated (using asm!) %d + %d = %d\n\n", x, y, sum);
for (int i = 10; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(1000 / portTICK_PERIOD_MS);