Lecture6 RISC v Assembly I
Lecture6 RISC v Assembly I
Computer Architectures
2
We will see many ISAs
● Intel ISA
○ Millions of PCs are based on Intel or Intel compatible architecture
○ It's a paradigmatic example of CISC architecture
● ARM
○ middle ground between CISC and RISC
○ Used in many embedded systems
● RISC-V
○ precisely RISC, modern, open-source
3
The RISC-V Instruction Set
● Developed at UC Berkeley as open ISA
● Now managed by the RISC-V Foundation (riscv.org)
● Similar ISAs have a large share of embedded core market
○ Applications in consumer electronics, network/storage equipment, cameras,
printers, …
4
Arithmetic Operations
● Add and subtract, three operands
○ Two sources and one destination
add a, b, c // a gets b + c
● All arithmetic operations have this form
○ Each RISC-V arithmetic instruction performs only one operation and must
always have exactly three variables.
● Design Principle 1: Simplicity favours regularity
○ Regularity makes implementation simpler
○ Simplicity enables higher performance at lower cost
5
Assembly language notation
● Each line of this language can contain at most one instruction.
add a, b, c // The sum of b and c is placed in a
add a, a, d // The sum of b, c, and d is now in a
add a, a, e // The sum of b, c, d, and e is now in a
● Comments always terminate at the end of a line.
6
Arithmetic Example
● We want to place the sum of four variables b, c, d, and e into variable a.
● The following sequence of instructions adds the four variables:
7
Arithmetic Example
● C code: a = b + c;
d = a − e;
● The compiler translates from C to RISC-V assembly language instructions.
add a, b, c
sub d, a, e
8
Arithmetic Example
● C code: f = (g + h) - (i + j);
● Compiled RISC-V code:
add t0, g, h // temp t0 = g + h
add t1, i, j // temp t1 = i + j
sub f, t0, t1 // f = t0 - t1
● The compiler must break this statement into several assembly
instructions:
○ Since only one operation is performed per RISC-V instruction!
9
Operands of the Computer Hardware
● We have operands in arithmetic instructions :
add a, b, c // a gets b + c
● Where are do these operands stay?
10
Operands of the Computer Hardware
● We have operands in arithmetic instructions :
add a, b, c // a gets b + c
● Where are do these operands stay?
● The operands of arithmetic instructions are restricted:
○ they must be from a limited number of special locations built directly in
hardware
● Called registers!
11
Registers - Recap
8-bit register
● Assembly Instructions: operate on data i7 D
C
Q+ o7
○ Immediate (constant) i6 D
C
Q+ o6
○ Values stored in registers i5 D
Q+ o5
Memory contents (various addressing modes)
C
○ i4 D
o4
● k-bit register: a set of k D-type flip-flops
Q+
C
i3 D
Q+ o3
C
i2 D
Q+ o2
C
i1 D
Q+ o1
C
i0 D
Q+ o0
C
Clock
I O
A D-type flip-flop
12
Clock
Register Operands
● Registers are visible to the programmer
○ Arithmetic instructions use register operands
● RISC-V has a 32 × 64-bit register file
○ Use for frequently accessed data
○ 64-bit data is called a “doubleword”
■ 32 x 64-bit general purpose registers x0 to x30
○ 32-bit data is called a “word”
● Design Principle 2: Smaller is faster
○ main memory: millions of locations
○ A very large number of registers may increase the clock cycle time
■ it takes electronic signals longer when they must travel farther.
13
RISC-V Registers
● x0: the constant value 0
● x1: return address
● x2: stack pointer
● x3: global pointer
● x4: thread pointer
● x5 – x7, x28 – x31: temporaries
● x8: frame pointer
● x9, x18 – x27: saved registers
● x10 – x11: function arguments/results
● x12 – x17: function arguments
14
Register Operand Example
● C code: f = (g + h) - (i + j);
● It is the compiler’s job to associate program variables with registers.
○ f, …, j in x19, x20, …, x23
15
Memory Operands
● Programming languages have simple variables that contain single data
elements
○ but they also have more complex data structures—arrays and structures.
16
Memory Operands
● Memory (contains billions of data elements)
○ a large, single-dimensional array
○ memory address acts as the index to that array -
starting at 0.
○ To access a word or doubleword in memory, we must
supply the memory address.
● Can hold composite data
○ data structures (arrays and structures) are kept in
memory!
17
Memory Operands
● We need data transfer instructions
○ transfer data between memory and registers.
● Such instructions are traditionally called
○ load - copies data from memory to a register
○ store - copies data from a register to memory
18
Memory Operands
● Arithmetic operations occur only on registers in RISC-V instructions.
● To apply arithmetic operations in RISC-V:
○ Load values from memory into registers
○ Perform arithmetic operation using registers
○ Store result from register to memory
19
Byte-addressed Memory
● Virtually all architectures today address individual bytes
Doubleword
0 1 2 3 4 5 6 7
Bytes
20
Alignment Restriction
● Many architectures have alignment restrictions:
○ words must start at addresses that are multiples of 4
○ doublewords must start at addresses that are multiples of 8
Addr 3 EA
Little Endian Addr 2 01
EA01BD1C Addr 1 BD
Addr 0 1C
Addr 3 1C
Big Endian Addr 2 BD
EA01BD1C Addr 1 01
Addr 0 EA
22
Memory Operand Example
● C code: g = h + A[8];
● Assume that:
○ starting address (or base address) of A is in x22.
■ A is an array of 100 doublewords.
○ registers x20 and x21 hold g and h
■ associated by the compiler
23
Memory Operand Example
● C code: g = h + A[8];
● One of the operands is in memory:
○ We must first transfer A[8] to a register.
24
Memory Operand Example
● C code: g = h + A[8];
● One of the operands is in memory:
○ We must first transfer A[8] to a register.
○ The address of A[8]:
■ the base of the array A + 64
found in register x22 the offset to select element 8
25
Memory Operand Example
● C code: g = h + A[8];
● One of the operands is in memory:
○ We must first transfer A[8] to a register.
○ The address of A[8]:
■ the base of the array A + 64
found in register x22 the offset to select element 8
27
Memory Operand Example
● The next instruction must add h (x21) to A[8] (x9) and put the sum in the
register corresponding to g (x20) :
28
Memory Operand Example
● C code:
g = h + A[8];
● Compiled RISC-V code:
29
Memory Operand Example
● C code:
A[12] = h + A[8];
● Assume that:
○ starting address (or base address) of A is in x22.
■ A is an array of 100 doublewords.
○ register x21 holds h
■ associated by the compiler
30
Memory Operand Example
● C code:
A[12] = h + A[8];
31
store doubleword RISC-V Instruction
● The instruction complementary to load is traditionally called store
○ it copies data from a register to memory.
● The format of the sd (store doubleword) instruction:
32
Registers vs. Memory
● Registers are faster to access than memory
● Operating on memory data requires loads and stores
○ More instructions to be executed
33
Immediate Operands
● The constants would have been placed in memory when the program was
loaded
○ We have to load a constant from memory to use one.
● For example, to add the constant 4 to register x22
○ assume that x3 + AddrConstant4 is the memory address of the constant 4:
34
Immediate Operands
● An alternative that avoids the memory load instruction:
○ Offer arithmetic instructions in which one operand is a constant.
● add immediate or addi
○ Constant data specified in an instruction
35
Constant Zero
● RISC-V dedicates register x0 to be hard-wired to the value zero.
● Making the common case fast:
○ Using frequency to justify the inclusions of constants is another example of the
great idea.
36
The end
● Read Sections 2.1, 2.2 and 2.3 of your book.
37