This commit is contained in:
nora 2022-06-27 12:14:42 +02:00
parent 7d120f7b5e
commit 72fbc4e55a
3 changed files with 17 additions and 4 deletions

View file

@ -138,6 +138,12 @@ The interrupt 0 exits the program. The exit code is passed in `r0`.
### `int 1` ### `int 1`
The interrupt 0 writes a string to standard output. `r0` contains the address of the string data. The interrupt 1 writes a string to standard output. `r0` contains the address of the string data.
`r1` contains the length of the string. The string is forwarded as raw bytes to the OS. `r1` contains the length of the string. The string is forwarded as raw bytes to the OS.
After the interrupt completed, `r0` will contain a 0 if writing was successful, or a `1` if it failed. After the interrupt completed, `r0` will contain a `0` if writing was successful, or a `1` if it failed.
### `int 2`
The interrupt 2 reads in a UTF-8 string from stdin. `r0` contains the address of the buffer for the data.
`r1` contains the length of the buffer. The interrupt returns after the buffer has been filled. If reading fails,
`1` will be put into `r0`, if it was successful, `0`.

View file

@ -5,7 +5,7 @@
|=== |===
|Constraint|Reason |Constraint|Reason
|Interpreter|Because the time for implementation is limited, the simpler option of an interpreter has to be chosen over a compiler. |Interpreter|Because the time for implementation is limited, the simpler option of an interpreter has to be chosen over a compiler.
|4 weeks limited time|The time of the assignment |4 weeks limited time|The time of the assignment.
|=== |===
== Conventions == Conventions

View file

@ -9,7 +9,7 @@
//! Decimal 1 //! Decimal 1
//! ``` //! ```
use std::io::Write; use std::io::{Read, Write};
use logos::Span; use logos::Span;
@ -113,6 +113,13 @@ impl InterpretCtx {
let is_ok = std::io::stdout().lock().write_all(slice).is_ok(); let is_ok = std::io::stdout().lock().write_all(slice).is_ok();
*self.reg_mut(Register(0)) = if is_ok { 0 } else { 1 }; *self.reg_mut(Register(0)) = if is_ok { 0 } else { 1 };
} }
2 => {
let buffer_addr = self.reg_addr(Register(0));
let buffer_len = self.reg_addr(Register(1));
let slice = &mut self.memory[buffer_addr..][..buffer_len];
let is_ok = std::io::stdin().read_exact(slice).is_ok();
*self.reg_mut(Register(0)) = if is_ok { 0 } else { 1 };
}
_ => panic!("invalid interrupt!"), _ => panic!("invalid interrupt!"),
} }
} }