This commit is contained in:
nora 2025-03-09 01:23:43 +01:00
parent ab8e4ebc13
commit 42c4d826f1
4 changed files with 135 additions and 10 deletions

View file

@ -1,7 +1,7 @@
CC = clang -Wall -Wpedantic -target riscv32-unknown-linux-gnu -fuse-ld=lld -march=rv32i
CC_STATIC = $(CC) -static -nostdlib -nodefaultlibs
RUSTC = rustc --target riscv32i-unknown-none-elf
RUSTC = rustc --target riscv32im-unknown-none-elf
all: init init1

View file

@ -51,23 +51,64 @@ struct Stderr;
impl core::fmt::Write for Stderr {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
let ret = write(2, s.as_bytes());
if ret < 0 { Err(core::fmt::Error) } else { Ok(()) }
if ret < 0 {
Err(core::fmt::Error)
} else {
Ok(())
}
}
}
macro_rules! eprintln {
($($tt:tt)*) => {
writeln!(Stderr, $($tt)*).unwrap();
};
}
macro_rules! eprint {
($($tt:tt)*) => {
write!(Stderr, $($tt)*).unwrap();
};
}
#[panic_handler]
fn handle(info: &core::panic::PanicInfo<'_>) -> ! {
let _ = writeln!(Stderr, "panicked: {}", info.message());
unsafe { core::arch::asm!("unimp", options(noreturn)) }
}
#[no_mangle]
fn _start() {
write(1, b"enter a number: ");
let mut buf = [0; 10];
let len = read(0, &mut buf);
let buf = &buf[..(len as usize)];
let n = str::from_utf8(buf).unwrap().trim().parse::<i32>().unwrap();
fn input() -> u64 {
eprint!("enter a number: ");
exit(n);
loop {
let mut buf = [0; 10];
let len = read(0, &mut buf);
let buf = &buf[..(len as usize)];
let n = str::from_utf8(buf).unwrap().trim();
match n.parse::<u64>() {
Ok(v) => return v,
Err(_) => {
eprint!("not a valid number, try again: ");
}
}
}
}
#[no_mangle]
fn _start() -> ! {
let random_number = 45;
loop {
let n = input();
if n == random_number {
eprintln!("correct!");
break;
} else if n < random_number {
eprintln!("random number is greater");
} else {
eprintln!("random number is smaller");
}
}
exit(0);
}