RISC-V Principles and Instructions

More 
RISC-V
, 
RISC-V
 Functions
Summary
 
RISC Design Principles
Smaller is faster:  32 registers, fewer instructions
Keep it simple:  rigid syntax
RISC-V
 Registers:  
s0-s
11
, 
t0-t
6
, 
x
0
No data types, just 
raw bits
, operations determine
how they are interpreted
Memory is byte-addressed
no types → no automatic pointer arithmetic
 
2
 
RISC-V
 Instructions
Arithmetic: 
 
ad
d, 
su
b, 
add
i
,
     
mult
,
div
Data Transfer: 
lw
,
 
sw
, 
     
lb, sb, lbu
Branching:
  
beq, bne, j, bge, blt, 
jal;
Bitwise:
  
and
,  
or
,  
xor
,
   
  
and
i
, 
or
i
, 
xor
i
Shifting:
  
sll
,  
srl
,  
sra
,
     
sll
i
, 
srl
i
, 
sra
i
 
3
i
 = “immediate”
(constant integer)
 
 
 
So Far:
lw reg, off(bAddr)
sw reg, off(bAddr)
 
 
 
 
 
 
 
What about characters (1B) and shorts (sometimes
2B), etc?
Want to be able to use interact with memory
values smaller than a word.
Memory and Variable Size
4
 
 
Trading Bytes with Memory
(2 approaches)
 
Method 1
:  Move words in and out of memory using
bit-masking and shifting
 
lw   s0,0(s1)
 
andi s0,s0,0xFF # lowest byte
Method 2
:  
Load/store byte instructions
 
lb   s1,1(s0)
 
sb   s1,0(s0)
5
*(s0) = 
0x00000180
Endianness
Big Endian:  
Most-significant byte at least address of word
word address = address of most significant byte
Little Endian:  
Least-significant byte at least address of
word
word address = address of least significant byte
6
m
s
b
l
s
b
3
(
s
0
)
2
(
s
0
)
1
(
s
0
)
0
(
s
0
)
0
(
s
0
)
1
(
s
0
)
2
(
s
0
)
3
(
s
0
)
*(s0) = 
0x00000180
RISC-V is Little Endian
 
 
Byte Instructions
 
lb
/
sb
 utilize the 
least significant byte of the
register
On 
sb
, upper 24 bits are ignored
On 
lb
, upper 24 bits are filled by sign-extension
For example, let 
*(s0) = 0x00000180
:
 
lb s1,1(s0)
 
lb s2,0(s0)
 
sb s2,2(s0)
7
 
 
# s1=0x00000001
# s2=0xFFFFFF80
# *(s0)=0x00800180
 
 
Half-Word Instructions
lh reg, off(bAddr) 
“load half”
sh reg, off(bAddr) 
“store half”
On 
sh
, upper 16 bits are ignored
On 
lh
, upper 16 bits are filled by sign-extension
8
Unsigned Instructions
lhu reg, off(bAddr)
“load half unsigned”
lbu reg, off(bAddr)
“load byte unsigned”
On 
l(b/h)u
, upper bits are filled by zero-extension
Why no s(h/b)u? Why no lwu?
Agenda
More Memory Instructions
Inequalities
Pseudo-Instructions
Bonus:  Memory Address Convention
Bonus:  
Alternate 
Register Convention Analogy
9
 
 
Inequalities in 
RISC-V
Set Less Than
 (
slt
)
slt dst,
reg
1,
reg
2
if value in 
reg
1
 < value in 
reg
2
, 
dst
 = 1, else
 0
Set Less Than Immediate 
(
slti
)
slti dst,reg1,imm
If value in 
reg1
 < 
imm
, 
dst
 = 1, else 0
These values are all interpreted as signed values
What if we want to compare unsigned numbers?
10
 
 
Unsigned Inequalities
11
 
# s0=0xFFFFFFFF
# t0=1
 (-1 < 1)
# t1=0
 (2
32
-1 >>> 1)
 
Unsigned versions
 of 
slt(i)
:
sltu
  dst,src1,src2
: unsigned comparison
sltiu
 dst,src,imm
: unsigned comparison
against constant
Example:
    addi  s0,
x
0,-1 
    slti  t0,s0,1
 
    sltiu t1,s0,1
 
Aside:  
RISC-V
 Signed vs. Unsigned
RISC-V
 terms “signed” and “unsigned” appear in 
2
different contexts:
Signed
 vs. 
unsigned
 bit extension
lb,lh
lbu,lhu
Signed
 vs. 
unsigned
 comparison
slt, slti
sltu, sltiu
12
 
 
undefined
Question:  
What C code properly fills in the
following blank?
do {i--; } while((z = 
____________
));
Loop:
              
# i→s0, j→s1
addi  s0,s0,-1     
# i = i - 1
slti 
 t0
,
s1
,2      
# t0 = (j < 2)
bne   
t0,
x0 
Loop
 
 
# goto Loop if t0!=0
slt  
 t0
,
s1
,s0     
# t1 = (j < i)
bne  
 t0
,x0 ,
Loop
  
# goto Loop if t0!=0
13
undefined
Question:  
What C code properly fills in the
following blank?
do {i--; } while((z = 
____________
));
Loop:
              
# i→s0, j→s1
addi  s0,s0,-1     
# i = i - 1
slti 
 t0
,
s1
,2      
# t0 = (j < 2)
bne   
t0,
x0 
Loop
 
 
# goto Loop if t0!=0
slt  
 t0
,
s1
,s0     
# t1 = (j < i)
bne  
 t0
,x0 ,
Loop
  
# goto Loop if t0!=0
14
Agenda
More Memory Instructions
Inequalities
Administrivia
Pseudo-Instructions
Bonus:  Memory Address Convention
Bonus:  
Alternate 
Register Convention Analogy
15
 
 
Assembler Pseudo-Instructions
 
more intuitive for programmers, but NOT directly
implemented in hardware
translated later into instructions which are
directly implemented in hardware
Example:
mv
 dst,
reg1
 translated into
addi dst,
reg1
,0 
or
 add dst,reg1,x0
 
Full list of RISC-V supported pseudo instructions are
on the greensheet
16
 
 
More
 Pseudo-Instructions
Load Immediate
 (
li
)
li dst,imm
Loads 32-bit immediate into 
dst
translates to: 
addi dst x0 imm
Load Address 
(
la
)
la dst,label
Loads address of specified label into 
dst
(translation omitted)
17
 
J is a Pseudo-Instruction
Even the 
j
 instruction is actually a pseudo-
Instruction
We will see what this converts to later this lecture
Pseudo-Instructions are core to writing RISC
assembly code and you will see it in any RISC
assembly code you read
18
6/28/2016
 
True Assembly vs RISC-V
True Assembly Language
The instructions a computer understands and
executes (directl
y implemented in hardware!)
RISC-V
 Assembly Language 
Instructions the assembly programmer can use
(includes pseudo-instructions)
Each 
RISC-V Assembly
 instruction becomes 1 or
more T
rue Assemblt
 instructions
True Assembly 
 
RISC-V Assembly
19
 
 
Slide Note
Embed
Share

This content covers essential topics related to RISC-V architecture, including design principles, registers, instructions for arithmetic, data transfer, branching, and bitwise operations. Additionally, it explores memory management, variable size handling, trading bytes with memory, and endianness concepts like Big and Little Endian. The use of byte instructions, memory access methods, and examples are also discussed in detail, providing a comprehensive overview of RISC-V concepts.

  • RISC-V
  • Architecture
  • Registers
  • Instructions
  • Memory

Uploaded on Feb 15, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. L09 RISC V -II CS295 More RISC-V, RISC-V Functions

  2. L09 RISC V -II CS295 Summary RISC Design Principles Smaller is faster: 32 registers, fewer instructions Keep it simple: rigid syntax RISC-V Registers: s0-s11, t0-t6, x0 No data types, just raw bits, operations determine how they are interpreted Memory is byte-addressed no types no automatic pointer arithmetic 2

  3. L09 RISC V - II CS295 RISC-V Instructions Arithmetic: add, sub, addi, Data Transfer: lw,sw, Branching: jal; Bitwise: Shifting: i= immediate (constant integer) mult,div lb, sb, lbu beq, bne, j, bge, blt, and, or, xor, andi, ori, xori sll, srl, sra, slli, srli, srai 3

  4. L09 RISC V - II CS295 Memory and Variable Size So Far: lw reg, off(bAddr) sw reg, off(bAddr) Computer Processor Control ( brain ) Devices Input Memory lw reg off(bAddr) Output sw reg off(bAddr) Datapath Registers What about characters (1B) and shorts (sometimes 2B), etc? Want to be able to use interact with memory values smaller than a word. 4

  5. L09 RISC V - II CS295 Trading Bytes with Memory (2 approaches) Method 1: Move words in and out of memory using bit-masking and shifting lw s0,0(s1) andi s0,s0,0xFF # lowest byte Method 2: Load/store byte instructions lb s1,1(s0) sb s1,0(s0) *(s0) = 0x00000180 00 00 01 80 5

  6. L09 RISC V - II CS295 Endianness Big Endian: Most-significant byte at least address of word word address = address of most significant byte Little Endian: Least-significant byte at least address of word word address = address of least significant byte *(s0) = 0x00000180 big endian 0(s0) 00 1(s0) 00 2(s0) 01 3(s0) 80 msb lsb 3(s0) 2(s0) 1(s0) 0(s0) little endian RISC-V is Little Endian 6

  7. L09 RISC V - II CS295 Byte Instructions lb/sb utilize the least significant byte of the register On sb, upper 24 bits are ignored On lb, upper 24 bits are filled by sign-extension For example, let *(s0) = 0x00000180: lb s1,1(s0) lb s2,0(s0) sb s2,2(s0) # *(s0)=0x00800180 # s1=0x00000001 # s2=0xFFFFFF80 7

  8. L09 RISC V - II CS295 Half-Word Instructions lh reg, off(bAddr) load half sh reg, off(bAddr) store half On sh, upper 16 bits are ignored On lh, upper 16 bits are filled by sign-extension Unsigned Instructions lhu reg, off(bAddr) load half unsigned lbu reg, off(bAddr) load byte unsigned On l(b/h)u, upper bits are filled by zero-extension Why no s(h/b)u? Why no lwu? 8

  9. L09 RISC V - II CS295 Agenda More Memory Instructions Inequalities Pseudo-Instructions Bonus: Memory Address Convention Bonus: Alternate Register Convention Analogy 9

  10. L09 RISC V - II CS295 Inequalities in RISC-V Set Less Than (slt) slt dst,reg1,reg2 if value in reg1 < value in reg2, dst = 1, else 0 Set Less Than Immediate (slti) slti dst,reg1,imm If value in reg1 < imm, dst = 1, else 0 These values are all interpreted as signed values What if we want to compare unsigned numbers? 10

  11. L09 RISC V - II CS295 Unsigned Inequalities Unsigned versions of slt(i): sltu dst,src1,src2: unsigned comparison sltiu dst,src,imm: unsigned comparison against constant Example: addi s0,x0,-1 slti t0,s0,1 sltiu t1,s0,1 # s0=0xFFFFFFFF # t0=1 (-1 < 1) # t1=0 (232-1 >>> 1) 11

  12. L09 RISC V - II CS295 Aside: RISC-V Signed vs. Unsigned RISC-V terms signed and unsigned appear in 2 different contexts: Signed vs. unsigned bit extension lb,lh lbu,lhu Signed vs. unsigned comparison slt, slti sltu, sltiu 12

  13. Question: What C code properly fills in the following blank? do {i--; } while((z = ____________)); Loop: # i s0, j s1 addi s0,s0,-1 # i = i - 1 slti t0,s1,2 # t0 = (j < 2) bne t0,x0 Loop slt t0,s1,s0 # t1 = (j < i) bne t0,x0 ,Loop # goto Loop if t0!=0 # goto Loop if t0!=0 j < 2 || j < i j 2 && j < i j < 2 || j i j < 2 && j i (A) (B) (C) (D) 13

  14. Question: What C code properly fills in the following blank? do {i--; } while((z = ____________)); Loop: # i s0, j s1 addi s0,s0,-1 # i = i - 1 slti t0,s1,2 # t0 = (j < 2) bne t0,x0 Loop slt t0,s1,s0 # t1 = (j < i) bne t0,x0 ,Loop # goto Loop if t0!=0 # goto Loop if t0!=0 j < 2 || j < i j 2 && j < i j < 2 || j i j < 2 && j i (A) (B) (C) (D) 14

  15. L09 RISC V - II CS295 Agenda More Memory Instructions Inequalities Administrivia Pseudo-Instructions Bonus: Memory Address Convention Bonus: Alternate Register Convention Analogy 15

  16. L09 RISC V - II CS295 Assembler Pseudo-Instructions more intuitive for programmers, but NOT directly implemented in hardware translated later into instructions which are directly implemented in hardware Example: mv dst,reg1 translated into addi dst,reg1,0 or add dst,reg1,x0 Full list of RISC-V supported pseudo instructions are on the greensheet 16

  17. L09 RISC V - II CS295 More Pseudo-Instructions Load Immediate (li) li dst,imm Loads 32-bit immediate into dst translates to: addi dst x0 imm Load Address (la) la dst,label Loads address of specified label into dst (translation omitted) 17

  18. L09 RISC V - II CS295 J is a Pseudo-Instruction Even the j instruction is actually a pseudo- Instruction We will see what this converts to later this lecture Pseudo-Instructions are core to writing RISC assembly code and you will see it in any RISC assembly code you read 6/28/2016 18

  19. L09 RISC V - II CS295 True Assembly vs RISC-V True Assembly Language The instructions a computer understands and executes (directly implemented in hardware!) RISC-V Assembly Language Instructions the assembly programmer can use (includes pseudo-instructions) Each RISC-V Assembly instruction becomes 1 or more True Assemblt instructions True Assembly RISC-V Assembly 19

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#