Last active
August 23, 2021 13:38
-
-
Save tonetheman/4e0a69f3c0af69b864df2b373f739111 to your computer and use it in GitHub Desktop.
rust syscall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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