Lec03 Arithmetic
Lec03 Arithmetic
2025 Spring
Overview
1 Introduction
2/27
Introduction
Welcome to RISC-V
RISC-V
• An open standard instruction set architecture (ISA)
• A clean break from the earlier MIPS-inspired designs
• Modular ISA organization
• Open standards, numerous proprietary and open-source cores
• Managed by RISC-V Foundation
4/27
RISC-V Timeline
5/27
Specifications
Specification of RISC-V
• Allow / Encourage custom extension
• Emphasize flexibility
• Standard extensions
• I (Integer-related extension)
• M (Standard integer multiply and divide extension)
• A (Atomic extension)
• F (Floating-point extension)
• D (double-precision extension)
• C (Compressed instruction extension)
• G (General purpose extension, including IMAFD)
• G extension in RV32I encodes in 32-bit, C extension encodes in 16-bit
• User / Supervisor / Machine level
Notice
Our Labs will focus on RV32I 6/27
RV32I Unprivileged Integer Register
7/27
Some Important Registers
8/27
Some Important Registers
9/27
Introduction
RV32I Base Types
Notice
We will be detailed in Lab 1-1
11/27
Arithmetic & Logical Instructions
RISC-V Arithmetic Instructions
13/27
RISC-V Immediate Instructions
Possible approaches?
• put “typical constants” in memory and load them
• create hard-wired registers (like zero) for constants like 1
• have special instructions that contain constants
addi sp, sp, 4 # sp = sp + 4
slti t0, s2, 15 # t0 = 1 if s2 < 15
14/27
RISC-V Immediate Instructions
15/27
Aside: How About Larger Constants?
1 A new “load upper immediate” instruction (U-type format, load top 20-bits)
lui t0, 1010 1010 1010 1010 1010b
2 Then must get the lower order bits right, use (I-type format, update low 12-bits)
ori t0, t0, 101010101010b
16/27
Aside: How About Larger Constants?
1 A new “load upper immediate” instruction (U-type format, load top 20-bits)
lui t0, 1010 1010 1010 1010 1010b
2 Then must get the lower order bits right, use (I-type format, update low 12-bits)
ori t0, t0, 101010101010b
10101010101010101010 000000000000
00000000000000000000 101010101010
10101010101010101010 101010101010
16/27
RISC-V Shift Operations
• Need operations to pack and unpack 8-bit characters into 32-bit words
• Shifts move all the bits in a word left or right
17/27
RISC-V Shift Instructions
18/27
RISC-V Logical Operations
R Format
and t0, t1, t2 # t0 = t1 & t2
or t0, t1, t2 # t0 = t1 | t2
xor t0, t1, t2 # t0 = t1 & (not t2) + (not t1) & t2
I Format
andi t0, t1, 0xFF00 # t0 = t1 & 0xff00
ori t0, t1, 0xFF00 # t0 = t1 | 0xff00
19/27
RISC-V Logical Instructions
20/27
Data Transfer Instructions
RISC-V Memory Access Instructions
• The data is loaded into (lw) or stored from (sw) a register in the register file – a 5 bit
address
• The memory address – a 32 bit address – is formed by adding the contents of the base
address register to the offset value
• A 12-bit field in RV32I meaning access is limited to memory locations within a region
from −2 KB to 2 KB of the address in the base register
22/27
RISC-V Memory Access Instructions
23/27
Machine Language – Load Instruction
24/27
Machine Language – Load Instruction
24/27
Byte Addresses
• Since 8-bit bytes are so useful, most architectures address individual bytes in memory
• Alignment restriction – the memory address of a word must be on natural word
boundaries (a multiple of 4 in RV32I)
• Big Endian: leftmost byte is word address
• IBM 360/370, Motorola 68k, MIPS, Sparc, HP PA
• Little Endian: rightmost byte is word address
• RISC-V, Intel 80x86, DEC Vax, DEC Alpha (Windows NT)
little endian byte 0
3 2 1 0
msb lsb
0 1 2 3
big endian byte 0
25/27
Aside: Loading and Storing Bytes
26/27
EX-1:
Given following code sequence and memory state:
Memory
0x 0 0 0 0 0 0 0 0 24
0x 0 0 0 0 0 0 0 0 20
add s3, zero, zero 0x 0 0 0 0 0 0 0 0 16
lb t0, 1(s3) 0x 1 0 0 0 0 0 1 0 12
sb t0, 6(s3) 0x 0 1 0 0 0 4 0 2 8
0x F F F F F F F F 4
0x 0 0 9 0 1 2 A 0 0
1 What value is left in t0? Data Word Address
(Decimal)
2 What word is changed in Memory and to what?
3 What if the machine was Big Endian?
27/27