LIB2 / FILE VIEW

b9_sgn.inc

DOWNLOAD ORIGINAL ↓

; =====================================================================
; B9 SGN r001 -- NON-QUALIFIED SOURCE CANDIDATE, 2026-11-02
; Self-contained numeric result, no PB-2000C math firmware calls.
; Donors: existing B9_VALIDATE (b9_convert.inc) and B9_ZERO
;         (b9_value.inc); neither donor is edited.
;
; Contract (candidate, subject to owner interface review):
;   Input:   $10/$11 -> 9-byte source B9 value in UA=&H14 RAM.
;   Output:  $24/$25 -> 9-byte writable destination in UA=&H14 RAM.
;   Result:  9-byte canonical B9 -1 / 0 / +1 on valid input.
;   Status:  $0=0 valid, $0=1 invalid B9; on error no output write.
;   Source:  unchanged for disjoint source/destination; exact alias OK.
;   Overlap: only disjoint buffers or exact alias supported.
;   Precondition: $SX=0, $SY=1; valid stack and RAM bank.
;   UA after call: &H14. Flags and $0..$21 may be clobbered.
;   Pointer $24/$25 is unchanged, by a version-pinned inspection of
;   B9_VALIDATE and B9_ZERO (not by existing published ABI promise).
;   Nonreentrant: inherits B9 converter's 2E40h..2EC4h scratch.
;
; Dependencies: include b9_convert.inc AND b9_value.inc once each.
; Source-level assumptions require native assembly and emulator tests.
; +1 B9 = 00 00 00 00 00 00 01 00 01
; -1 B9 = 00 00 00 00 00 00 01 00 06
; Zero  = 00 00 00 00 00 00 00 00 00
; =====================================================================
B9_SGN:
        PST     UA,&H14
        CAL     B9_VALIDATE
        AN      $0,$0
        JP      NZ,_B9S_RET     ; malformed: return, output unchanged
; $1=0 zero, 1 positive, 2 negative from B9_VALIDATE.
; Input has been fully inspected; now zero destination canonically.
        LDW     $10,$24
        CAL     B9_ZERO
        AN      $1,$1
        JP      Z,_B9S_OK
; Byte 6 is the normalized unit mantissa 01.
        LDW     $2,$24
        LDW     $4,6
        ADW     $2,$4
        PRE     IX,$2
        LD      $16,1
        ST      $16,(IX+$SX)
; Byte 8 contains biased exponent hundreds and signed code.
        LDW     $2,$24
        LDW     $4,8
        ADW     $2,$4
        PRE     IX,$2
        LD      $16,1           ; B9 +1
        LD      $17,$1
        SBC     $17,2
        JP      NZ,_B9S_STORE
        LD      $16,6           ; B9 -1
_B9S_STORE:
        ST      $16,(IX+$SX)
_B9S_OK:
        LD      $0,0
_B9S_RET:
        RTN