100% found this document useful (1 vote)
10 views70 pages

2025chapter - 02 RISC V

Chapter 2 of 'Computer Organization and Design: RISC-V' discusses the instruction set architecture (ISA) of RISC-V, highlighting its simplicity and regularity in design. It covers various operations such as arithmetic, memory access, and logical operations, emphasizing the use of registers and immediate operands for efficiency. The chapter also details how instructions are represented in binary and the importance of standardized ISAs for program compatibility across different systems.

Uploaded by

yxz767933
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
10 views70 pages

2025chapter - 02 RISC V

Chapter 2 of 'Computer Organization and Design: RISC-V' discusses the instruction set architecture (ISA) of RISC-V, highlighting its simplicity and regularity in design. It covers various operations such as arithmetic, memory access, and logical operations, emphasizing the use of registers and immediate operands for efficiency. The chapter also details how instructions are represented in binary and the importance of standardized ISAs for program compatibility across different systems.

Uploaded by

yxz767933
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMPUTER ORGANIZATION AND DESIGN RISC-V

Edition
The Hardware/Software Interface

Chapter 2
Instructions: Language
of the Computer
§2.1 Introduction
2.1 Instruction Set
n The repertoire of instructions of a
computer
n Different computers have different
instruction sets
n But with many aspects in common
n Early computers had very simple
instruction sets
n Simplified implementation
n Many modern computers also have simple
instruction sets

Chapter 2 — Instructions: Language of the Computer — 2


The RISC-V Instruction Set
n Used as the example throughout the book
n Developed at UC Berkeley as open ISA
n Now managed by the RISC-V Foundation
([Link])
n Typical of many modern ISAs
n See RISC-V Reference Data tear-out card
n Similar ISAs have a large share of embedded
core market
n Applications in consumer electronics, network/storage
equipment, cameras, printers, …

Chapter 2 — Instructions: Language of the Computer — 3


§2.2 Operations of the Computer Hardware
2.2 Arithmetic Operations
n Add and subtract, three operands
n Two sources and one destination
add a, b, c // a gets b + c
n All arithmetic operations have this form
n Design Principle 1: Simplicity favours
regularity
n Regularity makes implementation simpler
n Simplicity enables higher performance at
lower cost

Chapter 2 — Instructions: Language of the Computer — 4


Arithmetic Example
n C code:
f = (g + h) - (i + j);

n Compiled RISC-V code:


add t0, g, h // temp t0 = g + h
add t1, i, j // temp t1 = i + j
add f, t0, t1 // f = t0 - t1

Chapter 2 — Instructions: Language of the Computer — 5


§2.3 Operands of the Computer Hardware
2.3 Register Operands
n Arithmetic instructions use register
operands

n RISC-V has a 32 × 64-bit register file


n Use for frequently accessed data
n 64-bit data is called a “doubleword”
n 32 x 64-bit general purpose registers x0 to x30
n 32-bit data is called a “word”

n Design Principle 2: Smaller is faster


n c.f. main memory: millions of locations

Chapter 2 — Instructions: Language of the Computer — 6


RISC-V Registers
n x0: the constant value 0
n x1: return address
n x2: stack pointer
n x3: global pointer
n x4: thread pointer
n x5 – x7, x28 – x31: temporaries
n x8: frame pointer
n x9, x18 – x27: saved registers
n x10 – x11: function arguments/results
n x12 – x17: function arguments

Chapter 2 — Instructions: Language of the Computer — 7


Register Operand Example
n C code:
f = (g + h) - (i + j);
n f, …, j in x19, x20, …, x23

n Compiled RISC-V code:


add x5, x20, x21
add x6, x22, x23
sub x19, x5, x6

Chapter 2 — Instructions: Language of the Computer — 8


Memory Operands
n Main memory used for composite data
n Arrays, structures, dynamic data
n To apply arithmetic operations
n Load values from memory into registers
n Store result from register to memory
n Memory is byte addressed
n Each address identifies an 8-bit byte
n RISC-V is Little Endian
n Least-significant byte at least address of a word
n c.f. Big Endian: most-significant byte at least address
n RISC-V does not require words to be aligned in
memory
n Unlike some other ISAs
Chapter 2 — Instructions: Language of the Computer — 9
Memory Operand Example
n C code:
A[12] = h + A[8];
n h in x21, base address of A in x22

n Compiled RISC-V code:


n Index 8 requires offset of 64
8 bytes per doubleword
n

ld x9, 64(x22)
add x9, x21, x9
sd x9, 96(x22)

Chapter 2 — Instructions: Language of the Computer — 10


Registers vs. Memory
n Registers are faster to access than
memory
n Operating on memory data requires loads
and stores
n More instructions to be executed
n Compiler must use registers for variables
as much as possible
n Only spill to memory for less frequently used
variables
n Register optimization is important!

Chapter 2 — Instructions: Language of the Computer — 11


Immediate Operands
n Constant data specified in an instruction
addi x22, x22, 4

n Make the common case fast


n Small constants are common
n Immediate operand avoids a load instruction

Chapter 2 — Instructions: Language of the Computer — 12


§2.4 Signed and Unsigned Numbers
2.4 Unsigned Binary Integers
n Given an n-bit number
n 1 n2 1 0
x  x n1 2  x n2 2    x1 2  x 0 2

n Range: 0 to +2n – 1
n Example
n 0000 0000 … 0000 10112
= 0 + … + 1×23 + 0×22 +1×21 +1×20
= 0 + … + 8 + 0 + 2 + 1 = 1110
n Using 64 bits: 0 to +18,446,774,073,709,551,615

Chapter 2 — Instructions: Language of the Computer — 13


2s-Complement Signed Integers
n Given an n-bit number
n 1 n2 1 0
x   x n1 2  x n2 2    x1 2  x 0 2

n Range: –2n – 1 to +2n – 1 – 1


n Example
n 1111 1111 … 1111 11002
= –1×231 + 1×230 + … + 1×22 +0×21 +0×20
= –2,147,483,648 + 2,147,483,644 = –410
n Using 64 bits: −9,223,372,036,854,775,808
to 9,223,372,036,854,775,807

Chapter 2 — Instructions: Language of the Computer — 14


2s-Complement Signed Integers
n Bit 63 is sign bit
n 1 for negative numbers
n 0 for non-negative numbers
n –(–2n – 1) can’t be represented
n Non-negative numbers have the same unsigned
and 2s-complement representation
n Some specific numbers
n 0: 0000 0000 … 0000
n –1: 1111 1111 … 1111
n Most-negative: 1000 0000 … 0000
n Most-positive: 0111 1111 … 1111

Chapter 2 — Instructions: Language of the Computer — 15


Signed Negation
n Complement and add 1
n Complement means 1 → 0, 0 → 1

x  x  1111...111 2  1

x  1  x

n Example: negate +2
n +2 = 0000 0000 … 0010two
n –2 = 1111 1111 … 1101two + 1
= 1111 1111 … 1110two

Chapter 2 — Instructions: Language of the Computer — 16


Sign Extension
n Representing a number using more bits
n Preserve the numeric value
n Replicate the sign bit to the left
n c.f. unsigned values: extend with 0s
n Examples: 8-bit to 16-bit
n +2: 0000 0010 => 0000 0000 0000 0010
n –2: 1111 1110 => 1111 1111 1111 1110

n In RISC-V instruction set


n lb: sign-extend loaded byte
n lbu: zero-extend loaded byte

Chapter 2 — Instructions: Language of the Computer — 17


§2.5 Representing Instructions in the Computer
2.5 Representing Instructions
n Instructions are encoded in binary
n Called machine code

n RISC-V instructions
n Encoded as 32-bit instruction words
n Small number of formats encoding operation code
(opcode), register numbers, …
n Regularity!

Chapter 2 — Instructions: Language of the Computer — 18


Hexadecimal
n Base 16
n Compact representation of bit strings
n 4 bits per hex digit

0 0000 4 0100 8 1000 c 1100


1 0001 5 0101 9 1001 d 1101
2 0010 6 0110 a 1010 e 1110
3 0011 7 0111 b 1011 f 1111

n Example: eca8 6420


n 1110 1100 1010 1000 0110 0100 0010 0000

Chapter 2 — Instructions: Language of the Computer — 19


RISC-V R-format Instructions
funct7 rs2 rs1 funct3 rd opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits

n Instruction fields
n opcode: operation code
n rd: destination register number
n funct3: 3-bit function code (additional opcode)
n rs1: the first source register number
n rs2: the second source register number
n funct7: 7-bit function code (additional opcode)

Chapter 2 — Instructions: Language of the Computer — 20


R-format Example
funct7 rs2 rs1 funct3 rd opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits

add x9,x20,x21
0 21 20 0 9 51

0000000 10101 10100 000 01001 0110011

0000 0001 0101 1010 0000 0100 1011 0011two =


015A04B316

Chapter 2 — Instructions: Language of the Computer — 21


RISC-V I-format Instructions
immediate rs1 funct3 rd opcode
12 bits 5 bits 3 bits 5 bits 7 bits

n Immediate arithmetic and load instructions


n rs1: source or base address register number
n immediate: constant operand, or offset added to base address
n 2s-complement, sign extended
n Design Principle 3: Good design demands good
compromises
n Different formats complicate decoding, but allow 32-bit
instructions uniformly
n Keep formats as similar as possible

Chapter 2 — Instructions: Language of the Computer — 22


RISC-V S-format Instructions
imm[11:5] rs2 rs1 funct3 imm[4:0] opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits

n Different immediate format for store instructions


n rs1: base address register number
n rs2: source operand register number
n immediate: offset added to base address
n Split so that rs1 and rs2 fields always in the same place

Chapter 2 — Instructions: Language of the Computer — 23


Stored Program Computers
The BIG Picture n Instructions represented in
binary, just like data
n Instructions and data stored
in memory
n Programs can operate on
programs
n e.g., compilers, linkers, …
n Binary compatibility allows
compiled programs to work
on different computers
n Standardized ISAs

Chapter 2 — Instructions: Language of the Computer — 24


§2.6 Logical Operations
2.6 Logical Operations
n Instructions for bitwise manipulation
Operation C Java RISC-V
Shift left << << slli
Shift right >> >>> srli
Bit-by-bit AND & & and, andi
Bit-by-bit OR | | or, ori
Bit-by-bit XOR ^ ^ xor, xori
Bit-by-bit NOT ~ ~

n Useful for extracting and inserting


groups of bits in a word
Chapter 2 — Instructions: Language of the Computer — 25
Shift Operations
funct7 immed rs1 funct3 rd opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits

n immed: how many positions to shift


n Shift left logical
n Shift left and fill with 0 bits
n slli by i bits multiplies by 2i
n Shift right logical
n Shift right and fill with 0 bits
n srli by i bits divides by 2i (unsigned only)

Chapter 2 — Instructions: Language of the Computer — 26


AND Operations
n Useful to mask bits in a word
n Select some bits, clear others to 0
and x9,x10,x11

x10 00000000 00000000 00000000 00000000 00000000 00000000 00001101 11000000

x11 00000000 00000000 00000000 00000000 00000000 00000000 00111100 00000000

x9 00000000 00000000 00000000 00000000 00000000 00000000 00001100


00000000

Chapter 2 — Instructions: Language of the Computer — 27


OR Operations
n Useful to include bits in a word
n Set some bits to 1, leave others unchanged
or x9,x10,x11

x10 00000000 00000000 00000000 00000000 00000000 00000000 00001101 11000000

x11 00000000 00000000 00000000 00000000 00000000 00000000 00111100 00000000

x9 00000000 00000000 00000000 00000000 00000000 00000000 00111101 11000000

Chapter 2 — Instructions: Language of the Computer — 28


XOR Operations
n Differencing operation
n Set some bits to 1, leave others unchanged
xor x9,x10,x12 // NOT operation

x10 00000000 00000000 00000000 00000000 00000000 00000000 00001101 11000000

x12 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111

x9 11111111 11111111 11111111 11111111 11111111 11111111 11110010 00111111

Chapter 2 — Instructions: Language of the Computer — 29


§2.7 Instructions for Making Decisions
2.7 Conditional Operations
n Branch to a labeled instruction if a condition is
true
n Otherwise, continue sequentially

n beq rs1, rs2, L1


n if (rs1 == rs2) branch to instruction labeled L1

n bne rs1, rs2, L1


n if (rs1 != rs2) branch to instruction labeled L1

Chapter 2 — Instructions: Language of the Computer — 30


Compiling If Statements
n C code:
if (i==j) f = g+h;
else f = g-h;
n f, g, … in x19, x20, …
n Compiled RISC-V code:
bne x22, x23, Else
add x19, x20, x21
beq x0,x0,Exit // unconditional
Else: sub x19, x20, x21
Exit: …
Assembler calculates addresses
Chapter 2 — Instructions: Language of the Computer — 31
Compiling Loop Statements
n C code:
while (save[i] == k) i += 1;
n i in x22, k in x24, address of save in x25
n Compiled RISC-V code:
Loop: slli x10, x22, 3
add x10, x10, x25
ld x9, 0(x10)
bne x9, x24, Exit
addi x22, x22, 1
beq x0, x0, Loop
Exit: …

Chapter 2 — Instructions: Language of the Computer — 32


Basic Blocks
n A basic block is a sequence of instructions
with
n No embedded branches (except at end)
n No branch targets (except at beginning)

n A compiler identifies basic


blocks for optimization
n An advanced processor
can accelerate execution
of basic blocks

Chapter 2 — Instructions: Language of the Computer — 33


More Conditional Operations
n blt rs1, rs2, L1
n if (rs1 < rs2) branch to instruction labeled L1
n bge rs1, rs2, L1
n if (rs1 >= rs2) branch to instruction labeled L1
n Example
n if (a > b) a += 1;
n a in x22, b in x23
bge x23, x22, Exit // branch if b >= a
addi x22, x22, 1
Exit:

Chapter 2 — Instructions: Language of the Computer — 34


Signed vs. Unsigned
n Signed comparison: blt, bge
n Unsigned comparison: bltu, bgeu
n Example
n x22 = 1111 1111 1111 1111 1111 1111 1111 1111
n x23 = 0000 0000 0000 0000 0000 0000 0000 0001
n x22 < x23 // signed
n –1 < +1
n x22 > x23 // unsigned
n +4,294,967,295 > +1

Chapter 2 — Instructions: Language of the Computer — 35


§2.8 Supporting Procedures in Computer Hardware
2.8 Procedure Calling
n Steps required
1. Place parameters in registers x10 to x17
2. Transfer control to procedure
3. Acquire storage for procedure
4. Perform procedure’s operations
5. Place result in register for caller
6. Return to place of call (address in x1)

Chapter 2 — Instructions: Language of the Computer — 36


Procedure Call Instructions
n Procedure call: jump and link
jal x1, ProcedureLabel
n Address of following instruction put in x1

n Jumps to target address

n Procedure return: jump and link register


jalr x0, 0(x1)
n Like jal, but jumps to 0 + address in x1

n Use x0 as rd (x0 cannot be changed)

n Can also be used for computed jumps

n e.g., for case/switch statements


Chapter 2 — Instructions: Language of the Computer — 37
Leaf Procedure Example
n C code:
long long int leaf_example (
long long int g, long long int h,
long long int i, long long int j) {
long long int f;
f = (g + h) - (i + j);
return f;
}
n Arguments g, …, j in x10, …, x13

n f in x20

n temporaries x5, x6

n Need to save x5, x6, x20 on stack

Chapter 2 — Instructions: Language of the Computer — 38


Leaf Procedure Example
n RISC-V code:
leaf_example:
addi sp,sp,-24 Save x5, x6, x20 on stack
sd x5,16(sp)
sd x6,8(sp)
sd x20,0(sp
add x5,x10,x11 x5 = g + h
add x6,x12,x1 x6 = i + j
sub x20,x5,x6 f = x5 – x6
addi x10,x20,0 copy f to return register
ld x20,0(sp) Resore x5, x6, x20 from stack
ld x6,8(sp)
ld x5,16(sp)
addi sp,sp,24
jalr x0,0(x1) Return to caller

Chapter 2 — Instructions: Language of the Computer — 39


Local Data on the Stack

Think: push after modifying sp vs push before modifying sp

Chapter 2 — Instructions: Language of the Computer — 40


Register Usage
n x5 – x7, x28 – x31: temporary registers
n Not preserved by the callee

n x8 – x9, x18 – x27: saved registers


n If used, the callee saves and restores them

Chapter 2 — Instructions: Language of the Computer — 41


Non-Leaf Procedures
n Procedures that call other procedures
n For nested call, caller needs to save on
the stack:
n Its return address
n Any arguments and temporaries needed after
the call
n Restore from the stack after the call

Chapter 2 — Instructions: Language of the Computer — 42


Non-Leaf Procedure Example
n C code:
long long int fact (long long int n)
{
if (n < 1) return f;
else return n * fact(n - 1);
}

n Argument n in x10
n Result in x10

Chapter 2 — Instructions: Language of the Computer — 43


Leaf Procedure Example
n RISC-V code:
fact:
addi sp,sp,-16 Save return address and n on stack
sd x1,8(sp)
sd x10,0(sp)
addi x5,x10,-1 x5 = n - 1
bge x5,x0,L1 if n >= 1, go to L1
addi x10,x0,1 Else, set return value to 1
addi sp,sp,16 Pop stack, don’t bother restoring values
jalr x0,0(x1) Return
L1: addi x10,x10,-1 n=n-1
jal x1,fact call fact(n-1)
addi x6,x10,0 move result of fact(n - 1) to x6
ld x10,0(sp) Restore caller’s n
ld x1,8(sp) Restore caller’s return address
addi sp,sp,16 Pop stack
mul x10,x10,x6 return n * fact(n-1)
jalr x0,0(x1) return

Chapter 2 — Instructions: Language of the Computer — 44


Memory Layout
n Text: program code
n Static data: global
variables
n e.g., static variables in C,
constant arrays and strings
n x3 (global pointer)
initialized to address
allowing ±offsets into this
segment
n Dynamic data: heap
n E.g., malloc in C, new in
Java
n Stack: automatic storage
Chapter 2 — Instructions: Language of the Computer — 45
Local Data on the Stack

n Local data allocated by callee


n e.g., C automatic variables
n Procedure frame (activation record)
n Used by some compilers to manage stack storage
Chapter 2 — Instructions: Language of the Computer — 46
§2.9 Communicating with People
Character Data
n Byte-encoded character sets
n ASCII: 128 characters
n 95 graphic, 33 control
n Latin-1: 256 characters
n ASCII, +96 more graphic characters
n Unicode: 32-bit character set
n Used in Java, C++ wide characters, …
n Most of the world’s alphabets, plus symbols
n UTF-8, UTF-16: variable-length encodings

Chapter 2 — Instructions: Language of the Computer — 47


Byte/Halfword/Word Operations
n RISC-V byte/halfword/word load/store
n Load byte/halfword/word: Sign extend to 64 bits in rd
n lb rd, offset(rs1)
n lh rd, offset(rs1)
n lw rd, offset(rs1)
n Load byte/halfword/word unsigned: Zero extend to 64 bits in rd
n lbu rd, offset(rs1)
n lhu rd, offset(rs1)
n lwu rd, offset(rs1)
n Store byte/halfword/word: Store rightmost 8/16/32 bits
n sb rs2, offset(rs1)
n sh rs2, offset(rs1)
n sw rs2, offset(rs1)

Chapter 2 — Instructions: Language of the Computer — 48


2.9 String Copy Example
n C code:
n Null-terminated string
void strcpy (char x[], char y[])
{ size_t i;
i = 0;
while ((x[i]=y[i])!='\0')
i += 1;
}

Chapter 2 — Instructions: Language of the Computer — 49


String Copy Example
n RISC-V code:
strcpy:
addi sp,sp,-8 // adjust stack for 1 doubleword
sd x19,0(sp) // push x19
add x19,x0,x0 // i=0
L1: add x5,x19,x10 // x5 = addr of y[i]
lbu x6,0(x5) // x6 = y[i]
add x7,x19,x10 // x7 = addr of x[i]
sb x6,0(x7) // x[i] = y[i]
beq x6,x0,L2 // if y[i] == 0 then exit
addi x19,x19, 1 // i = i + 1
jal x0,L1 // next iteration of loop
L2: ld x19,0(sp) // restore saved x19
addi sp,sp,8 // pop 1 doubleword from stack
jalr x0,0(x1) // and return

Chapter 2 — Instructions: Language of the Computer — 50


§2.10 RISC-V Addressing for Wide Immediates and Addresses
2.10 32-bit Constants
n Most constants are small
n 12-bit immediate is sufficient
n For the occasional 32-bit constant
lui rd, constant
n Copies 20-bit constant to bits [31:12] of rd
n Extends bit 31 to bits [63:32]
n Clears bits [11:0] of rd to 0
lui x19, 976 // 0x003D0
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0011 1101 0000 0000 0000 0000

addi x19,x19,128 // 0x500


0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0011 1101 0000 0101 0000 0000

Chapter 2 — Instructions: Language of the Computer — 51


Branch Addressing
n Branch instructions specify
n Opcode, two registers, target address
n Most branch targets are near branch
n Forward or backward
n SB format:
imm imm
[10:5] rs2 rs1 funct3 [4:1] opcode

imm[12] imm[11]

n PC-relative addressing
n Target address = PC + immediate × 2

Chapter 2 — Instructions: Language of the Computer — 52


Jump Addressing
n Jump and link (jal) target uses 20-bit
immediate for larger range
n UJ format:
imm[10:1] imm[19:12] rd opcode
5 bits 7 bits
imm[20] imm[11]

n For long jumps, eg, to 32-bit absolute


address
n lui: load address[31:12] to temp register
n jalr: add address[11:0] and jump to target

Chapter 2 — Instructions: Language of the Computer — 53


RISC-V Addressing Summary

Chapter 2 — Instructions: Language of the Computer — 54


RISC-V Encoding Summary

Chapter 2 — Instructions: Language of the Computer — 55


§2.11 Parallelism and Instructions: Synchronization
2.11 Synchronization
n Two processors sharing an area of memory
n P1 writes, then P2 reads
n Data race if P1 and P2 don’t synchronize
n Result depends of order of accesses
n Hardware support required
n Atomic read/write memory operation
n No other access to the location allowed between the
read and write
n Could be a single instruction
n E.g., atomic swap of register ↔ memory
n Or an atomic pair of instructions

Chapter 2 — Instructions: Language of the Computer — 56


Synchronization in RISC-V
n Load reserved: lr.d rd,(rs1)
n Load from address in rs1 to rd
n Place reservation on memory address
n Store conditional: sc.d rd,(rs1),rs2
n Store from rs2 to address in rs1
n Succeeds if location not changed since the lr.d
n Returns 0 in rd
n Fails if location is changed
n Returns non-zero value in rd

Think: atomic swap using a single instruction?

Chapter 2 — Instructions: Language of the Computer — 57


Synchronization in RISC-V
n Example 1: atomic swap (to test/set lock variable)
again: lr.d x10,(x20)
sc.d x11,(x20),x23 // X11 = status
bne x11,x0,again // branch if store failed
addi x23,x10,0 // X23 = loaded value
n Example 2: lock
addi x12,x0,1 // copy locked value
again: lr.d x10,(x20) // read lock
bne x10,x0,again // check if it is 0 yet
sc.d x11,(x20),x12 // attempt to store
bne x11,x0,again // branch if fails

n Unlock:
sd x0,0(x20) // free lock

Chapter 2 — Instructions: Language of the Computer — 58


§2.13 A C Sort Example to Put It All Together
2.13 C Sort Example
n Illustrates use of assembly instructions
for a C bubble sort function
n Swap procedure (leaf)
void swap(long long int v[],
long long int k)
{
long long int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}
n v in x10, k in x11, temp in x5
Chapter 2 — Instructions: Language of the Computer — 59
The Procedure Swap
swap:
slli x6,x11,3 // reg x6 = k * 8
add x6,x10,x6 // reg x6 = v + (k * 8)
ld x5,0(x6) // reg x5 (temp) = v[k]
ld x7,8(x6) // reg x7 = v[k + 1]
sd x7,0(x6) // v[k] = reg x7
sd x5,8(x6) // v[k+1] = reg x5 (temp)
jalr x0,0(x1) // return to calling routine

Chapter 2 — Instructions: Language of the Computer — 60


The Sort Procedure in C
n Non-leaf (calls swap)
void sort (long long int v[], size_t n)
{
size_t i, j;
for (i = 0; i < n; i += 1) {
for (j = i – 1;
j >= 0 && v[j] > v[j + 1];
j -= 1) {
swap(v,j);
}
}
}
n v in x10, n in x11, i in x19, j in x20

Chapter 2 — Instructions: Language of the Computer — 61


The Outer Loop
n Skeleton of outer loop:
n for (i = 0; i <n; i += 1) {

li x19,0 // i = 0
for1tst:
bge x19,x11,exit1 // go to exit1 if x19 ≥ x11 (i≥n)

(body of outer for-loop)

addi x19,x19,1 // i += 1
j for1tst // branch to test of outer loop
exit1:

Chapter 2 — Instructions: Language of the Computer — 62


The Inner Loop
n Skeleton of inner loop:
n for (j = i − 1; j >= 0 && v[j] > v[j + 1]; j − = 1) {
addi x20,x19,-1 // j = i −1
for2tst:
blt x20,x0,exit2 // go to exit2 if X20 < 0 (j < 0)
slli x5,x20,3 // reg x5 = j * 8
add x5,x10,x5 // reg x5 = v + (j * 8)
ld x6,0(x5) // reg x6 = v[j]
ld x7,8(x5) // reg x7 = v[j + 1]
ble x6,x7,exit2 // go to exit2 if x6 ≤ x7
mv x21, x10 // copy parameter x10 into x21
mv x22, x11 // copy parameter x11 into x22
mv x10, x21 // first swap parameter is v
mv x11, x20 // second swap parameter is j
jal x1,swap // call swap
addi x20,x20,-1 // j –= 1
j for2tst // branch to test of inner loop
exit2:

Chapter 2 — Instructions: Language of the Computer — 63


Preserving Registers
n Preserve saved registers:
addi sp,sp,-40 // make room on stack for 5 regs
sd x1,32(sp) // save x1 on stack
sd x22,24(sp) // save x22 on stack
sd x21,16(sp) // save x21 on stack
sd x20,8(sp) // save x20 on stack
sd x19,0(sp) // save x19 on stack

n Restore saved registers:


exit1:
sd x19,0(sp) // restore x19 from stack
sd x20,8(sp) // restore x20 from stack
sd x21,16(sp) // restore x21 from stack
sd x22,24(sp) // restore x22 from stack
sd x1,32(sp) // restore x1 from stack
addi sp,sp, 40 // restore stack pointer
jalr x0,0(x1)

Chapter 2 — Instructions: Language of the Computer — 64


§2.14 Arrays versus Pointers
2.14 Arrays vs. Pointers
n Array indexing involves
n Multiplying index by element size
n Adding to array base address
n Pointers correspond directly to memory
addresses
n Can avoid indexing complexity

Chapter 2 — Instructions: Language of the Computer — 65


Example: Clearing an Array
clear1(int array[], int size) { clear2(int *array, int size) {
int i; int *p;
for (i = 0; i < size; i += 1) for (p = &array[0]; p < &array[size];
array[i] = 0; p = p + 1)
} *p = 0;
}

li x5,0 // i = 0 mv x5,x10 // p = address


loop1: // of array[0]
slli x6,x5,3 // x6 = i * 8 slli x6,x11,3 // x6 = size * 8
add x7,x10,x6 // x7 = address add x7,x10,x6 // x7 = address
// of array[i] // of array[size]
sd x0,0(x7) // array[i] = 0 loop2:
addi x5,x5,1 // i = i + 1 sd x0,0(x5) // Memory[p] = 0
blt x5,x11,loop1 // if (i<size) addi x5,x5,8 // p = p + 8
// go to loop1 bltu x5,x7,loop2
// if (p<&array[size])
// go to loop2

Chapter 2 — Instructions: Language of the Computer — 66


Comparison of Array vs. Ptr
n Multiply “strength reduced” to shift
n Array version requires shift to be inside
loop
n Part of index calculation for incremented i
n c.f. incrementing pointer
n Compiler can achieve same effect as
manual use of pointers
n Induction variable elimination
n Better to make program clearer and safer

Chapter 2 — Instructions: Language of the Computer — 67


§2.18 The Rest of the RISC-V Instruction Set
2.20 Other RISC-V Instructions
n Base integer instructions (RV64I)
n Those previously described, plus
n auipc rd, immed // rd = (imm<<12) + pc
n follow by jalr (adds 12-bit immed) for long jump
n slt, sltu, slti, sltui: set less than (like MIPS)
n addw, subw, addiw: 32-bit add/sub
n sllw, srlw, srlw, slliw, srliw, sraiw: 32-bit shift
n 32-bit variant: RV32I
n registers are 32-bits wide, 32-bit operations

Chapter 2 — Instructions: Language of the Computer — 68


Instruction Set Extensions
n M: integer multiply, divide, remainder
n A: atomic memory operations
n F: single-precision floating point
n D: double-precision floating point
n C: compressed instructions
n 16-bit encoding for frequently used
instructions

Chapter 2 — Instructions: Language of the Computer — 69


§2.20 Concluding Remarks
Concluding Remarks
n Design principles
1. Simplicity favors regularity
2. Smaller is faster
3. Good design demands good compromises
n Make the common case fast
n Layers of software/hardware
n Compiler, assembler, hardware
n RISC-V: typical of RISC ISAs
n c.f. x86

Chapter 2 — Instructions: Language of the Computer — 70

You might also like