FA-89674 / Instruction set emulation / Member archive
SUB overflow computed with the ADD formula · case 04
Subtracting a negative from a positive that overflows is not flagged, and harmless subtractions are.
Case contract
Input [a, ops]; ops are 'inc', 'dec', ['add', v], ['sub', v] on 8-bit A; carry starts 0. After each op report [A, S, Z, H, PV, N, C]. inc: H = carry out of bit 3, PV = (A was 0x7F), N = 0, C unchanged. dec: H = borrow from bit 4 (low nibble was 0), PV = (A was 0x80), N = 1, C unchanged. add: H = carry out of bit 3, PV = signed overflow, N = 0, C = carry out. sub: H = borrow into bit 4 (low nibble of A < low nibble of v), PV = signed overflow, N = 1, C = borrow.
Why this case matters
Half-carry and parity/overflow flags are observable through DAA and conditional branches; emulators often get the INC/DEC special cases wrong.
One recorded failure
Sample boundary fixtureThis sample comes from the broken implementation of a controlled reproducer.
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| inc from ff | [[0, 0, 1, 1, 0, 0, 0], [255, 1, 0, 1, 1, 1, 1], [0, 0, 1, 1, 0, 0, 1]] | [[0, 0, 1, 1, 0, 0, 0], [255, 1, 0, 1, 0, 1, 1], [0, 0, 1, 1, 0, 0, 1]] | Failed |
MEMBER ARCHIVE
The complete case is available to members.
This record includes three runnable implementations, regression fixtures, execution results, and source hashes.
Member access is invitation-based. Sign in with your invited account to inspect the sources.
Sign in to the archive ↗