BRK

In 6502 assembly language programming, BRK is an opcode that causes a software interrupt or trap. The generalized actions of BRK are as follows:
* The B bit in the processor status register is set to indicate that a BRK instruction is being processed.
* The program counter is incremented by two and then pushed on to the stack, most significant byte first.
* The status register is pushed on to the stack.
* Maskable interrupts (IRQ) are disabled by setting the I bit in the status register.
* The program counter is loaded with the address stored in memory address 0xFFFE (LSB) and 0xFFFF (MSB—the 6502 is little endian), and execution is continued at that point.

Note that neither the accumulator or index registers are pushed on to the stack. Also, the vector (0xFFFE) through which the processor jumps in response to BRK is the same as that for an interrupt request, requiring that the IRQ service routine get a copy of the status register from the stack to check the B bit so as to determine whether the interrupt was hardware or software.

In the original 6502, the simultaneous assertion of a hardware interrupt line and execution of BRK was not accounted for in the design—the BRK instruction will be ignored in such a case. Also, the status of the decimal mode flag in the processor status register was preserved, which could potentially result in a difficult to locate bug in the interrupt handler if decimal mode happened to be enabled at the time of BRK being executed. This was fixed in the CMOS versions of the processor.

One popular use of the BRK instruction is as a debugging aid. The machine instruction comprises just a single byte, with the hexadecimal value 00. Thus it can be used to manually insert a breakpoint at any point in a program just by overwriting a single byte at that point with the value 00. Of course, one has to adjust the return address and restore the overwritten code before returning from the debugger to the program. One useful technique, advocated by Kuckes and Thompson, is to liberally sprinkle one's code with NOP instructions that can be replaced by BRK instructions without altering the actual behaviour of the program being debugged.
 
< Prev   Next >