Immediate Values (#) vs. Relative Addresses ($): The Birth of Position-Independent Code
Why programs say "2 rows down ($2)" instead of "Address 0002": the secret behind code that can survive anywhere in memory
Notice the symbol (# or $) right next to each number! This is called the Addressing Mode: - # (Immediate Value): #9 means the literal number 9. - $ (Direct Relative Address): $1 means the location 1 box down from the current line.
Right now, Address 0000 says MOV.AB #9, $1 (send the number 9 to the box $1 = 1 row down), and a green arrow points to 0001.
🎯 Your Task Change the B (Right Value) of 0000 from $1 to $2 (2 boxes down = 0002), watch the green arrow move to 0002, and press [STEP] so Cell 0002's B-value becomes 9!
1. The Crucial Distinction: A Literal Number (#) vs. A Box Location ($)
To conclude Chapter 1, Step 6 introduces one of the most transformative concepts in computer architecture: Addressing Modes. For anyone learning low-level programming, understanding the difference between "#" and "
quot; is the exact moment when machine code stops looking like cryptic symbols and starts making complete logical sense.Even when the digit is the same (such as 2), prefixing it with "#" versus "
quot; changes its meaning to the CPU entirely: - "#2" (Immediate Mode) means the literal number 2 itself. The CPU does not look at any other memory box; it simply uses the constant value 2 written right inside the instruction. - "$2" (Direct Relative Mode) means the memory box located 2 rows down from the currently executing instruction. Here, 2 is not a quantity to be copied—it is a spatial coordinate (an address offset) telling the CPU which box to open.When you edited row 0000 to read "MOV.AB #9, $2", you instructed the CPU: "Take the literal number 9 (#9), and place it into the memory box located 2 rows ahead ($2 = Address 0002)." That is why the green target arrow in the workbench stretched down from 0001 to 0002 the instant you changed $1 to $2.
[0000] MOV.AB #9, $2
│ └──► $2 (Direct Relative Address): "Target the box 2 rows down (0002)"
└───────► #9 (Immediate Literal Value): "Send the literal number 9"2. Why Relative Offsets ($) Instead of Absolute Addresses?
A perceptive student might ask: "If we want to write to Address 0002, why say '2 boxes down from here ($2)'? Why not just write the absolute address '0002' directly?"
Absolute addresses work fine only if your program is guaranteed to live at Address 0000 forever. But think about what happens in a real CoreWar battle—or inside a modern operating system like Linux, macOS, or Windows. When a match begins in the Arena, the MARS loader places your warrior at a random starting address (say, Address 3412) so enemies cannot pre-calculate where you are. Furthermore, self-replicating programs constantly copy themselves to brand-new locations across memory.
If your instruction used an absolute address ("Write to 0002"), then a warrior spawned at Address 3412 would fire its data all the way back to 0002 instead of updating its own data box two rows below at 3414!
By expressing every address as a relative offset from the current instruction ("$2" = current PC + 2), your code becomes completely independent of where it sits in memory. Whether your instruction is at 0000 (0000 + 2 = 0002) or at 3412 (3412 + 2 = 3414), "$2" always points to the box two rows below itself.
3. Direct Connection to Modern OS Security: PIC and ASLR
In computer science, machine code that can execute properly regardless of where it is loaded in memory is called Position-Independent Code (PIC).
In fact, almost every application and shared library (.dll, .so, .dylib) running on your computer or phone today is compiled using the exact same PC-relative addressing principle you just used in Step 6. Modern operating systems rely on a critical security defense called ASLR (Address Space Layout Randomization), which randomly shuffles the memory addresses where programs are loaded every time they launch so that attackers cannot guess where specific functions reside.
Because programs are randomized in memory by ASLR—just like Warriors are randomly placed in the CoreWar Arena—modern CPUs (such as x86-64 with RIP-relative addressing and ARM64) use relative offsets extensively. By mastering "#" and "
quot; here in Chapter 1, you have grasped the foundational principle of both mobile digital organisms and modern operating system security.🔒 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.