167. Instruction bne
implement Branch if Not Equal behavior.
Lesson 167 of 356 · tests/chapter_01_cpu/test_167_instruction_bne.py
Why this step exists
Zero clear records a non-equal result from an earlier operation, so BNE can branch without performing another comparison.
In this step, add exactly this implementation
`emulator/cpu/instructions.py::bne`::
def bne(cpu: CPU, offset: int):
if not cpu.flags.get_zero_flag():
cpu.pc = (cpu.pc + offset) & 0xFFFFThe function applies the signed displacement to the already advanced PC and constrains the target to 16 bits.
Invariants: Zero clear takes the branch, including wrapping underflow or overflow; Zero set leaves PC unchanged. Flags, other registers, and memory remain untouched. Misconception: BNE tests Zero, not Negative, and does not perform a comparison or fetch an operand.
Out of scope: BPL/BMI/BVC/BVS are lessons 168-171. Branch opcode imports, handlers, and table entries are deferred to lessons 172-179.
Run this lesson
uv run pytest tests/chapter_01_cpu/test_167_instruction_bne.py -v