The Arithmetic Logic Unit (ALU) and the Read-Modify-Write Micro-Cycle
Behind a single "ADD" instruction lies a three-phase round trip between memory cells and the processor's Arithmetic Logic Unit
In Chapter 2, we'll learn the core instructions that manipulate memory. First, let's observe an example of the ADD instruction!
Address 0000 contains ADD.AB #2, $1: - A (Left Value): The number to add (#2) - B (Right Value): Where to add it ($1 = 1 box down at 0001) - The data box at 0001 starts with a B-value of 3.
🎯 Your Task (No Editing Needed) Press [STEP] once and watch the B-value of 0001 increase from 3 to 5 (3 + 2)!
1. The Mathematical Brain of the CPU: The ALU
Opening Chapter 2, Step 7 allowed you to observe the most fundamental arithmetic instruction in computing: "ADD.AB #2, $1". With a single click of the [STEP] button, the B-value inside the data box at Address 0001 transformed from 3 into 5.
Inside every physical or virtual processor, the hardware circuit responsible for actually performing math like "3 + 2 = 5" is called the ALU (Arithmetic Logic Unit). If the Program Counter (PC) is the navigator pointing to which line of code runs next, the ALU is the workshop floor where numbers are combined, subtracted, and compared.
Crucially, in a Von Neumann computer, memory cells (RAM) have no ability to do math on their own—they are passive storage boxes. How, then, does executing "ADD.AB #2, $1" at Address 0000 change the number stored inside Address 0001?
[0000] ADD.AB #2, $1 ──(1. Read Source A = 2 & Target B at 0001 = 3)──► ┌─────────┐
│ ALU │ (3 + 2 = 5)
[0001] DAT.F #0, #5 ◄────────(2. Write Result 5 back to 0001.B)────────┘─────────┘2. The Read-Modify-Write Sequence
On screen, the number 3 at Address 0001 appears to turn into 5 instantaneously. Under the hood of the MARS virtual machine, however, modifying a memory cell requires a classic three-phase hardware sequence known as Read-Modify-Write (RMW):
1. READ: The processor extracts the source value (#2) from the A-field of Address 0000. It then resolves the destination address ($1 = Address 0001), opens Cell 0001, and reads the current value of its B-field (3) into the ALU. 2. MODIFY: The ALU adds the two inputs together (3 + 2), producing the result 5. 3. WRITE: The processor writes the newly computed value (5) back into the B-field of Address 0001, overwriting the previous value of 3 in place.
Because ADD performs a complete Read-Modify-Write cycle rather than a blind overwrite, the prior state of Cell 0001 matters: starting with 3 and adding 2 yields 5. This ability to accumulate state over time is what allows memory cells to act as variables and counters. In multi-processor computer systems, hardware engineers must take special care to ensure that Read-Modify-Write cycles are executed atomically (indivisibly) so that no other processor can alter the memory cell between the Read step and the Write step. In MARS, every instruction's Read-Modify-Write sequence completes atomically within a single clock tick.
3. How the ".AB" Instruction Modifier Routes Data Between Rooms
Take a closer look at the two-letter suffix ".AB" in "ADD.AB". Because every memory box in CoreWar contains two number rooms (A and B), an instruction must specify which room to read from and which room to write into.
In the ICWS-94 standard, the two letters of the modifier act as a routing switchboard: - The first letter ("A" in .AB) tells the CPU to read the number from the A-field (left room) of the source. - The second letter ("B" in .AB) tells the CPU to add that number into the B-field (right room) of the destination cell.
Other modifiers like ".A" (left-to-left), ".B" (right-to-right), ".BA" (right-to-left), and ".F" (both rooms in parallel) give Redcode programmers surgical control over how data flows between rooms. In traditional microprocessors, this routing is handled by multiplexers (MUX circuits) that select which register bits enter the ALU and which byte lanes are enabled during a memory write. By exposing these modifiers explicitly in the instruction syntax, Redcode-94 lets you see the exact internal wiring of the processor on every line of code. Whenever you see ".AB", visualize a diagonal wire connecting the left room of the source directly into the right room of the destination.
🔒 MISSION CLEAR Unlocks the Full Lecture Note
Complete the experiment in the simulator above to automatically unlock the full deep-dive article—or click below to read the full text immediately.