Skip to content

Instantly share code, notes, and snippets.

@eterps
Created October 6, 2022 10:26
Show Gist options
  • Save eterps/d70591a2f6e7f9822e5ef49d0958b82b to your computer and use it in GitHub Desktop.
Save eterps/d70591a2f6e7f9822e5ef49d0958b82b to your computer and use it in GitHub Desktop.
Call write syscall on x86-64 Linux in Rust
use std::arch::asm;
fn main() {
let str = b"Hello world\n";
unsafe {
let res = syswrite(str.as_ptr() as u64, str.len() as u64);
println!("res: {}", res);
}
}
unsafe fn syswrite(strptr: u64, strlen: u64) -> i64 {
let res: i64;
asm!(
"syscall",
in("rax") 1,
in("rdi") 1,
in("rsi") strptr,
in("rdx") strlen,
lateout("rax") res
);
res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment