Files
R8Forge/examples/undigits_demo.r8asm
dsh c4048132cc asm/undigits: the inverse of clb_digits (ASCII decimal -> integer)
clb_digits could turn a number into four ASCII digits but nothing could turn
them back, so a program that stored or received a decimal string had no way
home.  clb_undigits REG N S is that way back:

    clb_digits   N REG S      ; 1234 -> 49 50 51 52
    clb_undigits REG N S      ; 49 50 51 52 -> 1234

Same number of digit cells as clb_digits writes (floor(W*log10 2) + 1, four at
11 bits), each read as its low 7 bits, so leading zeros are fine and garbage in
bits 7 and above is ignored.  No validity checking, exactly like clb_digits: a
cell that is not '0'..'9' yields a deterministic but meaningless number, and the
documentation says so.

Horner in a runtime loop, one clb_mul in the whole tape:

    N = 0 ; repeat 4: N = N*10 + digit ; shift the digit cells down one

9642 characters (minimum driver) -- smaller than clb_digits, because the reverse
direction needs no division.

Three things worth remembering, all found by testing rather than reasoning:

  * c ^ 48 == c - 48 for '0'..'9' (the top two bits cancel, the low four are
    identical), so subtracting '0' is one assembly time `.xor 48`: no
    subtraction, no constant cell.
  * The low-7-bit mask is `ror` 7, then set_0+ror per bit for the rest: the total
    rotation is W, so the cell ends up facing the same way.  The first version
    wrote `rol; set_0; ror` in a %rep and cleared the same bit four times -- it
    looks right and is undetectable on inputs whose high bits are already 0
    (measured: 0|5<<7 came through as 688).
  * The round counter must be erased before it is built: set_1 only clears the
    bit it rotates to, so high ones left in the cell turn into extra rounds
    (measured: dirty scratch gave 1234 -> 1564).

Tests and docs:

  * r8forge_byte_op_test gains test_undigits: all 2048 values x {clean, garbage
    in bits 7+} = 4096 cases, worst 36,481 steps.
  * examples/undigits_demo.r8asm: "1234" -> 1234, "0007" -> 7, and a
    digits -> undigits round trip on 2047; examples_test runs it like the rest.
  * asm/test.r8asm calls it too (round trip inside the digits section).  That
    pushed the file over ROT-8-viz's 65536 instruction limit, so the redundant
    second raw_add case, the reversed raw_sub case and the standalone divmod
    section are gone -- every macro is still called at least once, and the file
    is 64513 characters / 236439 steps / 93 outputs now.
  * SPEC 7.9 (contract + the three traps), 10.2, 10.3 and 11, the AGENTS byte
    table / verified list / naming table / baselines, and the README example
    table all updated.
2026-11-09 16:17:39 +08:00

57 lines
1.0 KiB
Plaintext

; clb_undigits:把一串 ASCII 十进制数字拼回整数 —— clb_digits 的反方向。
;
; 三个用例:
; 1) 直接摆 "1234"(ASCII 49 50 51 52)-> 1234
; 2) "0007" -> 7(前导 0 不是问题)
; 3) 往返:2047 -> clb_digits -> "2047" -> clb_undigits -> 2047
; (顺便把中间那四位 ASCII 打出来)
; out 1234 7 50 48 52 55 2047
%include "asm/digits.r8asm"
%include "asm/undigits.r8asm"
; ---- 1) "1234"
.var REG 0
.var N 4
.var S 5
.tp REG
.xor 49
.tp REG+1
.xor 50
.tp REG+2
.xor 51
.tp REG+3
.xor 52
clb_undigits REG N S
output N ; 1234
; ---- 2) "0007"
.var REG2 20
.var N2 24
.var S2 25
.tp REG2
.xor 48
.tp REG2+1
.xor 48
.tp REG2+2
.xor 48
.tp REG2+3
.xor 55
clb_undigits REG2 N2 S2
output N2 ; 7
; ---- 3) 往返
.var V 40
.var DREG 41
.var DS 45
.var N3 58
.var S3 59
.tp V
.xor 2047
clb_digits V DREG DS
output DREG+0 ; 50 '2'
output DREG+1 ; 48 '0'
output DREG+2 ; 52 '4'
output DREG+3 ; 55 '7'
clb_undigits DREG N3 S3
output N3 ; 2047