Files
SplitBit-Emulator/Programs/Libraries/int8.asm
T
Anachronaut e1273337c4 Two mechanical fixes SplitLint found: MVQA, and RSTA for zero
Twenty four places moved Q into A or B by pushing it and popping it back.
That is four bus cycles and two bytes to do what MVQA does in one of each,
and several of them are inside loops - Life, the calculator, int8. Nineteen
more loaded zero with INIA 0d0 where RSTA says the same thing in one byte.

Both are equivalent at the CPU rather than by assertion: RSTA and INIA both
leave Status alone, and PSHQ followed by POPA nets to A = Q with the Stack
Pointer where it started. The one difference is that the pair leaves a copy
of Q in memory just below the Stack Pointer and MVQA does not, which
nothing here reads.

Five recorded outputs moved and every one of them says the change worked:

- 16x16Life fits five more generations into the same cycle budget, the
  first 457 lines identical, because the loop got cheaper.
- Life.sbx is 1409 bytes rather than 1411, in three tests that list it.
- Edit.sbx is 1995 rather than 1996.

That last one broke a check I added this morning, and the hole is worth
recording: the CosmOS README's claim about Edit's size did not have the
word "Edit" on the same line as the number, because the subject was in the
sentence before, so the check that measures quoted sizes skipped it
silently. The sentence now names what it is talking about, which makes it
both checkable and clearer, and the check fails on a wrong number there.

Comments on either half of a replaced pair are carried onto the
instruction that replaces them, so nothing anybody wrote was lost.
2026-08-26 17:53:10 -04:00

85 lines
2.6 KiB
NASM

; This file supplies basic support for int8 values.
#Program
; Basic int8 subroutines.
; An int8 value is an 8 bit unsigned integer. Just a byte literally interpreted as a value.
; Basic operations on int8 values use A and B as operands with the result returned in Q, similar to the bare arithmetic instructions of SplitBit.
int8mult:
; Multiply A by B and return the result in Q.
; This subroutine is limited to outputs in the range of 0-255.
; It does not reliably return with the Carry Flag set if the result is too large to fit in the range.
; First, check if either is zero, and if so, return zero.
BRB return0
BRA return0
PSHB ; Push the multiplier to the stack.
PSHA ; Push the multiplicand to the stack.
POPB ; Pop the multiplicand into B.
RSTA ; Set A to 0.
CCF ; Clear the Carry Flag, just in case.
int8multLoop:
ADD ; Add the multiplicand to A.
POPA ; Pop the multiplier into A.
DECA ; Subtract one from the multiplier.
BRA int8multDone ; If the multiplier becomes zero, we're done.
PSHA ; Push the multiplier back to the stack.
MVQA ; Push the running total to the stack. Pop the running total into A.
BRI int8multLoop
return0:
RSTA
RSTB
ADD
int8multDone:
RET
int8div:
; Divide A by B and return the quotient in Q.
; First, check if either A or B are 0, if so, return 0.
BRB return0
BRA return0
; This dance of pushes, pops, and addition creates a quotient counter on the Stack while preserving A and B.
PSHA ; Push A to the Stack.
PSHB ; Push B to the Stack.
RSTA ; Set A and B to 0.
RSTB
ADD ; Add them together, setting Q to 0.
POPB ; Restore B.
POPA ; Restore A.
PSHQ ; Push a 0 onto the Stack.
; The Carry Flag is clear and we can proceed with the division.
int8divLoop:
SUB ; Subtract B from A.
; If we generate a carry we're done.
BRC int8divDone
; Otherwise, increment the quotient counter and subtract again.
POPA ; Pop the quotient counter from the stack.
INCA ; Increment it.
PSHA ; Push it back onto the stack.
MVQA ; Push the running total onto the stack. Pop the runing total into A.
BRI int8divLoop ; Branch back to the loop.
int8divDone:
CCF ; Clear the Carry Flag.
POPA ; Pop the quotient into A.
RSTB ; Set B to 0.
ADD ; Add them together to move the result to Q.
RET ; Return to the caller.
int8mod:
; Divide A by B and return the remainder in Q.
; If A or B are 0, return 0.
BRB return0
BRA return0
int8modLoop:
SUB ; Subtract B from A.
BRC int8modDone
MVQA
BRI int8modLoop
int8modDone:
; Add the divisor to Q to get the remainder.
CCF
MVQA
ADD
RET