Basic Examples
Simple examples of RAM programs
Addition
This program adds two numbers stored in memory addresses 1 and 2, and stores the result in address 3:
To run this program with specific input values:
After execution, memory address 3 will contain 12.
Subtraction
This program subtracts the number in address 2 from the number in address 1, and stores the result in address 3:
Multiplication
This program multiplies two numbers stored in addresses 1 and 2, and stores the result in address 3:
Division
This program divides the number in address 1 by the number in address 2, and stores the result in address 3:
Swap
This program swaps the values stored in addresses 1 and 2:
Absolute Value
This program calculates the absolute value of the number stored in address 1 and stores the result in address 2:
Counting
This program counts from 1 to the value stored in address 1, storing each value in consecutive memory locations starting from address 2:
Maximum
This program finds the maximum of two numbers stored in addresses 1 and 2, and stores the result in address 3:
Minimum
This program finds the minimum of two numbers stored in addresses 1 and 2, and stores the result in address 3:
Sum of Array
This program calculates the sum of an array of numbers. Address 1 contains the length of the array, and the array elements are stored starting from address 2:
These examples demonstrate the basic capabilities of the RAM language. You can use them as starting points for your own programs or as references when learning the language.