Z80 Assembly 10: Interrupt Modes and Handling External Events

What is an Interrupt? An interrupt is an external signal from a hardware device (like a timer or keyboard controller) that tells the CPU to immediately stop what it’s doing and execute a specific routine to handle the event. This is how the system maintains responsiveness. The Two Key Commands: Instruction Action Purpose EI Enable Interrupts Allows the CPU to listen for and respond to external interrupt requests. DI Disable Interrupts Prevents the CPU from responding to external requests (used for critical code sections). The routine that handles the interrupt must end with RETI (Return from Interrupt) or RETN (Return from Non-Maskable Interrupt) instead of a simple RET. ...

September 27, 2025

Z80 Assembly 09: BCD Arithmetic and the RST (Restart) Interrupts

BCD Arithmetic: The DAA Instruction Binary-Coded Decimal (BCD) is a way to store numbers where each byte holds two decimal digits (0-9). This is vital for financial or numerical applications where decimal accuracy is required, as standard binary math can introduce rounding errors when converting to decimal. The Problem: If you add 9 and 1 in binary, the result is 10 (0A Hex). In BCD, the result should be 10 (10 Hex). ...

September 27, 2025

Z80 Assembly 08: Multi-Byte Math and the Alternate Registers

Arithmetic with Carry: ADC and SBC When adding numbers larger than 8 bits (like a 16-bit word or a 32-bit integer), you must add the Carry flag (C) from the previous operation into the current one. This is done with the ADC and SBC instructions. The Principle: Add (or Subtract) the current byte PLUS the Carry flag. Instruction Action Purpose ADC A, R A ← A + R + C Add with Carry: Used to link the result of the previous 8-bit addition. SBC A, R A ← A - R - C Subtract with Carry/Borrow: Used to link the result of the previous 8-bit subtraction. Example: 16-bit Addition (BC + DE) ...

September 27, 2025

Z80 Assembly 07: Block Transfers and String Operations

High-Speed Block Transfer Instructions The Z80 includes a powerful set of instructions designed to handle large blocks of memory quickly. These commands primarily rely on three 16-bit register pairs: Register Pair Purpose in Block Commands HL Source Address (Where to read data from) DE Destination Address (Where to write data to) BC Counter (How many bytes to move/compare) Transferring Data (Move) The LDI (Load, Increment) and LDIR (Load, Increment, Repeat) commands are used to copy blocks of memory. ...

September 27, 2025

Z80 Assembly 06: Rotation and Shifts (Fast Multiplication/Division)

Understanding Shifts and Rotations Shift and rotate instructions move the bits within a register. This is often done to perform arithmetic: shifting a number left by one position is equivalent to multiplying by 2, and shifting right is equivalent to integer dividing by 2. Key Difference: Shift: The bit that leaves one end is discarded, and a new bit (0 or the sign bit) enters the other end. Rotate: The bit that leaves one end wraps around to enter the other end, forming a circle. Arithmetic Shifts (SLA and SRA) Arithmetic shifts are best for multiplication and division because they handle the sign of the number correctly. ...

September 27, 2025

Z80 Assembly 05: Logic Operations and Bit Manipulation

Logic Operations: AND, OR, and XOR These instructions perform bitwise logic operations between the Accumulator (A) and a source (another register or immediate value), storing the result back in A. They are crucial for filtering or combining specific data flags. The Core Logic Commands: Instruction Action Purpose AND N Bitwise AND (A = A & N) Filtering/Masking: Clears (forces to 0) any bits in A that are 0 in N. OR N Bitwise OR (A = A | N) Setting: Sets (forces to 1) any bits in A that are 1 in N. XOR N Bitwise XOR (A = A ^ N) Toggling: Flips the state (0→1, 1→0) of any bits in A that are 1 in N. Example: Filtering a Status Register If we only care about the highest two bits of a status register read into A: ...

September 27, 2025

Z80 Assembly 04: Memory, I/O Ports, and the Index Registers

Memory Management with 16-bit Pointers The Z80 is excellent at handling 16-bit memory addresses. The most common pairs for this are HL, DE, and BC. Moving Data to/from Memory: Remember that parentheses () mean “the contents of the address.” Instruction Action Analogy LD A, (HL) Loads the byte at the address in HL into A. Reads the note at the address in your address book. LD (DE), A Stores the byte in A into the address in DE. Writes a note at the address in your address book. Stepping Through Memory: ...

September 27, 2025

Z80 Assembly 03: Subroutines and The Stack (Saving Your Work)

Subroutines: Reusing Code A subroutine is a block of code designed to perform a specific task that can be called from anywhere in your program. This is how you avoid writing the same code repeatedly. The Two Commands: Command Action Analogy CALL NNNN Jumps to the address NNNN (the subroutine). Go to another room to perform a task. RET Jumps back to the instruction immediately following the CALL. Return to where you were before the task. The Stack: The LIFO Scratchpad The Stack is an area of memory managed by the Stack Pointer (SP). When you call a subroutine, the Z80 automatically uses the stack to save the return address. ...

September 27, 2025

Z80 Assembly 01: Your First Step and the Register Crew

Your CPU’s Workspace: The Registers Think of the Z80’s registers as a small, specialized crew of fast workers who hold all your data. They are faster than memory, so we want them to do most of the work. The Main Workers (8-bit) Register Analogy Key Function A (Accumulator) The Calculator Where math always happens. If you add, subtract, or compare, it happens here. B, C, D, E, H, L The General Storage Used to hold temporary numbers or counts. The Main Address Book (16-bit) ...

September 27, 2025

Z80 Assembly 02: Arithmetic, Flags, and Control Flow

Arithmetic and the Flags Register Operations like ADD and SUB affect the Flags Register (F), which is essential for conditional execution. Key Flags: Z (Zero): Set if the result of an operation is zero. C (Carry): Set if there is a carry-out from the most significant bit. Instruction Description ADD A, R Adds the 8-bit contents of R to A. Only A can be the destination. ADD HL, DE Adds the 16-bit contents of DE to HL. Controlling Execution with Jumps Jumps (changing the Program Counter) allow programs to make decisions and repeat blocks of code. ...

September 27, 2025