# STR core r002 -- candidate callable ABI
## Scope
Six independent bounded NUL byte-string functions. `STR_APPEND` depends explicitly on `STR_COPY`, all others are self-contained; `str_abi.inc` supplies common statuses. No UI, keyboard, serial or numerical-library ownership. Methods are callable via `CAL` and return with `RTN`.
## Input/return matrix
| Method | Input registers | Output registers | Status cases |
|---|---|---|---|
| `STR_LEN` | `$10/11` address, `$12` read cap | `$0` status, `$8` length | OK, BAD_CAP, UNTERMINATED |
| `STR_COPY` | `$10/11` source, `$12` cap, `$13/14` dest, `$15` cap | `$0` status, `$8` source length | OK, BAD_CAP, UNTERMINATED, NO_SPACE |
| `STR_CMP` | `$10/11` A, `$12` cap, `$13/14` B, `$15` cap | `$0` status, `$8` cmp (-1/0/+1 encoded FF/00/01) | OK, BAD_CAP, UNTERMINATED |
| `STR_APPEND` | same pointers/caps as COPY | `$0` status, `$8` final text length | OK, BAD_CAP, UNTERMINATED, NO_SPACE |
| `STR_SKIPWS` | `$10/11` source, `$12` cap, `$13` start offset | `$0` status, `$8` first non-WS/NUL offset, `$9` skip length | OK, BAD_CAP, UNTERMINATED, BAD_OFFSET |
| `STR_INDEX` | `$10/11` haystack, `$12` cap, `$13/14` needle, `$15` cap | `$0` status, `$8` first occurrence | OK, BAD_CAP, UNTERMINATED, NOT_FOUND |
All status values are defined by `str_abi.inc` (0..5) as `STR_OK`, `STR_UNTR`, `STR_NOSP`, `STR_BCAP`, `STR_BOFF`, `STR_NFND` respectively. The old `STR_ST_*` equate labels were renamed in r002a to comply with HD61 Rev. 0.45; semantic status values are unchanged. On errors, `$8` is zero, and `$9` also zero for `STR_SKIPWS`. The comparison value FFh is *not* an error when `$0=0`. `STR_INDEX` returning zero is success for a match at offset zero; NOT_FOUND is signaled **only** by status 5.
## ABI prerequisites
Pointer pairs little-endian; capacities unsigned byte 1..255 including NUL; length 0..254. The caller must select the appropriate RAM bank in `UA` and keep each span contiguous inside it. `$SY` is bound to a register containing 1 (currently `$30`); STR does **not** depend on `$SZ`. The current Lab initializes `PSR SZ,0`, binding it to volatile status register `$0` rather than a zero literal. STR r003 uses immediate `0` for zero initializers. Current Lab `$SX` refers to `$31=0`. The functions do not change memory bank, keyboard state, display state, serial configuration, or the special registers. All public input register values stay unchanged. `IX` and flags may change. Methods use no persistent private RAM and are not promised interrupt-safe or reentrant across unknown firmware environments.
## Register effects
| Method | Working registers possibly changed beyond return values |
|---|---|
| LEN | `$9,$16`, IX, flags |
| COPY | `$9,$16,$17,$20,$21`, IX, flags |
| CMP | `$9,$16..$18,$20`, IX, flags |
| APPEND | `$9,$16,$17,$20..$23,$26..$29`, IX, flags (including called COPY) |
| SKIPWS | `$16,$17,$20`, IX, flags |
| INDEX | `$9,$16..$18,$20..$27`, IX, flags |
Other register preservation claims require actual HD61 assembly and target execution corroboration; this is a source-level ABI candidate. In particular, pointer register clobber interactions with platform firmware must be assessed in integrated ROM.
## Behavior and edge cases
All methods verify required input NUL within declared cap and refuse zero capacity. `STR_COPY` copies length+1 including the terminator; `STR_APPEND` copies source including terminator to immediately after existing destination text. Overlapping spans use memmove-like behavior, with preflight before writes. On invalid termination or inadequate output capacity, destination is unchanged. For `STR_CMP`, lexical unsigned ASCII bytes are compared, and both input strings are fully validated before a comparison can succeed. For `STR_SKIPWS`, whitespace is only tab/LF/CR/space and start offset `0..length` is allowed. `STR_INDEX` is case-sensitive and returns the first zero-based substring match; empty needle matches offset zero.
## Minimal composition example
```asm
#INCLUDE (lib/str/str_abi.inc)
#INCLUDE (lib/str/str_copy.inc)
#INCLUDE (lib/str/str_append.inc)
; App must initialize UA, SY=1 and input registers before CAL STR_APPEND.
```
## Source and qualification
Source paths are `src/lib/str/*.inc`. Detailed clobbers and source contract in each header. Source unit tests in `src/tests/test_str_core_r002.py` ran 3,772 interpreter/oracle cases. Test ROM source is `src/str-test01.s`; independent capture checks require real owner bytes. No native assembler, PB-2000C, AI-1000, or physical hardware PASS yet.
## r003 native observation / source correction
The owner's first STR TEST01 emulator report is evidence that the r002b ROM assembled and ran, **not** evidence of behavioral PASS. Eleven cases disagree with the expected statuses or result bytes. `PSR SZ,0` binds the `$SZ` pseudo-register to `$0`; after the STR routine sets its status in `$0`, `LD $n,$SZ` reads that status instead of constant zero. A corrected source interpreter reproduces all 17 native-observed status/return pairs on the old source and all 17 expected outcomes on the revised source. All STR zero initializations now use `LD $n,0`; `SZ` need not point at a zero-valued register. Rebuild and actual emulator retest of r003 still open.