Lec4 ch2-1
Lec4 ch2-1
Lec4 ch2-1
It is easy to see by formal-logical methods that there exist certain [instruction sets] that are
in abstract adequate to control and cause the execution of any sequence of operations.…
The really decisive considerations from the present point of view, in selecting an
[instruction set], are more of a practical nature: simplicity of the equipment demanded by
the [instruction set], and the clarity of its application to the actually important problems
together with the speed of its handling of those problems.
[Burks, Goldstine, and von Neumann]
Stored-program concept
• The idea that instructions and data of many
types can be stored in memory as numbers and
thus be easy to change, leading to the stored-
program computer.
• Treating instructions in the same way as data
greatly simplifies both the memory hardware
and the software of computer systems.
Operations of the computer hardware
• Example 2:
C code: a = b + c + d + e
RISC-V code:
add a, b, c // a=b+c
add a, a, d // a=a+d
add a, a, e // a=a+e
8
CONTD…
• Example 3:
C code: a = b + c;
d = a – e;
RISC-V code: add a, b, c
sub d, a, e
• Example 4:
C code: f = (g + h) – (i + j);
RISC-V code:
add t0,g,h //temporary variable t0 contains g + h
add t1,i,j //temporary variable t1 contains i + j
sub f,t0,t1
Operands of the Computer Hardware
• Design Principle 2: Smaller is faster
– Arithmetic instructions operands must be in registers
– Size of register is 64 bits (group of 64 bits is called doubleword)
– RISC-V has 32 registers. Reasons of keeping only 32 registers are:
1. A very large number of registers may increase the clock cycle time.
2. The number of bits it would take in the instruction format.
– The RISC-V convention is x followed by the number of the register.
10
Example
• Example 1: f = (g + h) – (i + j);
The variables f, g, h, i, and j are assigned to the registers
x19, x20, 221, x22 and x23 respectively.
add x5,x20,x21
add x6,x22,x23
sub x19,x5,x6
RISC-V Register conventions
Memory operands
13
Memory addresses and contents of memory at those addresses
CONTD…
• Since 8-bit bytes are useful in many programs, virtually all architectures today
address individual bytes. Therefore, the address of a doubleword matches the address
of one of the 8 bytes within the doubleword, and addresses of sequential
doublewords differ by 8.
Load Instruction
16
STORE Instruction
“Used to store a doubleword to memory”
• The actual RISC-V name for this instruction is sd, standing for store
doubleword.
• sd x1, offset(x2)
• The address is computed by adding the contents of register x1 to the
sign-extended offset (which is an immediate value).
• Value from register x2 is then copied to the memory address computed.
• Example:
• sd x6, 20(x19) // Memory[x19+ 20] = x6
17
CONTD…
• Example 1: g = h + A[8];
ld x9,64(x22) // Temporary reg x9 gets A[8]
add x20,x21,x9 // g = h + A[8]
20
Constant 0
21
To keep the examples simple, in this book all pointers are assumed to be 64 bits
and all C integers are assumed to be declared as long long int to keep them the
same size.
Signed and unsigned numbers
• Numbers are kept in computer hardware as a series of high and low electronic
signals, and so they are considered base 2 numbers.
• Base 2 numbers are called binary numbers.
• A single digit of a binary number is thus the “atom” of computing, since all
information is composed of binary digits or bits.
• This fundamental building block can be one of two values, which can be thought of
as several alternatives: high or low, on or off, true or false, or 1 or 0.
• In any number base, the value of ith digit d is ,where i starts at 0 and
increases from right to left.
CONTD…
Example:
Represents
RISC-V doubleword
• The phrase least significant bit is used to refer to the rightmost bit (bit 0 above) and
most significant bit to the leftmost bit (bit 63).
CONTD…
• The RISC-V doubleword is 64 bits long, so we can represent different 64-bit patterns.
• 64-bit binary numbers can be represented in terms of the bit value times a power of 2
(here means the ith bit of x):
• Keep in mind that the binary bit patterns above are simply representatives of
numbers. Numbers really have an infinite number of digits, with almost all being 0
except for a few of the rightmost digits. We just don’t normally show leading 0s.
• If result of arithmetic operations cannot be represented by these rightmost
hardware bits, overflow is said to have occurred. It’s up to the programming
language, the operating system, and the program to determine what to do if
overflow occurs.
CONTD…
• Computer programs calculate both positive and negative numbers, so we need a
representation that distinguishes the positive from the negative.
• The representation that made the hardware simple is two’s complement
representation: leading 0s mean positive, and leading 1s mean negative.
• Two’s complement representation has the advantage that all negative numbers have a
1 in the most significant bit. Thus, hardware needs to test only this bit to see if a
number is positive or negative (with the number 0 is considered positive). This bit is
often called the sign bit.
• Overflow occurs when the sign bit is incorrect): a 0 on the left of the bit pattern when
the number is negative or a 1 when the number is positive.
Two’s complemet
• =
Negating this number by inverting the bits and adding one,
+
Sign extension
• How to convert a binary number represented in n bits to a number represented with more than n bits.
• Take the most significant bit from the smaller quantity—the sign bit—and replicate it to fill the new
bits of the larger quantity.
• The 16-bit binary version of the number 2 is
• It is converted to a 64-bit number by making 48 copies of the value in the most significant bit (0)
and placing that in the left of the doubleword. The right part gets the old value:
• =
• If we negate this number using two’s complement method
• Creating a 64-bit version of the negative number means copying the sign bit 48 times and placing it
on the left:
• =
Binary to Hexadecimal
Instruction Formats
• All RISC-V instruction takes exactly 32 bits—the same size as a data word.
• There is a conflict between the desire to keep all instructions the same length and
the desire to have a single instruction format. This leads us to the final hardware
design principle:
• Design Principle 4: Good design demands good compromises.
• The compromise chosen by the RISC-V designers is to keep all instructions the same
length, thereby requiring different kinds of instruction formats for different kinds of
instructions.
• R-format
• I-format
• S-format
31
R-format
Instruction fields !
• op: Basic operation code (opcode).
• rd: destination register number. It gets the result of the operation.
• funct3: An additional opcode field.
• rs1: The first register source operand.
• rs2: The second register source operand.
• funct7: An additional opcode field.
32
I-format
33
S-format
• A[30] = h + A[30] + 1;
is compiled into
• ld x9, 240(x10) // Temporary reg x9 gets A[30]
• add x9, x21, x9 // Temporary reg x9 gets h+A[30]
• addi x9, x9, 1 // Temporary reg x9 gets h+A[30]+1
• sd x9, 240(x10) // Stores h+A[30]+1 back into A[30]