概要
paiza.ioでaarch64アセンブラやってみる。
インラインアセンブラ、やってみた。
サンプルコード
use std::fs;
use std::io::{Write, BufWriter};
use std::process::Command;
fn main() {
let b = "
#include <stdio.h>
long int addvalues(long int val1, long int val2) {
long int result;
__asm__ (\"ADD %[Rd], %[Rs1], %[Rs2]\" : [Rd] \"=r\" (result) : [Rs1] \"r\" (val1), [Rs2] \"r\" (val2));
return result;
}
int main(int argc, char **argv, char **envp) {
long int val = addvalues(1234567890123456789L, 1L);
printf(\"addvalues = %ld \", val);
return 0;
}";
let mut f = BufWriter::new(fs::File::create("hello.c").unwrap());
f.write(b.as_bytes()).unwrap();
f.flush();
let output0 = Command::new("gcc").arg("hello.c").output().expect("failed to execute process");
let hello0 = output0.stderr;
println!("{}", std::str::from_utf8(&hello0).unwrap());
let output = Command::new("./a.out").output().expect("failed to execute process");
let hello = output.stdout;
println!("{}", std::str::from_utf8(&hello).unwrap());
}
投入したソース
#include <stdio.h>
long int addvalues(long int val1, long int val2) {
long int result;
__asm__ (
"ADD %[Rd], %[Rs1], %[Rs2]"
: [Rd] "=r" (result)
: [Rs1] "r" (val1), [Rs2] "r" (val2)
);
return result;
}
int main(int argc, char **argv, char **envp) {
long int val = addvalues(1234567890123456789L, 1L);
printf("addvalues = %ld ", val);
return 0;
}
実行結果
addvalues = 1234567890123456790
成果物
以上。