0% found this document useful (0 votes)
3 views

Lecture 3 - Number Systems and ISA r-type EECS 388

Uploaded by

usamog68
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 3 - Number Systems and ISA r-type EECS 388

Uploaded by

usamog68
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

EECS 388: Embedded Systems

Lecture 3 – Number Systems and


Instruction Set Architecture (R-type)
Fall 2024

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)

• To understand a value of a variable and use it properly, a


compiler needs to understand both the raw values and
the type of a variable

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?

Only when two systems with different


endianness want to share data

5
How to store multi-byte variables
(short, int, long, etc.) in memory?
MSB LSB

• int var = 0x49345678;


• assume &var = 0x0;
Big Endian Little Endian
Address data Address data
0x00 0x49 MSB 0x00 0x78 LSB

0x01 0x34 0x01 0x56


0x02 0x56 0x02 0x34
0x03 0x78 LSB 0x03 0x49 MSB

… … … …
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

• Hexadecimal (base 16)


• Symbols: 0,1,…,9,A,B,…,F
• E.g., 12316 = 0x123 = 1x162 + 2x161 + 3x160
• 0x indicates hex
*Assuming unsigned number

9
• Binary to Hex to Decimal conversion!

• 1 hex symbol is 4 binary bits


• 1 hex symbol represents decimal values
from 0 to 15
•0–F
• 0xAB is 8 bits (1 bytes)
• Converted to binary: 1010 10112
• 0xA is 4 bits (not one bytes!)
10
Number Systems

Decimal Hexadecimal Binary


0 0x0 0b0
2
9
0xA
0xF
0x1F
0b1000 0000
0b1000 0011
0b1000 0000 0000 0000

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

Complete Red Green Yellow

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

1F16=1*16^1 + F*16^0= 16+15=3110 Complete Red Green Yellow

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

Starting from LSB, each 4-bit binary is a Hex digit.

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

Decimal Hexadecimal Binary


0 0x0 0b0
2 0x2 0b10
9 0x9 0b1001
10 0xA 0b1010
15 0xF 0b1111
31 0x1F 0b1 1111
128 0x80 0b1000 0000
131 0x83 0b1000 0011
32768 0x8000 0b1000 0000 0000 0000

Starting from LSD, each Hex digit is a 4-bit binary

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

- (bn-1 * 2n-1) + bn-2 * 2n-2 + … + b1 * 21 + b0 * 20


• Example:
0b1111 0011 = - (24) + 21 + 20 = -13 (d)
• Trick (Number -> complement -> add 1:
1310 -> 0000 1101 -> 1111 0010 -> 1111 0011-> 1111 0011

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: Add -7 and 9 using 2’s complement


• 9 in 2’s complement =0b01001 (requires 5 bits at least)
• 7 in unsigned binary =0b00111; (used 5 bits to match with 9)
• thus: -7 in signed binary is
• Number>Complement->Add 1>Signed bit in MSB
• -7->0b0111->0b1000->0b1001->0b11001

• 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)

The Von Neumann computer model


29
Von Neumann model has five parts:

• Memory Computer program and data resides here


Processing is carried out here
• Processing Unit
e.g., Add, Sub, compare, …
Keep track of where we are in the
• Control unit
process of running a program
• Input Provide the information for processing

• Output Store processed data or actuate upon


the data
30
Context
• Recommended reading
Application Program
Chapter 2 of “Computer Organization
and Design” Patterson, Hennessy
Operating
Compiler System

Machine Language (ISA)

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.

• Different versions of MIPS: MIPS I, II, III, IV, and V


with 32 and 64 version

• Widely adopted by the embedded market

• HiFive1 Rev B in the lab has processor with


RISC-V, which is very similar to MIPS
• RISC-reduced instruction set computer
• MIPS is derived from RISC-1 ISA
• Example processors with MIPS ISA
• PIC32, ATI/AMD Xilleon, Broadcom Sentry5,
R4700 Orion

33
The memory contains bits that stores:
Program (instructions) & Data

Each location has


- Address: A unique bit patter for identifying
a memory location
Address space: Total number of addresses of a memory
• E.g., n-bit address width => 2𝑛𝑛 address space
- Content: bits stored at the memory address
Addressability: Number of bits stored at each address
• E.g., 8-bit addressable or “byte addressable”

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

Address space: of 216 unique memory locations


Addressability: 8 bits or byte addressable
36
Classes of Instructions in MIPS
R-type I-type J-type
Memory Control
Arithmetic access
instructions flow

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

Call & Return

For more details: https://www.comp.nus.edu.sg/~adi-yoga/CS2100/ch06c/

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)

• All arithmetic instructions in MIPS must use


registers
• CPU uses registers to store variable a, b, c

40
Machine Language: R-type instruction

• Instructions, like registers and words of op rs rt rd shamt funct


data, are also 32 bits long
• Example: add $t0, $s1, $s2 000000 10001 10010 01000 00000 100000
• Registers have numbers: $t0=9,
$s1=17, $s2=18 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

• 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

Fields Decimal Value Binaries


opcode 0 000000
$rs 9 01001
$rt 10 01010
$rd 8 01000
shamt 0 00000
funct 32 100000
Example from: https://www.comp.nus.edu.sg/~adi-yoga/CS2100/ch06c1/

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

Is 64 register better than 32?


• Design Principle: smaller is faster.
What is the reason behind the design principle “smaller is faster”?

45
Register Naming
• Assembly codes use naming convention for the registers
• Helps to understand and debug assembly codes better

• https://godbolt.org/

46

You might also like