RARS (RISC-V Assembler and Runtime System)

1. Data Movement & Pseudo-Instructions

These commands move data between registers, or between registers and memory.
Pseudo-instructions (marked with *) are simplified shortcuts
that RARS automatically converts into valid machine code.
li rd, imm* – Load Immediate. Loads a constant number into the destination register.
la rd, symbol* – Load Address. Loads a memory address (e.g., of a string variable) into a register.
mv rd, rs* – Move. Copies the value from one register to another.
lw rd, offset(rs1) – Load Word. Reads a 32-bit value from memory into a register.
sw rs2, offset(rs1) – Store Word. Writes a 32-bit register value back into memory.
lb rd, offset(rs1) – Load Byte. Reads a single byte from memory.
sb rs2, offset(rs1) – Store Byte. Writes a single byte back into memory.

2. Arithmetic & Logical Instructions

These carry out core mathematical algorithms and bitwise operations.
Adding an i suffix means the instruction performs an operation directly
with a literal integer (immediate value).
add rd, rs1, rs2 – Adds two registers and saves the result in a destination register.
addi rd, rs1, imm – Adds an immediate integer value directly to a register.
sub rd, rs1, rs2 – Subtracts register rs2 from rs1.
mul rd, rs1, rs2 – Multiplies two registers together.
div rd, rs1, rs2 – Divides rs1 by rs2 and stores the quotient.
rem rd, rs1, rs2 – Computes the division remainder (modulo).
and rd, rs1, rs2 – Performs a bitwise AND operation.
or rd, rs1, rs2 – Performs a bitwise OR operation.

3. Branching & Control Flow (Loops & Conditionals)

Used to control program flow, such as creating if/else statements or for/while loops.
beq rs1, rs2, label – Branch if Equal. Jumps to the label if registers match.
bne rs1, rs2, label – Branch if Not Equal. Jumps to the label if registers differ.
blt rs1, rs2, label – Branch if Less Than. Jumps if rs1 is less than rs2.
bge rs1, rs2, label – Branch if Greater Than or Equal. Jumps if rs1 is greater or equal to rs2.
j label* – Jump. Automatically jumps to the specified label unconditionally.
jal rd, label – Jump and Link. Jumps to a label and saves the return address (used for calling subroutines).
jalr rd, offset(rs1) – Jump and Link Register. Jumps to an address held in a register
(used to return from functions).

4. RARS Environment Calls (ecall)

To print output, read inputs, or terminate a program, you interact with the RARS runtime system.
You must load the service code into register a7, load any inputs/arguments into register a0, and then run ecall.
Service Code
(a7)
Operation ArgumentsReturn Value
1Print Integer a0 = integer to printNone
4Print String a0 = memory address of stringNone
5Read Integer Nonea0 = integer entered
8Read String a0 = buffer address,
a1 = max length
None
93Exit Program a0 = exit status code
(0 for success)
Terminates

Example Program Structure

This code block demonstrates how these commands fit together to print a string and exit safely:

.data message: .asciz "Hello from RARS!\n" # Define text data .text .globl main # Global starting point main: la a0, message # Load string address into a0 li a7, 4 # Load "Print String" code into a7 ecall # Trigger the system execution li a0, 0 # Return exit status code 0 li a7, 93 # Load "Exit Program" code into a7 ecall # Terminate the application