From a442eaa13afc96054c8e9035080b26511de518f9 Mon Sep 17 00:00:00 2001 From: darcy Date: Sat, 28 Sep 2024 12:21:54 +1000 Subject: [PATCH 1/3] Remove unnecessary string allocation for `PUTS` --- src/runtime.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/runtime.rs b/src/runtime.rs index 8308249..c69df2d 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -282,17 +282,15 @@ impl RunState { 0x22 => { // could probably rewrite with iterators but idk if worth let mut addr = *self.reg(0); - let mut string = String::new(); loop { let chr_raw = *self.mem(addr); let chr_ascii = (chr_raw & 0xFF) as u8 as char; if chr_ascii == '\0' { break; } - string.push(chr_ascii); + print!("{}", chr_ascii); addr += 1; } - print!("{string}"); stdout().flush().unwrap(); } // in From fb2d32359cda7007dbf2535203db3ecf6d526fdf Mon Sep 17 00:00:00 2001 From: darcy Date: Sat, 28 Sep 2024 12:26:17 +1000 Subject: [PATCH 2/3] Implement `PUTSP` based on `PUTS` --- src/runtime.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/runtime.rs b/src/runtime.rs index c69df2d..cdf8242 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -302,8 +302,22 @@ impl RunState { } // putsp 0x24 => { - // TODO: impl putsp - todo!("TODO: putsp can be put off until someone needs it") + let mut addr = *self.reg(0); + loop { + let chr_raw = *self.mem(addr); + let chr_high = ((chr_raw >> 8) & 0xFF) as u8 as char; + if chr_high == '\0' { + break; + } + print!("{}", chr_high); + let chr_low = (chr_raw & 0xFF) as u8 as char; + if chr_low == '\0' { + break; + } + print!("{}", chr_low); + addr += 1; + } + stdout().flush().unwrap(); } // halt 0x25 => { From d25a64b169f4fac5d7e5b6f7e4a6f2a2bd255777 Mon Sep 17 00:00:00 2001 From: darcy Date: Sun, 29 Sep 2024 20:52:31 +1000 Subject: [PATCH 3/3] Remove duplication for `PUT` and `PUTS` --- src/runtime.rs | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/runtime.rs b/src/runtime.rs index cdf8242..cc8db42 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -281,15 +281,13 @@ impl RunState { // puts 0x22 => { // could probably rewrite with iterators but idk if worth - let mut addr = *self.reg(0); - loop { + for addr in *self.reg(0).. { let chr_raw = *self.mem(addr); let chr_ascii = (chr_raw & 0xFF) as u8 as char; if chr_ascii == '\0' { break; } print!("{}", chr_ascii); - addr += 1; } stdout().flush().unwrap(); } @@ -302,20 +300,15 @@ impl RunState { } // putsp 0x24 => { - let mut addr = *self.reg(0); - loop { + 'string: for addr in *self.reg(0).. { let chr_raw = *self.mem(addr); - let chr_high = ((chr_raw >> 8) & 0xFF) as u8 as char; - if chr_high == '\0' { - break; - } - print!("{}", chr_high); - let chr_low = (chr_raw & 0xFF) as u8 as char; - if chr_low == '\0' { - break; + for chr in [chr_raw >> 8, chr_raw & 0xFF] { + let chr_ascii = chr as u8 as char; + if chr_ascii == '\0' { + break 'string; + } + print!("{}", chr_ascii); } - print!("{}", chr_low); - addr += 1; } stdout().flush().unwrap(); }