Lecture 3 - Number Systems and ISA r-type EECS 388
Lecture 3 - Number Systems and ISA r-type EECS 388
Jacob Schoonover
Lecture notes based in part on slides created by Alex Fosdick,
Heechul Yun, Mohammad Alian, and Tamzidul Hoque
1
Agenda & Announcements
• Number systems
• ISA introduction
• Quiz
• Labs begin!
• Sept 2 holiday - lab sections on this day still need to
complete lab1 and turn it in.
2
Bits vs Bytes
8 bits == 1 byte
Most b7 b6 b5 b4 b3 b2 b1 b0 Least
significant bit significant bit
(MSB) (LSB)
3
Data Storage in Memory
8 bits == 1 byte
Most b7 b6 b5 b4 b3 b2 b1 b0 Least
significant bit significant bit
(MSB) (LSB)
10002=810
00002=010 Change MSB: high impact
10012=910 Change LSB: low impact
4
Little Endian vs. Big Endian
Does it really matter?
5
How to store multi-byte variables
(short, int, long, etc.) in memory?
MSB LSB
… … … …
6
Number Systems
• Decimal (base 10)
• Symbols: 0,1,…,9
• E.g., 12310 = 1x102 + 2x101 + 3x100
• Binary (base 2)
• Symbols: 0,1 *Assuming unsigned number
• E.g., 10112 = 0b1011 = 1x23 + 0x22 + 1x21 + 1x20
• Hexadecimal (base 16)
• Symbols: 0,1,…,9,A,B,…,F
• E.g., 12316 = 0x123 = 1x162 + 2x161 + 3x160
*Assuming unsigned number
7
Positional System
• Value of a number is defined by:
• Digits
• Index/position
• Base 8
Converting to Decimal:
summation of (Digit × 𝑩𝑩𝑩𝑩𝑩𝑩𝑩𝑩𝑰𝑰𝑰𝑰𝑰𝑰𝑰𝑰𝑰𝑰 ) for all digit
1 1 2 5
Digit × 𝑩𝑩𝑩𝑩𝑩𝑩𝑩𝑩𝑰𝑰𝑰𝑰𝑰𝑰𝑰𝑰𝑰𝑰 1 × 83 1 × 82 2 × 81 5 × 80
512 64 16 5
Value= Sum of Products Value in decimal=(512+64+16+5)=597
8
Converting to Decimal (cont.)
• Binary (base 2)
Digit × 𝑩𝑩𝑩𝑩𝑩𝑩𝑩𝑩 𝑰𝑰𝑰𝑰𝑰𝑰𝑰𝑰𝑰𝑰
• Symbols: 0,1
• E.g., 10112 = 0b1011 = 1x23 + 0x22 + 1x21 + 1x20
• 0b indicates binary
9
• Binary to Hex to Decimal conversion!
11
Number Systems
• Conversion to Decimal: Hand calculation is hard for large number
• Need sum(Digit × 𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵 𝐼𝐼𝐼𝐼𝐼𝐼𝐼𝐼𝐼𝐼 ) equation
Decimal Hexadecimal Binary
0 0x0 0b0
2
9
0xA
0xF
0x1F
0b1000 0000
0b1000 0011
0b1000 0000 0000 0000
12
Number Systems
• Conversion to Decimal: Hand calculation is hard for large number
• Need sum(Digit × 𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵 𝐼𝐼𝐼𝐼𝐼𝐼𝐼𝐼𝐼𝐼 ) equation
Decimal Hexadecimal Binary
0 0x0 0b0
2 0x2
9 0x9
10 0xA
15 0xF
31 0x1F
0b1000 0000
0b1000 0011
0b1000 0000 0000 0000
13
Number Systems
• Conversion to Decimal: Hand calculation is hard for large number
• Need sum(Digit × 𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵 𝐼𝐼𝐼𝐼𝐼𝐼𝐼𝐼𝐼𝐼 ) equation
Decimal Hexadecimal Binary
0 0x0 0b0
2 0x2
9 0x9
10 0xA
15 0xF
31 0x1F
0x80 0b1000 0000
0x83 0b1000 0011
0x8000 0b1000 0000 0000 0000
14
Number Systems
• Conversion between Hex & Binary: Easy even for large numbers
• Just need to know the conversion between hex to bin for 0 to F
Decimal Hexadecimal Binary
0 0x0 0b0
2 0x2
9 0x9
10 0xA
15 0xF
31 0x1F
128 0x80 0b1000 0000
131 0x83 0b1000 0011
32768 0x8000 0b1000 0000 0000 0000
15
Number Systems
16
Categories of Numbers
• Unsigned
• Signed
• Fractional
17
Unsigned Numbers
• n-bit binary number:
bn-1 bn-2 … b1 b0
MSB LSB
• Decimal value:
bn-1 * 2n-1 + bn-2 * 2n-2 + … + b1 * 21 + b0 * 20
• Example:
0b10011 = 24 + 21 + 20 = 19 (d)
18
Representing Signed number
• Solution 1: Sign magnitude method: Reserve
a bit to represent the sign Sign Magnitude:
000 = 0
• Challenges: 001 = +1
ambiguous zero
• balance – equal number of negatives and positives
010 = +2
• There will be +0 and -0
ambiguous zero – 011 = +3
• How do we determine the sign of a number 100 = 0
after arithmetic (like add) 101 = -1
• Example: -2 + 1= 110+001=111=-3 110 = -2
111 = -3
Magnitude
sign
(value)
19
Representing Signed number
• Solution 2: Using
“two’s complement”
representation
• Eliminates negative
zero and makes
Max Int
arithmetic in hardware
easier
MSB of each +ve is 0
+ve Range: 0 to 231-1
Min Int
After 231 all are -ve
MSB of each –ve is 1
What is the –ve
range?
20
Signed – 2’s Complement Numbers
• n-bit binary number:
bn-1 bn-2 … b1 b0
• Decimal value: MSB LSB
21
• Two’s Complement Numbers
e.g., a 4-bit signed variable:
Positive Values Negative Values
Decimal Binary Decimal Binary
7 0 000 -1 1 111
6 0 001 -2 1 110
5 0 010 -3 1 101
4 0 011 -4 1 100
3 0 100 -5 1 011
2 0 101 -6 1 010
1 0 110 -7 1 001
0 0 111
22
2’s Complement Numbers
9 + (-7) = 2
• Verify: 9 – 7 = 9 + (-7)
• 0b01001
• + 0b11001
• 0b00010= 2
24
https://piolabs.com/blog/insights/debugging-embedded.html
25
Fractional Numbers: Float and
double
Float
• IEEE 754 single precision floating point numbers
• 1-bit sign, 8-bits exponent, 23-bits fraction
• 6 significant decimal digits of precision
Double
• 1-bit sign, 11-bits exponent, 52-bits fraction
• 15-17 significant decimal digits of precision
26
Fractional Numbers: Float and
double
27
Single precision example
2^-1 = 0.5
2^-2 = 0.25
2^-3 = 0.125
2^-4 = 0.0625
https://www.geeksforgeeks.org/ieee-standard-754-floating-point-numbers/
28
Memory
Input Output
• push button • LED
• Keyboard • Monitor
• Sensors • I/O
• Disk • Disk
Processing Unit
ALU Reg
Control Unit
Program Counter (PC) Instruction Register (IR)
Digital Logic
Electronic Circuits
Transistors
31
Motivation to Learn ISA
• Instructions are words used to command computer
hardware
• The collection of this words, or the vocabulary is called
“instruction set”
Why we need to understand this vocabulary?
Provides ability to
Without instruction Useful when examine code at Understanding of
set, the design and working with bare lower level of instruction format is
functionality of metal embedded abstractions and required for compiler
architecture cannot systems solve bugs/security designers
be understood
issues
32
MIPS ISA
• MIPS (Microprocessor without Interlocked Pipelined
Stages) MIPS Technologies, based in the United
States.
33
The memory contains bits that stores:
Program (instructions) & Data
34
Example
1 byte
Address data
0x00 0x00
0x01 0xF1 What is the total storage capacity
0x02 0x11 in terms of bits of a memory with:
• 8-bit addresses
… … • Addressability of 16 bits?
0xFE 0x20
0xFF 0x34
Address space: of 28 unique memory locations
Addressability: 8 bits or byte addressable
35
Example: MIPS Memory
1 bytes
0x0000 0x00
0x0001 0xF1
0x0002 0x11
… …
0xFFFE 0x20
0xFFFF 0xFA
Load &
Integer Jump
Store
Floating Conditional
Point Branch
Call &
Return
37
Assembly to binary translation
Arithmetic Memory Control
access flow
instructions
Integer Jump
Load &
Floating Point Store Conditional Branch
38
Opcode and Operand
• Opcode: operation that is executed by
the CPU (ex: add, sub)
• Operand: data or memory location used
to execute that operation.
39
Arithmetic Instructions
• Most instructions have 3 operands
• Operand order is fixed (destination first)
40
Machine Language: R-type instruction
• Instruction Format:
41
Encoding rules
funct encoding Registers encoding
Operation Hexadecimal Decimal
add 20 32
sub 22 34
sll 00 00
srl 02 02
and 24 36
or 25 37
xor 26 38
nor 27 39
42
Example
add $t0, $t1, $t2
#op $rd, $rs, $rt
43
Register Naming in MIPS
44
Registers
Registers:
• Limited number of memory location connected to ALU
• MIPS32 has 32 registers (there is a MIPS64 also)
• Each of the 32 register has capacity to hold 32 bits (also known as word)
Register Memory
• When we say MIPS in this class we assume the MIPS32
C code: A = B + C;
A = B - C;
M[2
M[1
MIPS code: add $r1, $r2, $r3 M[0
sub $r1, $r2, $r3
45
Register Naming
• Assembly codes use naming convention for the registers
• Helps to understand and debug assembly codes better
• https://godbolt.org/
46