Simple Target Machine Model
A simple target machine model is used in compilers to generate low-level code such as assembly or machine code. It mimics a typical three-address machine with support for:
- Load/store instructions
- Arithmetic operations
- Conditional and unconditional jumps
- Simple addressing modes
Machine Characteristics:
- Byte-addressable memory
- n general-purpose registers:
R0, R1, ..., Rn-1
- Operands are integers
- Most instructions are of the form:
OP target, source1, source2
Instruction Types:
1. Load (LD)
Transfers data from memory to a register.
Syntax: LD dst, addr
Meaning: dst = contents of addr
Examples:
LD R1, x
→ Load value at addressx
into registerR1
LD R1, R2
→ Copy value fromR2
toR1
2. Store (ST)
Transfers data from a register to memory.
Syntax: ST r, x
Meaning: x = contents of r
Example: ST R3, a
stores the value in R3
into memory location a
.
3. Computation (OP)
Performs arithmetic or logical operations.
Syntax: OP dst, src1, src2
Example:
ADD R1, R2, R3
→R1 = R2 + R3
SUB R1, R2, R3
→R1 = R2 - R3
Supports both binary and unary operations.
4. Unconditional Jump (BR)
Used for unconditional control transfer.
Syntax: BR L
Effect: Go to instruction labeled L
.
5. Conditional Jump (Bcond)
Used for branching based on conditions.
Syntax: Bcond r, L
Examples:
BLTZ R1, L
→ Branch toL
ifR1 < 0
BGTZ R2, L
→ Branch toL
ifR2 > 0
Addressing Modes:
1. Direct Addressing:
LD R1, x
→ Load value at memory locationx
intoR1
.
2. Register Indexed Addressing:
LD R1, a(R2)
→ Load value from addressa + contents(R2)
intoR1
.
Used for accessing arrays.
3. Offset Indexed Addressing:
LD R1, 100(R2)
→ Load value from address100 + contents(R2)
intoR1
.
Used for pointer arithmetic.
4. Indirect Addressing:
LD R1, *R2
→ Load value from address stored inR2
LD R1, *100(R2)
→ Load from address stored at100 + R2
Used for dereferencing pointers.
5. Immediate Addressing:
LD R1, #100
→ Load constant 100 intoR1
ADD R1, R1, #5
→ Add 5 to the value inR1
Example:
LD R1, #10 // Load 10 into R1
ST R1, a // Store R1 into memory location a
LD R2, a(R3) // Load value from address a + R3
ADD R4, R2, #5 // R4 = R2 + 5
BLTZ R4, NEG // If R4 < 0, jump to NEG