0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paiza.ioでaarch64アセンブラ その3

Last updated at Posted at 2025-11-14

概要

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 

成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?