| 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. |
| 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. |
| 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). |
| 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 | Arguments | Return Value |
|---|---|---|---|
| 1 | Print Integer | a0 = integer to print | None |
| 4 | Print String | a0 = memory address of string | None |
| 5 | Read Integer | None | a0 = integer entered |
| 8 | Read String | a0 = buffer address, a1 = max length | None |
| 93 | Exit Program | a0 = exit status code (0 for success) | Terminates |
This code block demonstrates how these commands fit together to print a string and exit safely: