ram
Language

Instructions

Reference for all RAM language instructions

Arithmetic Instructions

These instructions perform arithmetic operations on the accumulator (R0).

InstructionDescriptionExample
ADD xAdd the value at address x to the accumulatorADD 1
SUB xSubtract the value at address x from the accumulatorSUB 2
MUL xMultiply the accumulator by the value at address xMUL 3
DIV xDivide the accumulator by the value at address xDIV 4
MOD xSet the accumulator to the remainder of division by the value at address xMOD 5

Memory Instructions

These instructions are used to move data between memory and the accumulator.

InstructionDescriptionExample
LOAD xLoad the value from address x into the accumulatorLOAD 1
STORE xStore the value in the accumulator to address xSTORE 2
CLEARSet the accumulator to zeroCLEAR

Control Flow Instructions

These instructions control the flow of execution in the program.

InstructionDescriptionExample
JUMP xJump to the instruction with label xJUMP loop
JGTZ xJump to x if the accumulator is greater than zeroJGTZ positive
JZERO xJump to x if the accumulator is zeroJZERO zero
JNEG xJump to x if the accumulator is negativeJNEG negative
HALTStop program executionHALT

Input/Output Instructions

These instructions handle input and output operations.

InstructionDescriptionExample
READ xRead a value from input and store it at address xREAD 1
WRITE xWrite the value at address x to outputWRITE 2

Register Instructions

These instructions operate on specific registers.

InstructionDescriptionExample
LOAD RxLoad the value from register x into the accumulatorLOAD R1
STORE RxStore the value in the accumulator to register xSTORE R2

Advanced Instructions

These instructions provide additional functionality for more complex programs.

InstructionDescriptionExample
AND xBitwise AND the accumulator with the value at address xAND 1
OR xBitwise OR the accumulator with the value at address xOR 2
XOR xBitwise XOR the accumulator with the value at address xXOR 3
NOTBitwise NOT the accumulatorNOT
SHL xShift the accumulator left by x bitsSHL 1
SHR xShift the accumulator right by x bitsSHR 2

Example Program

Here's an example program that uses several of these instructions:

# Calculate the factorial of the value at address 1
# and store the result at address 2
        LOAD 1      # Load the input value
        STORE 3     # Store a copy in address 3 (counter)
        LOAD =1     # Initialize the result to 1
        STORE 2     # Store the initial result
loop:   LOAD 3      # Load the counter
        JZERO end   # If counter is zero, we're done
        LOAD 2      # Load the current result
        MUL 3       # Multiply by the counter
        STORE 2     # Store the new result
        LOAD 3      # Load the counter
        SUB =1      # Decrement the counter
        STORE 3     # Store the updated counter
        JUMP loop   # Repeat
end:    HALT        # Stop execution

This program calculates the factorial of the value stored at address 1 and stores the result at address 2.

The exact set of available instructions may vary depending on the RAM implementation. Check the documentation for your specific implementation for the most accurate information.

On this page