Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Created May 14, 2018 22:11
Show Gist options
  • Save Pzixel/a8f2aba3e155828f975b27600337de9e to your computer and use it in GitHub Desktop.
Save Pzixel/a8f2aba3e155828f975b27600337de9e to your computer and use it in GitHub Desktop.
#![feature(lang_items)]
#![feature(start)]
#![no_std]
#![no_main]
// These functions are used by the compiler, but not
// for a bare-bones hello world. These are normally
// provided by libstd.
#[lang = "eh_personality"]
#[no_mangle]
pub extern fn rust_eh_personality() {
}
// This function may be needed based on the compilation target.
#[lang = "eh_unwind_resume"]
#[no_mangle]
pub extern fn rust_eh_unwind_resume() {
}
#[lang = "panic_fmt"]
#[no_mangle]
pub extern fn rust_begin_panic(_msg: core::fmt::Arguments,
_file: &'static str,
_line: u32) -> ! {
loop {}
}
// Entry point for this program.
#[no_mangle] // ensure that this symbol is called `main` in the output
pub extern fn main(_argc: i32, _argv: *const *const u8) -> i32 {
extern {
fn printf(fmt: *const u8, ...) -> i32;
fn scanf(format: *const u8, ...) -> i32;
}
let mut buffer = [0u8; 100];
unsafe {
scanf("%s".as_ptr(), buffer.as_mut_ptr());
let mut remain = 0;
for b in buffer.iter() {
if *b >= 0x30 {
remain = ((remain * 10) + b - 0x30) % 7;
} else {
break;
}
}
let result = [remain + 0x30, 0];
printf(result.as_ptr());
}
0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment