Memory Operations
Understanding Memory
Memory is where programs store data that doesn't fit in registers. Our compiler simulates 1024 memory locations, each capable of holding one integer value.
LDR - Load from Memory
LDR loads a value from memory into a register:
LDR Rd, address
Example:
LDR R0, 100 // Load value from memory[100] into R0
STR - Store to Memory
STR stores a register value into memory:
STR Rs, address
Example:
MOV R0, #42
STR R0, 100 // Store R0's value (42) to memory[100]
Complete Example
// Store and load example
MOV R0, #100 // R0 = 100
STR R0, 50 // memory[50] = 100
MOV R0, #0 // Clear R0
LDR R1, 50 // R1 = memory[50] = 100
HALT
Using Memory for Data Storage
// Store multiple values
MOV R0, #10
STR R0, 0 // memory[0] = 10
MOV R0, #20
STR R0, 1 // memory[1] = 20
MOV R0, #30
STR R0, 2 // memory[2] = 30
// Load and sum them
LDR R1, 0 // R1 = 10
LDR R2, 1 // R2 = 20
LDR R3, 2 // R3 = 30
ADD R4, R1, R2 // R4 = 30
ADD R4, R4, R3 // R4 = 60
HALT