Skip to content

Commit

Permalink
Remove duplication for PUT and PUTS
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Sep 29, 2024
1 parent fb2d323 commit d25a64b
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down

0 comments on commit d25a64b

Please sign in to comment.