COREWAR

/
Step 10: Using the Subtraction Instruction "SUB"
🎯Use SUB to make Cell 0001 B-value equal to 7(0/7)

MISSIONS (30 STEPS)

CH.1: UI & MEMORY BASICS (1-6)
BASIC★☆☆
Step 1: The "STEP" Button That Advances Time
🔒
BASIC★☆☆
Step 2: Process Termination and Restoring with "RESET"
🔒
BASIC★☆☆
Step 3: "Edit Mode" vs. "Running Mode"EDIT
🔒
BASIC★☆☆
Step 4: The Three Rooms of a Memory Box (Changing B)EDIT
🔒
BASIC★☆☆
Step 5: Modifying Both the Left Room (A) and Right Room (B)EDIT
🔒
BASIC★☆☆
Step 6: Literal Numbers (#) vs. Relative Addresses ($)EDIT
CH.2: CORE INSTRUCTIONS (7-13)
🔒
ARITHMETIC★☆☆
Step 7: Observing the "ADD" Instruction
🔒
ARITHMETIC★☆☆
Step 8: Changing the Number to Add (A-Field)EDIT
🔒
ARITHMETIC★☆☆
Step 9: Changing the Target Address (B-Field)EDIT
🔒
ARITHMETIC★☆☆
Step 10: Using the Subtraction Instruction "SUB"EDIT
🔒
BASIC★☆☆
Step 11: Copying an Entire Box with "MOV"EDIT
🔒
CONTROL★☆☆
Step 12: Looping Back in Time with "JMP"EDIT
🔒
CONTROL★☆☆
Step 13: Build Your First Program (Automatic Counter)EDIT
CH.3: VON NEUMANN & DIGITAL LIFE (14-18)
🔒
BASIC★☆☆
Step 14: Why Does "DAT" Become a Bomb?EDIT
🔒
BASIC★☆☆
Step 15: Copying Your Own Instruction (Code = Data)EDIT
🔒
BASIC★☆☆
Step 16: Observation: Stepping Through "Imp", the Oldest Digital Organism
🔒
BASIC★☆☆
Step 17: Modifying the Imp (Copy Destination Experiment)EDIT
🔒
ARITHMETIC★★☆
Step 18: Understanding Indirect Addressing (@ Pointers)EDIT
CH.4: CLASSIC ARCHETYPES (19-24)
🔒
STRATEGY★★☆
Step 19: Observation: Dissecting the "Dwarf" Bomber Step-by-Step
🔒
STRATEGY★★☆
Step 20: Modifying Dwarf (Changing the Bombing Interval)EDIT
🔒
STRATEGY★★☆
Step 21: First Combat: Tune Your Bomb Interval to Destroy the TargetEDIT
🔒
CONTROL★★☆
Step 22: Observation: The "SPL" Split Instruction and Mitosis
🔒
CONTROL★★☆
Step 23: Finding Enemies with Conditional Skip "SEQ / CMP"EDIT
🔒
STRATEGY★★☆
Step 24: Stopping an Imp with a Pre-Decrement "Imp-Gate"EDIT
CH.5: BATTLE EVOLUTION (25-30)
🔒
STRATEGY★★☆
Step 25: [Stone] High-Speed Bomber and Step Size TuningEDIT
🔒
STRATEGY★★☆
Step 26: [Paper] Observing the Self-Replicating Swarm "Mice"
🔒
STRATEGY★★★
Step 27: [Modifying Paper] Tune Clone Dispersal Distance to Overwhelm StoneEDIT
🔒
STRATEGY★★☆
Step 28: [Scissors] Tuning Scanner Stride to Shred PaperEDIT
🔒
STRATEGY★★★
Step 29: [Trap] Abducting Enemy Processes with "Vampire"EDIT
🔒
STRATEGY★★★
Step 30: Graduation Exam: Tune Your Warrior and Win the Final BattleEDIT
🟢 EDIT MODE (Click cells to modify)
[Addr][Opcode][A (Left)][B (Right)]
📦 DATA / BOMB (DAT)
0000
▼MOV
#
$
👈 EDIT
0001
▼MOV
<
<
0002
↺JMN
$
$
0003
❖SPL
@
#
0004
+ADD
#
$
0005
↺JMP
$
$
0006
■DAT
$0
$0
++ ADD ROW
0007
■DAT
$0
$0
0008
■DAT
$0
$0
0009
■DAT
$0
$0
0010
■DAT
$0
$0
0011
■DAT
$0
$0
0012
■DAT
$0
$0
0013
■DAT
$0
$0
0014
■DAT
$0
$0
0015
■DAT
$0
$0
0016
■DAT
$0
$0
0017
■DAT
$0
$0
0018
■DAT
$0
$0
0019
■DAT
$0
$0
0020
■DAT
$0
$0
0021
■DAT
$0
$0
0022
■DAT
$0
$0
0023
■DAT
$0
$0
0024
■DAT
$0
$0
0025
■DAT
$0
$0
0026
■DAT
$0
$0
0027
■DAT
$0
$0
🏟️ARENA (20 ROWS)SCOPE: 0000 - 0019
[CORE MEMORY 800 CELLS (20 ROWS × 40 COLS)]1:1 Row Synced with Editor
TASKS: 0
CYC: 0
📖 LECTURE NOTE // STEP 10 OF 30Chapter 2: Core Instructions & Autonomous Loops⏱ 5 min read
📖 Lecture Note: Computer Science & Artificial Life Principles Behind This Experiment

Subtraction (SUB) and Modular Arithmetic in a Finite Ring Universe

Switching opcodes in the ALU and understanding how numbers wrap around in CoreWar's circular modular arithmetic

#SUB (Subtract)#Opcode Selection#ALU Control Signal#Modular Arithmetic#Circular Core
🔬Step 10: Using the Subtraction Instruction "SUB"

You can click the Opcode dropdown to switch instructions: - ADD: Addition - SUB: Subtraction

Right now, Cell 0001 holds 10, and Cell 0000 is ADD.AB #3, $1.

🎯 Your Task Change the highlighted Opcode (ADD) at Address 0000 to SUB, then press [STEP] to subtract 3 from 10 and make Cell 0001's B-value 7!

1. The Opcode as an ALU Function Selector Switch

In Step 10, you opened the leftmost room of Address 0000—the Opcode selector—and switched the instruction from "ADD" (Addition) to "SUB" (Subtraction), subtracting 3 from 10 at Address 0001 to produce 7.

Compare the two instructions side by side: "ADD.AB #3, $1" and "SUB.AB #3, $1". Their modifier (.AB), source operand (#3), and destination operand ($1) are 100% identical. Only the Opcode changed.

From a hardware engineering perspective, both instructions use the exact same data path: the CPU fetches 3 from 0000.A and 10 from 0001.B, feeds both numbers into the Arithmetic Logic Unit (ALU), and writes the output back into 0001.B. The Opcode simply acts as a multi-way control switch telling the ALU which internal circuit to activate—addition (+) or subtraction (-). In hardware design, an ALU contains both an adder and logic gates wired in parallel, and a 4-bit or 5-bit binary Opcode field feeds into a multiplexer that selects which arithmetic result is latched back into memory at the end of the clock cycle.

Inputs: A = 3, Target B = 10
  ├─ If Opcode == ADD ──► ALU computes 10 + 3 = 13
  └─ If Opcode == SUB ──► ALU computes 10 - 3 = 7  ──► Write 7 to [0001].B

2. What Happens When You Subtract 1 from 0? The Secret of Modular Arithmetic

In this mission, subtracting 3 from 10 yielded a straightforward positive integer: 7. But what happens in a computer when you subtract a larger number from a smaller one—for example, subtracting 1 from 0?

In everyday arithmetic, 0 - 1 is -1 (and our editor conveniently displays negative relative offsets like $-1 for readability). Underneath the surface, however, the CoreWar universe (MARS) operates on strict Modular Arithmetic over a finite circular ring of size N (where N = 800 cells in the Academy, and N = 8,000 cells in the Laboratory).

Think of the face of a 12-hour clock. If the hour hand is at 0 (12 o'clock) and you subtract 1 hour, you do not land on "-1 o'clock"—you wrap around backward to 11 o'clock. In the exact same way, every number in an 800-cell CoreWar core wraps around modulo 800: subtracting 1 from 0 yields 799 (which, in a circular ring of 800 cells, is the cell immediately above 0000!). In abstract algebra, this mathematical structure is known as the finite cyclic ring Z/800Z, where addition, subtraction, and multiplication are always closed within the set {0, 1, 2, ..., 799} and integer overflow is impossible.

3. Two's Complement: How Computers Unify Addition and Subtraction

This equivalence—where moving backward by 1 (-1) on a circular ring arrives at the exact same destination as moving forward by N-1 (+799)—is not unique to CoreWar. It is the exact mathematical foundation of Two's Complement arithmetic used by every modern digital processor on Earth.

Inside a physical CPU, there is actually no separate hardware circuit built from scratch for subtraction. Instead, when executing "B - A", the ALU converts A into its modular additive inverse (in binary, inverting all bits of A and adding 1) and feeds the numbers through the standard addition circuit!

In CoreWar tactics, the SUB instruction is essential whenever you want a pointer to sweep backward through memory (moving upward toward lower addresses) or when counting a loop variable down toward zero. Because the memory core is a closed circular torus, sweeping backward from Address 0000 immediately wraps around to the highest addresses of the arena (0799, 0798, 0797...), allowing your program to sneak up on an enemy from behind! Furthermore, combining ADD and SUB within the same warrior allows you to manipulate two pointers in opposite directions simultaneously, creating pincer attacks that close in on targets from both sides of the circular ring.

🔒

🔒 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.