Last active
January 6, 2020 14:14
-
-
Save tirr-c/a1b64132b17a3c15d2c010d7394b1ec7 to your computer and use it in GitHub Desktop.
Minimal Hello World in Rust
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(asm, panic_implementation)] | |
#![no_std] | |
#![no_main] | |
///! With nightly compiler, run: | |
///! ```sh | |
///! rustc -O -C panic=abort -Z pre-link-arg=-nostartfiles no-std.rs | |
///! ``` | |
#[panic_implementation] | |
#[no_mangle] | |
pub extern "C" fn panic(_: &core::panic::PanicInfo) -> ! { | |
loop {} | |
} | |
pub mod syscall { | |
pub struct Syscall<T>(::core::marker::PhantomData<T>); | |
macro_rules! syscall_impl { | |
($syscall:ident $count:ident $id:ident, $ret:ident ( $( $arg:ident ),* ) { $asm:expr }) => { | |
pub struct $count; | |
impl $syscall<$count> { | |
pub unsafe fn with($id: usize $(, $arg: usize)*) -> usize { | |
let $ret: usize; | |
$asm; | |
$ret | |
} | |
} | |
}; | |
} | |
syscall_impl!(Syscall Zero id, ret () { | |
asm!( | |
"syscall" | |
: "={rax}"(ret) | |
: "{rax}"(id) | |
: "memory" | |
: "volatile" | |
) | |
}); | |
syscall_impl!(Syscall One id, ret (arg1) { | |
asm!( | |
"syscall" | |
: "={rax}"(ret) | |
: "{rax}"(id), "{rdi}"(arg1) | |
: "memory" | |
: "volatile" | |
) | |
}); | |
syscall_impl!(Syscall Two id, ret (arg1, arg2) { | |
asm!( | |
"syscall" | |
: "={rax}"(ret) | |
: "{rax}"(id), "{rdi}"(arg1), "{rsi}"(arg2) | |
: "memory" | |
: "volatile" | |
) | |
}); | |
syscall_impl!(Syscall Three id, ret (arg1, arg2, arg3) { | |
asm!( | |
"syscall" | |
: "={rax}"(ret) | |
: "{rax}"(id), "{rdi}"(arg1), "{rsi}"(arg2), "{rdx}"(arg3) | |
: "memory" | |
: "volatile" | |
) | |
}); | |
} | |
fn exit(code: usize) -> ! { | |
use syscall::{Syscall, One}; | |
unsafe { | |
Syscall::<One>::with(60, code); | |
core::hint::unreachable_unchecked(); | |
} | |
} | |
fn puts(s: &str) { | |
use syscall::{Syscall, Three}; | |
unsafe { | |
Syscall::<Three>::with( | |
1, | |
0, | |
s as *const str as *const u8 as usize, | |
s.len() | |
); | |
} | |
} | |
#[no_mangle] | |
pub extern "C" fn _start() -> ! { | |
puts("Hello, world!\n"); | |
exit(0); | |
} |
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
no-std: file format elf64-x86-64 | |
Disassembly of section .text: | |
0000000000001000 <_start>: | |
1000: 48 8d 35 f9 0f 00 00 lea 0xff9(%rip),%rsi # 2000 <_start+0x1000> | |
1007: b8 01 00 00 00 mov $0x1,%eax | |
100c: 31 ff xor %edi,%edi | |
100e: ba 0e 00 00 00 mov $0xe,%edx | |
1013: 0f 05 syscall | |
1015: b8 3c 00 00 00 mov $0x3c,%eax | |
101a: 31 ff xor %edi,%edi | |
101c: 0f 05 syscall | |
101e: 0f 0b ud2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment