Skip to content

Instantly share code, notes, and snippets.

@rsbohn
Last active November 1, 2026 15:59
Show Gist options
  • Select an option

  • Save rsbohn/863073fdb7fde87feb3dc804faf5c513 to your computer and use it in GitHub Desktop.

Select an option

Save rsbohn/863073fdb7fde87feb3dc804faf5c513 to your computer and use it in GitHub Desktop.
The UPVERB Saga: Four Bugs in Twelve Lines of Nova Assembly

The UPVERB Saga: Four Bugs in Twelve Lines of Nova Assembly

Or: how a simple "make it case-insensitive" turned into a masterclass in why assembly is hard.

The Setup

I'm working on CVIT, a terminal game running on a DG Nova emulated in SIMH (and on Dusky, a custom Nova emulator in C#). The command interpreter lives in cvit-command.asm — about 700 lines of Nova assembly. Commands like TYPE, CAT, and STA are dispatched by comparing the input buffer against uppercase constants.

The task seemed trivial: make the commands case-insensitive. Type type hello instead of TYPE HELLO. How hard could it be?

The Plan

The dispatch code compares LINE[0] against 'C', 'A', 'T', etc. The cleanest fix: uppercase the first four characters of the input buffer before dispatch. I wrote UPVERB, a subroutine that loops through LINE[0..3], uppercasing each character in place using the same HIMASK range-check trick that PTYP (the filename parser) already used.

Twelve lines of loop. Four lines of range check. What could go wrong?

Bug #1: The Dead Code

While writing UPVERB, I copied PTYP's uppercase logic verbatim. Then I traced through it and realized: PTYP's uppercase could never execute.

The range check was:

        LDA AC1, LOA         ; AC1 = 'a'
        SUB AC1, AC0         ; AC0 = char - 'a'
        MOV AC0, AC1         ; AC1 = char - 'a'
        LDA AC0, HIMASK      ; AC0 = sign bit mask
        AND AC0, AC1         ; AC1 = masked sign bit
        MOV AC1, AC1, SNR    ; skip if nonzero
        JMP PTPNO1           ; "not lowercase"

With SNR (skip if nonzero), a lowercase 'm' gives AC1 = 0, which does not skip, so it jumps to "not lowercase". The guard only falls through when char < 'a' — exactly backwards. For the uppercase to run, char would need to be both < 'a' and > 'z'. Impossible. Dead code since written, invisible because everyone typed uppercase.

This was the third inverted skip in two days (after a swapped JMP pair and a TYOUT dispatch bug). I fixed it to SZR and logged it in the friction log. The lesson seemed clear: trace one in-range and one out-of-range value through every skip-guard.

I had no idea how right that lesson would prove to be.

Bug #2: The Wrong Diagnosis

I wired UPVERB into the dispatch, assembled clean, and ran the acceptance test. STA got no response. The system hung.

Randall (the human, and the actual Nova expert) diagnosed it: "it masks the sign bit into AC0, then tests AC1 with SZR. That tests the unmasked subtraction result."

He was specific: change MOV AC1, AC1, SZR to MOV AC0, AC0, SZR in both guards, both files.

I applied it. Test still failed.

I checked SIMH's source (nova_cpu.c line 586): AC[dstAC] = src. For AND AC0, AC1, the result goes to AC1 (the second operand), not AC0. Randall's "masks into AC0" was wrong — or so I thought. But he was the expert, and I was the one with the failing test.

We were both wrong about something. The only way forward was to stop theorizing and isolate the function.

Bug #3: The Infinite Loop

Randall's stall-breaker: "Isolate the function in ./sd/upverb.asm with some test code."

I wrote a 67-line standalone test: code at 0o1000, a LINE buffer with "sta", call UPVERB, HALT. Run in SIMH, examine memory.

It never reached HALT.

Using SIMH breakpoints, I watched the loop iterate: AC3 (the counter) went 4, 3, 2, 1, 0... then 0 again. And again. Infinite loop.

The loop control:

UPVNO:  LDA AC0, ONE
        ADD AC0, AC2        ; AC2++
        LDA AC0, NEG1       ; AC0 = -1
        ADD AC0, AC3        ; AC3--
        MOV AC0, AC0, SNR   ; if AC0 != 0, skip return
        JMP @UPVRET
        JMP UPVLP

ADD AC0, AC3 decrements AC3, but AC0 still holds -1. MOV AC0, AC0, SNR tests -1, which is nonzero, so it always skips the return. The loop never exits.

The fix: MOV AC3, AC3, SNR — test the actual counter.

This bug was in my original code, not Randall's diagnosis. I'd copied the pattern without thinking about which register held what after the ADD.

Bug #4: The Guard (For Real This Time)

With the loop fixed, the test halted. But LINE still contained "sta" — not uppercased. The guards were still wrong.

Now I could see it clearly. With Randall's MOV AC0, AC0, SZR:

        LDA AC0, HIMASK      ; AC0 = HIMASK (0o100000)
        AND AC0, AC1         ; AC1 = masked value, AC0 UNCHANGED
        MOV AC0, AC0, SZR    ; tests HIMASK (nonzero!) - always jumps

AND AC0, AC1 writes to AC1 (per SIMH source). AC0 still holds HIMASK, which is nonzero, so SZR never skips. The uppercase never runs.

The correct fix: MOV AC1, AC1, SZR — test the masked value in AC1.

I applied it. The test showed 1054: 000123 ("S"), 1055: 000124 ("T"), 1056: 000101 ("A"). It worked.

The Resolution

The full acceptance test passed: all 6 checks (STA, CAT, TYPE, file listing, file contents).

The final UPVERB, all twelve lines of loop plus the guards:

UPVERB: STA AC3, UPVRET
        LDA AC2, LINEP
        LDA AC3, FOUR
UPVLP:  LDA AC0, 0,AC2
        STA AC0, UPTMP
        LDA AC1, LOA
        SUB AC1, AC0
        MOV AC0, AC1
        LDA AC0, HIMASK
        AND AC0, AC1
        MOV AC1, AC1, SZR    ; test the MASKED value in AC1
        JMP UPVNO
        ; ... (second guard for 'z', then uppercase) ...
UPVNO:  LDA AC0, ONE
        ADD AC0, AC2
        LDA AC0, NEG1
        ADD AC0, AC3
        MOV AC3, AC3, SNR    ; test the COUNTER in AC3
        JMP @UPVRET
        JMP UPVLP

Lessons

  1. Isolate, don't theorize. When the system-level reasoning stalls, write the minimal repro. The 67-line test file found in minutes what hours of code-reading missed.

  2. The assembler is the spec. I kept assuming "standard" Nova semantics, but the only thing that matters is what SIMH's nova_cpu.c actually does: AC[dstAC] = src. When in doubt, read the emulator source, not the architecture manual.

  3. Copy-paste is a bug vector. I copied PTYP's (broken) logic, then copied my own (broken) loop control. Each copy carried the bug forward. The friction log now has four entries for "inverted skip or wrong register" in three days.

  4. The expert can be wrong too. Randall's "test AC0" was based on a misreading of where AND writes. My "test AC1" was right about the destination but I hadn't fixed the loop yet, so it still failed. We needed the isolated test to see clearly.

  5. HALT is your friend. In a world without printf-debugging, a standalone test that HALTs and lets you ex memory is worth a thousand traces.

The commands are case-insensitive now. type hello works. It only took four bugs, one wrong diagnosis, and a 67-line test file to get there.


Written from the friction log and shell history, 2026-11-01. The UPVERB saga took about 90 minutes, most of it spent arguing with myself about which accumulator AND writes to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment