IntroRARS RV Assembler
IntroRARS RV Assembler
curso 2024-2025
• Basic instructions
• Pseudo instructions
• Directives
• System Calls
• Macros
4
RARS for assembly and debugging
programming directly x3
x4
gp
tp
Global pointer
Thread pointer
x5 t0 Temporary / Alternate link register
x6-x7 t1 - t2 Temporary register
• In assembler, we x8 s0 / fp Saved register / Frame pointer
prefer using aliases x9 s1 Saved register
x10-x11 a0-a1 Function argument / Return value registers
to indicate registers x12-x17 a2-a7 Function argument registers
x18-x27 s2-s11 Saved registers
x28-x31 t3-t6 Temporary registers
ABI stand for:
Application Binary Interface 6
RISC-V Instructions (reminder)
• A typical RISC-V instruction is on this format: OPCODE rd, rs1, rs2
• In programming terms, we could think as: rd = f(rs1, rs2)
– The opcode specifies some function f to perform on the two source registers rs1 and rs2
and produce a result which is stored in the destination register rd.
• Example: For the ADD and SUB instruction this is often written as:
ADD x6, x5, x2 # x6 ← x5 + x2
SUB x8, x6, x2 # x8 ← x6 - x2
• Another Example: ADD immediate and Branch if lower:
ADDI x2, x0, 1 # x2 ← x0 + 1
BLT x0, x1, loop # IF x0 < x1 GOTO loop
* RARS interpreter uses hash (#) symbols for comments.
7
BLT stands for Branch if Lower Than
RISC-V Instructions (reminder)
• Why Three Operands? In assembly code instructions are encoded in a fixed width format (32-
bits in RISC-V). To make it easier for the decoder inside the processor to figure out what an
instruction does, it helps to have different parts of the instruction being in fixed locations.
8
RISC-V Instructions (reminder)
• The diagram shows what the different bits are used for in 32-bit
encoding. You can observe the regularity:
– Opcode at same position and size (bit-0 to bit-6)
– The selection of the rd register spans the same bit positions, bit-7 to bit-11
– This also happens for the fields selecting the source registers rs1 and rs2.
• You can however write a bunch of RISC-V instructions that don’t
take three arguments, but a lot of these are in fact pseudo-
instructions. (We will introduce them shortly).
– For example:
NEG x2, x4 # x2 ← negative of x4
– It is in fact a shorthand for:
SUB x2, zero, x4 # x2 ← zero - x4 9
RISC-V Assembler: Loading and Storing Data
• To load data from memory into registers or store data in registers to
memory we use the L and S instructions. You need to use different
suffixes to indicate what you are loading or storing:
– LB - Load Byte, LW - Load Word, LD - Load Double We use only LW and
– SB - Store Byte, SW - Store Word, SD - Store Double SW for simplicity.
• On line 12, we check if x1 is still larger than zero (to see if not end countdown).
If it is, we want to jump to line 04 (where we use SUB to subtract 1 from x1).
• However, we don't write BLT zero, x1, 4. Instead we specify -8. That is
because jumps are relative (to PC). We jump two instructions backwards.
12
RISC-V Assembler: Addresses, Jumps and Labels
• The relative branch saves a lot of space. You only have 32-bits to
encode an instruction:
– Encode a register requires 5 bits. 2 registers for branch eats up 10-bits.
– The opcode and function (funct3) eat up 10-bits (7+3).
– That leaves 12-bits to specify an address to jump to. The maximum number
you get with 12-bits is 4096 (2¹²) -1. Thus, if your program was larger than 4
KB you couldn’t perform jumps.
16
RISC-V Assembler: Jumps and Branch
• Jump and Link (JAL). The JAL instruction can be used for both
calling functions or just making a simple unconditional jump.
– JAL makes a relative jump (relative to PC) just like the conditional branch.
– However, the provided register argument is not used for comparisons but to
store return address.
– If you don’t need the return address, you can simply provide the zero reg. x0.
• The convention used with RISC-V is that the return address should
be stored in the return address register ra (which is x1).
17
RISC-V Assembler: Jumps and Branch
• Jump and Link (JAL). It is a J-Format Instruction
– The big difference is that JALR jumps are not relative to PC, Instead they
are relative to rs1 .
19
RISC-V Assembler: Jumps and Branch
• Jump and Link Register (JALR). It is a I-Format Instruction
22
RISC-V Assembler: Constants
• The following example shows loading a constant using the %hi and
%lo assembler functions.
lui a0, %hi(UART_BASE) #20 most significant bits. i.e . a0 <- 0x40003000
23
RISC-V Assembler: data and text segment
• You need to define
what goes to data and
instruction memory.
26