diff --git a/README.md b/README.md index 214f3b9..03542b0 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,12 @@ The interrupt 0 exits the program. The exit code is passed in `r0`. ### `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. -After the interrupt completed, `r0` will contain a 0 if writing was successful, or a `1` if it failed. \ No newline at end of file +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`. \ No newline at end of file diff --git a/doc/src/02_architecture_constraints.adoc b/doc/src/02_architecture_constraints.adoc index 636968b..8614b9a 100644 --- a/doc/src/02_architecture_constraints.adoc +++ b/doc/src/02_architecture_constraints.adoc @@ -5,7 +5,7 @@ |=== |Constraint|Reason |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 diff --git a/src/interpret.rs b/src/interpret.rs index bc3874f..ee79686 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -9,7 +9,7 @@ //! Decimal 1 //! ``` -use std::io::Write; +use std::io::{Read, Write}; use logos::Span; @@ -113,6 +113,13 @@ impl InterpretCtx { let is_ok = std::io::stdout().lock().write_all(slice).is_ok(); *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!"), } }