ITSC 2181 - Module 6 Unit 2 Memory Access in RISC-V

I. Shift Right Logical and Arithmetic Operators in RISC-V

A. Shift Right Logical Immediate (SRLI/SRLIW)

  • Syntax: srli dest, src, imm

  • Function: Divides value in src by 2^imm, rounds down.

  • Only for unsigned integers.

  • Example 1:

    • x5 = 10

    • srliw x6, x5, 110 / 2 = 5x6 = 5

  • Example 2:

    • x4 = 23

    • srli x3, x4, 123 / 2 = 11.5 → floor(11.5) = 11x3 = 11

    • Always round down (Ex: 11.5 → 11)

B. Shift Right Arithmetic Immediate (SRAI/SRAIW)

  • Syntax: srai dest, src, imm

  • Used for signed integers.

  • Preserves the sign bit (leftmost bit).

  • Example:

    • x5 = -105

    • srai x6, x5, 1-105 / 2 = -52.5 → floor(-52.5) = -53x6 = -53

    • Always round down negatives!!


II. RISC-V Assembly Translation Example

A. Example Expression: f = g – (j * 33) + (f * 2);

  • Assume: f = t0, g = t1, j = t2

  • Assembly steps:

    1. addi t4, t2, zero // copy j to t4

    2. slli t2, t2, 5 // j * 32

    3. add t2, t2, t4 // j * 33

    4. slli t0, t0, 1 // f * 2

    5. sub t1, t1, t2 // g - (j * 33)

    6. add t0, t0, t1 // result → f


III. Unit 1 Review Reminders

A. Code Translation Examples:

  1. f = f * 128;

  2. g += 10;

  3. j -= g;

  4. f += g + j;

B. General Assembly Concepts

  • One instruction = one line of assembly

  • High-level variables → assembly operands

  • Operand types: registers, constants, memory locations

  • Operation types:

    • Arithmetic

    • Memory load/store

    • Control transfer


IV. Memory Load and Store

A. Basic Concepts

  • ALU only works with data in registers/immediates.

  • To operate on memory data:

    1. Load into register

    2. Operate

    3. Store back to memory

  • Memory = byte-addressed (1 byte = 8 bits)

B. Load/Store Operators

Instruction

Usage

ld/sd

double, long int, pointers

lw/sw

int, unsigned, float

lh/sh

short, unsigned short

lbu/sbu

char

C. Word Definition

  • 1 word = 32 bits = 4 bytes

  • 1 halfword = 16 bits = 2 bytes


V. Assembly Examples

A. a = i + 1; (Registers: a = x1, i = x2)

addi x1, x2, 1    // a = i + 1

B. a = i++;

add x1, x2, x0    // a = i
addi x2, x2, 1    // i = i + 1

C. a = ++i;

addi x2, x2, 1    // i = i + 1
add x1, x2, x0    // a = i

VI. Memory Access Examples

A. Example 1: a = i + 1; (i and a in memory, addr in x6/x7)

lw x10, 0(x7)      // load i
addi x11, x10, 1   // i + 1
sw x11, 0(x6)      // store to a
  • Offset: Constant before the base register

  • Format: lw dest, offset(base_reg)

B. Example 2: i++; a = i;

lw x10, 0(x7)     // load i
addi x11, x10, 1  // increment
sw x11, 0(x7)     // update i
lw x10, 0(x7)     // reload i
sw x10, 0(x6)     // store into a

C. Example 3: double A[N]; h = A[8];

  • A in x22, h in x21

ld x9, 64(x22)    // A[8] = offset 8*8 = 64
add x21, x9, x0   // h = A[8]

D. Example 4: A[8] = A[10]; (int array, word size = 4 bytes)

lw x6, 40(x22)    // A[10]
sw x6, 32(x22)    // A[8]

E. Example 5: A[i] = B[i];

slliw x6, x5, 2      // offset = i * 4
add x7, x23, x6      // address of B[i]
lw x9, 0(x7)         // load B[i]
add x8, x22, x6      // address of A[i]
sw x9, 0(x8)         // store to A[i]

F. Example 6: A[i] = B[i-1] + B[i] + B[i+1];

slliw x6, x5, 2      // i * 4
add x7, x23, x6      // B[i] addr
lw x9, 0(x7)         // B[i]
lw x10, 4(x7)        // B[i+1]
lw x11, -4(x7)       // B[i-1]
add x9, x9, x10
add x9, x9, x11
add x8, x22, x6      // A[i] addr
sw x9, 0(x8)         // store result

VII. Practice Problems ("You Try!")

A. i = j * 3 + g * 16; (i=t0, j=t1, g=t2)

slli t3, t1, 1     // j * 2
add t3, t3, t1     // j * 3
slli t4, t2, 4     // g * 16
add t0, t3, t4     // i = j*3 + g*16

B. A[8] = j * 10; (j = t1, A base = t5)

slli t2, t1, 3     // j * 8
add t2, t2, t1     // j * 9
add t2, t2, t1     // j * 10
sw t2, 32(t5)      // A[8] = j * 10

C. A[5] = j * 10 + g * 7; (j = t1, g = t2, A base = t5)

slli t3, t1, 3     // j * 8
add t3, t3, t1     // j * 9
add t3, t3, t1     // j * 10
slli t4, t2, 3     // g * 8
sub t4, t4, t2     // g * 7
add t6, t3, t4     // total sum
sw t6, 20(t5)      // store at A[5]

VIII. Recap

  • Instruction Types:

    • Arithmetic: add, sub, addi

    • Memory: ld, sd, lw, sw

  • Data Movement:

    • Registers <-> Memory = load/store

    • Use base + offset format for memory access

  • Memory Addressing:

    • Use offsets to navigate arrays or memory blocks

    • Convert index * data size for offsets


Let me know if you’d like this as a downloadable document or flashcards for study!