Arithmetic Operations
Basic Arithmetic Instructions
Assembly language provides fundamental arithmetic operations that mirror basic math. Our compiler supports ADD, SUB, and MUL instructions.
ADD Instruction
ADD performs addition of two operands:
ADD Rd, Rn, operand
Examples:
MOV R0, #10
MOV R1, #20
ADD R2, R0, R1 // R2 = R0 + R1 = 30
MOV R3, #15
ADD R4, R3, #5 // R4 = R3 + 5 = 20
SUB Instruction
SUB performs subtraction:
SUB Rd, Rn, operand
Examples:
MOV R0, #50
MOV R1, #20
SUB R2, R0, R1 // R2 = R0 - R1 = 30
MOV R3, #100
SUB R4, R3, #25 // R4 = R3 - 25 = 75
MUL Instruction
MUL performs multiplication:
MUL Rd, Rn, Rm
Examples:
MOV R0, #6
MOV R1, #7
MUL R2, R0, R1 // R2 = R0 * R1 = 42
MOV R3, #12
MOV R4, #5
MUL R5, R3, R4 // R5 = 60
Practical Example: Computing Average
// Calculate average of three numbers
MOV R0, #10 // First number
MOV R1, #20 // Second number
MOV R2, #30 // Third number
ADD R3, R0, R1 // R3 = 10 + 20 = 30
ADD R3, R3, R2 // R3 = 30 + 30 = 60 (sum)
MOV R4, #3 // Divisor
// Note: Division not directly supported in basic version
// Result R3 contains sum (60)
HALT
Complex Calculations
Example: Calculate (5 + 3) * (7 - 2)
MOV R0, #5
MOV R1, #3
ADD R2, R0, R1 // R2 = 8
MOV R3, #7
MOV R4, #2
SUB R5, R3, R4 // R5 = 5
MUL R6, R2, R5 // R6 = 8 * 5 = 40
HALT