From 1af92eb15618f56811c5845e00829c4be6fe301c Mon Sep 17 00:00:00 2001 From: StarCells Date: Thu, 5 Nov 2026 22:43:19 +0800 Subject: [PATCH] r8asm: make .loop consume the bit it just cleared .endloop expanded to >-+]< : flb clears the LSB and rnz then tests that very same bit, so it always sees 0 and the body ran exactly once no matter how many 1s the counter cell held. Measured: a counter of 0b111 came out as 0b110 after a single pass. Putting a ror after the flb pulls the next bit down before the test, which is what SPEC describes -- consecutive 1s from the LSB, one per iteration. Measured: 0b0000_0111 -> 3 passes, counter 0 0b1111_1111 -> 8 passes, counter 0 (a bare ror would spin forever) Nothing under asm/ uses .loop, so no existing tape changes. --- src/r8asm.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/r8asm.cc b/src/r8asm.cc index e90efb7..e6721d6 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -281,8 +281,13 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) { // >[< } break; case builtin_t::ENDLOOP: { + // Consume one '1' from the auxiliary cell and pull the next bit down. + // Without the ror, the rnz could only ever see the bit that flb just + // cleared, so a .loop over 0b111 ran once instead of three times + // (SPEC: "consecutive 1s starting from the LSB"). to_return.children.push_back(ROT8_NODE("stp")); to_return.children.push_back(ROT8_NODE("flb")); + to_return.children.push_back(ROT8_NODE("ror")); to_return.children.push_back(ROT8_NODE("rnz")); to_return.children.push_back(ROT8_NODE("btp")); // >-+]<