Skip to content

Instantly share code, notes, and snippets.

@tonetheman
Last active August 23, 2021 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonetheman/4e0a69f3c0af69b864df2b373f739111 to your computer and use it in GitHub Desktop.
Save tonetheman/4e0a69f3c0af69b864df2b373f739111 to your computer and use it in GitHub Desktop.
rust syscall
#![feature(llvm_asm)]
fn main() {
let message = String::from("hello world tony!\n");
syscall(message);
}
#[cfg(target_os = "linux")]
#[inline(never)]
fn syscall(message: String) {
let msg_ptr = message.as_ptr();
print!("{:?}\n", msg_ptr);
let len = message.len();
print!("len is {:?}\n", len);
unsafe {
llvm_asm!("
mov $$1, %rax # sys call 1 is write on linux
mov $$1, %rdi # file handle 1 is std out
mov $0, %rsi # address of string to output
mov $1, %rdx # number of bytes
syscall # call the kernel now
"
:
: "r"(msg_ptr), "r"(len)
: "rax", "rdi", "rsi", "rdx"
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment