Microprocessors & Interfacing
Basics of Computing with Microprocessor Systems
COMP9032 Week1 1
Copyright By PowCoder代写 加微信 powcoder
Lecture Overview
• Basic Microprocessor Hardware Structure
• Data Representation
– Hexadecimal
• Instruction Set Architecture
COMP9032 Week1 2
Fundamental Hardware Components in Computing System
• ALU: Arithmetic and Logic Unit
• RF: Register File (a set of registers) • CU: Control Unit
• IM/DM: Instruction/Data Memory
• I/O: Input/Output Devices
COMP9032 Week1 3
Execution Cycle
IF: Instruction Fetch ID: Instruction Decode RR: Read Register File EX: Execution
WR: Write Result NPC: Next Instruction
Note: Steps can be merged/broken down/expanded
COMP9032 Week1 4
Microprocessor
• A microprocessor is the datapath and control unit on a single chip.
– Note, it often includes other components for functional and performance enhancement
• If a microprocessor, its associated support circuitry, memory and peripheral I/O components are implemented on a single chip, it is a microcontroller.
– We use Atmel AVR microcontroller as the example in our course
COMPAtmel9032 Week1 5
Data Representation
• For a digital microprocessor system to be able to compute and process data, the data must be properly represented
– How to represent numbers for calculation?
• Binary number • Binary code
– How to represent characters, symbols and other values for processing?
• Will be covered later
COMP9032 Week1 6
=123 +022 +12+1
– All digits (aka bits) must be less than 2 (0~1). What are the first 16 binary integers?
COMP9032 Week1 7
Hexadecimal
=F163 +2162 +416+B =15163 +2162 +416+11
– All digits must be less than 16 (0~9,A,B,C,D,E,F)
• Conversion between binary to hexadecimal
– One hexadecimal digit→4 binary digits – E.g. 0xA1
COMP9032 Week1 8
Binary vs Decimal
• Conversion
– binary to decimal
• Based on the expanded form
anan−1…a1a0 𝑟
=an ×rn +an−1 ×rn−1 +…+a1 ×r+a0
r:radix,base 0 ≤ ai < r
– decimal to binary
• Repeated division by 2
– Each division generates a remainder→binary digit
COMP9032 Week1 9
• To binary
– To convert (11)10 to binary
11 1 51 20 11 0
(11)10=(1011) 2
COMP9032 Week1 10
Hexadecimal vs Decimal
• Conversion
– Hexadecimal to decimal
• Based on the expanded form
anan−1...a1a0 𝑟
=an ×rn +an−1 ×rn−1 +...+a1 ×r+a0
r:radix,base 0 ≤ ai < r
– decimal to hexadecimal
• Repeated division by 16
– Each division generates a remainder→hexadecimal digit
COMP9032 Week1 11
– To convert (99)10 to hexadecimal
(99)10=(63) hex
• To hexadecimal
COMP9032 Week1 12
Binary Arithmetic Operations
• Similar to decimal operations
• Examples of addition and multiplication are given in the next two slides.
COMP9032 Week1 13
Binary Addition
• Example:
– Addition of two 4-bit binary numbers. How many bits are required for holding the result?
1001+0110 = (__________)
COMP9032 Week1 14
Binary Multiplication
• Example:
– Multiplication of two 4-bit unsigned binary numbers. How many bits are required for holding the result?
1001*0110 = (____________________)
COMP9032 Week1 15
Binary Subtraction
• Subtraction can be defined as addition of the additive inverse (namely signed addition)
a – b = a + (-b)
• We use two’s complement code/number, b*, to
represent -b.
– for n-bit number
b* = 2n – b = +1
– The MSB (Most Significant Bit) of a 2’s complement code is
the sign bit
• For example, for a 4-bit 2’s complement number
• (1001) → -7, (0111) → 7
bit-wise inversion
(2n -1) – b
COMP9032 Week1 16
Exercise 1
• For each of the following decimal numbers, what is its 8-bit 2’s complement number?
(a) 7 (b) 127 (c) -12
• An n-bit binary number can be interpreted in two different ways: signed (i.e. 2’s complement number) or unsigned. What decimal value does the 4-bit number, 1011, represent in each of the following two cases?
(a) if it is a signed number
(b) if it is an unsigned number
COMP9032 Week1 17
Exercise 1
• For each of the following decimal numbers, what is its 8-bit 2’s complement number?
(a) 7 (b) 127 (c) -12
• An n-bit binary number can be interpreted in two different ways: signed (i.e. 2’s complement number) or unsigned. What decimal value does the 4-bit number, 1011, represent in each of the following two cases?
(a) if it is a signed number
(b) if it is an unsigned number
COMP9032 Week1 18
Signed Addition
• E.g. 4-bit 2’s-complement additions/subtractions
(1) 0101 + 0010 (5 + 2): 0101
+ 0010 = 00111
(3) 0010-0101(2-5): 0010
+ 1011 (= 0101*) = 1101 (= 0011*). Result means -3.
0101 - 0010 (5 - 2): 0101
+ 1110 (= 0010*) = 10011
(4) -0101 - 0010 (-5 - 2):
1011 (= 0101*) + 1110 (= 0010*) = 11001
Result means -7.
COMP9032 Week1
Signed Addition
• E.g.4-bit2’s-complementadditions/subtractions
(1) 0101 + 0010 (5 + 2): 0101
+ 0010 = 00111
(3) 0010-0101(2-5): 0010
+ 1011 (= 0101*) = 1101 (= 0011*). Result means -3.
(2) 0101 - 0010 (5 - 2): 0101
+ 1110 (= 0010*) = 10011
(4) -0101 - 0010 (-5 - 2):
1011 (= 0101*) + 1110 (= 0010*) = 11001
Result means -7.
COMP9032 Week1
• In digital computer systems, values are represented by a fixed number of bits.
• Overflow happens when the calculation result is beyond the range that can be represented with the given number size.
COMP9032 Week1 21
Exercise 2
For the following 4-bit signed calculations, check whether there are any overflows.
1) 1000-0001 2) 1000+0101 3) 0101+0110
COMP9032 Week1 22
Microprocessor Applications
• A microprocessor application system can be abstracted in a three-level structure
– ISA (Instruction Set Architecture) is the interface between hardware and software
C program compiled
to ISA program
Assembly program
ISA program executed by hardware
COMP9032 Week1
Instruction Set
• Instruction set provides the vocabulary and grammar for programmer/software to communicate with the hardware machine.
• It is machine oriented
– Different type of machines have a different instruction set
• For example
– 68K has a more comprehensive instruction set than ARM machine
– Same operations could be represented differently in different machines
– Addition:
– Branching: – Load:
– Addition:
– Branching: – Load:
add r2, r1 breq 6
ldi r30, $F0
add d1,d2 breq 6
mov #1234, d2
;r2 r2+r1
;branch if equal condition is true ;r30 F0
;d2 d2+d1
;branch if equal condition is true ;d21234
COMP9032 Week1
Instructions
• Instructions can be written in two languages
– Machine language
• Binary representation • Used by machines
– Assembly language
• Textual representation
• Easier to understand than machine language • Used by human being.
COMP9032 Week1 25
Machine Code vs. Assembly Code
• Basically, there is a one-to-one mapping between machine instructions and assembly instructions
– For example, AVR instruction for incrementing register r16 by 1:
• 1001010100000011 (machinecode) • incr16 (assemblycode)
• Assembly language also includes directives
– Directives
• Instructions to the assembler
– Assembler is a program to translate assembly code into machine code.
• Example:
– .def temp = r16
– .include “m2560def.inc”
COMP9032 Week1 26
Instruction Set Architecture (ISA)
ISA specifies all aspects of a computer architecture visible to a programmer
– Instructions (just mentioned) – Native data types
– Registers
– Memory models
– Addressing modes
COMP9032 Week1 27
Native Data Types
• Different machines support different data types in hardware
• e.g. Pentium II:
Signed integer
Unsigned integer
BCD integer
Floating point
• e.g. Atmel AVR (we are using):
Signed integer
Unsigned integer
BCD integer
Floating point
COMP9032 Week1 28
• Two types
– General purpose – Special purpose
– Program Counter (PC) – Status Register
– Stack Pointer (SP)
– Input/Output Registers
– Stack Pointer and Input/Output Registers will be discussed in detail later.
COMP9032 Week1 29
General Purpose Registers
• A set of registers in the machine
– Used for storing temporary data/results – For example
• In (68K) instruction add d3, d5, the operands of the operation are stored in general registers d3 and d5, and the result is stored in d5.
• Can be structured differently in different machines
– For example
• Separate general-purpose registers for data and address
• Different number of registers and different size of registers
– 32 32-bit registers in MIPS – 16 32-bit registers in ARM
COMP9032 Week1 30
Program Counter (PC)
• Special register
– For storing the memory address of currently executed instruction
• Can be of different size
– E.g. 16 bits, 32 bits
• Can be auto-incremented
– By the instruction word size
– Hence, giving rise to the name “counter”
COMP9032 Week1 31
Status Register
• Contains a number of bits with each bit being associated with processor (CPU) operations
• Typical status bits
– V: Overflow – C: Carry
– N: Negative
• Used for controlling the program execution flow
COMP9032 Week1 32
Memory Model
• Related to how memory is used to store data
– Addressable unit size – Address spaces
– Endianness
– Alignment
COMP9032 Week1 33
Addressable Unit Size
• Memory has units, each of which has an address
• Most basic unit size is 8 bits (1 byte)
– Related addresses are called byte-addresses.
• Modern processors can have multiple-byte unit
– e.g. 32-bit instruction memory in MIPS 16-bit instruction memory in AVR
– Related addresses are called word-addresses. COMP9032 Week1 34
Address Space
• The range of addresses a processor can access.
– A processor can have one or more address spaces. For example
• Princeton architecture or architecture
– A single linear address space for both instructions and data memory
• Harvard architecture
– Separate address spaces for instruction and data memories
COMP9032 Week1 35
Address Space (cont.)
• Address space is not necessarily just for “memory”
– E.g, all general-purpose registers and I/O registers can be accessed through memory addresses in AVR
COMP9032 Week1 36
Endianness
• Memory objects
– Memory objects are basic entities that can be accessed as a function of the address and the size
• E.g. bytes, words, longwords
• For large objects (multiple bytes), there are two byte-ordering conventions
– Little endian – little end (least significant byte) stored first (at lowest address)
• Intel microprocessors (Pentium etc)
– Big endian – big end (most significant byte) stored first
• SPARC, Motorola microprocessors
COMP9032 Week1 37
Big Endian & Little Endian
• Example: 0x12345678—a long word of 4 bytes. It is stored in the memory from a byte address 0x00000013
– big endian:
Address data 0x00000013 0x12 0x00000014 0x34 0x00000015 0x56 0x00000016 0x78
– little endian:
Address data 0x00000013 0x78 0x00000014 0x56 0x00000015 0x34 0x00000016 0x12
COMP9032 Week1 38
• Modern computers read from or write to a memory address in fix-sized chunks
– for example, word size
• Alignment improves the memory access efficiency by putting the data at a memory address that is multiple of the chunk size
– for example, with AVR, data of the word type in the program memory are aligned with the word addresses.
COMP9032 Week1 39
Addressing Mode
• Instructions need to specify where to get operands
• Some possible ways
– an operand value is in the instruction
– an operand value is in a register
• the register number is given in the instruction
– an operand value is in memory
• address is given in the instruction
• address is given in a register
– the register number is in the instruction
• address is a register content plus some offset
– register number is in the instruction
– offset is in the instruction (or in a register)
• These ways of specifying the operand locations are called addressing mode.
COMP9032 Week1 40
Addressing Mode (cont.)
• Some addressing mode examples, based on the 68K machine, are given in the next slides.
– Using instruction: addw a, b
• addition on operands of the word size, b a+b
• For each addressing mode, there are
– a general description and
– an example to show how the address mode is used.
• the specified addressing mode for the first operand of instruction is highlighted in red.
COMP9032 Week1 41
Immediate Addressing
• The data is from the instruction
– i.e the operand is immediately available from the instruction
• For example, in 68K
addw #99, d7
– d799 + d7; value 99 comes from the instruction – d7 is a register
COMP9032 Week1 42
Register Direct Addressing
• The data is from a register, and the register is directly given by the instruction
• For example, in 68K
– d7d7 + d0; add the value in d0 to the value in d7 and store the result to d7
– d0 and d7 are registers
addw d0,d7
COMP9032 Week1 43
Memory Direct Addressing
• The data is from memory, and the memory address is directly given by the instruction
• We use notion: (addr) to represent memory value at address, addr
• For example, in 68K
– d7d7 + (0x123A); add value in memory location 0x123A to register d7
addw 0x123A, d7
COMP9032 Week1 44
Memory Register Indirect Addressing
• The data is from memory, and the memory address is given by a register, which is given by the instruction
• For example, in 68K
– d7d7 + (a0); add value in memory with the address stored in register a0, to register d7
• For example, if a0 = 100 and (100) = 123, then this adds 123 to d7
addw (a0),d7
COMP9032 Week1 45
Memory Register Indirect Auto- increment
• The data is from memory, and the memory address is given by a register, which is given by the instruction; and the value of the register is automatically increased – to point to the next memory object.
• For example, in 68K
– d7d7 + (a0); a0a0 + 2
COMP9032 Week1 46
addw (a0)+,d7
Memory Register Indirect Auto- decrement
• The data is from memory, and the memory address is given by a register, which is given by the instruction; but the value of the register is automatically decreased before such an operation.
• For example, in 68K
– a0a0 –2; d7d7 + (a0);
COMP9032 Week1 47
addw -(a0),d7
Memory Register Indirect with Displacement
• The data is from memory with the address given by the register plus an offset
– Used to access a member in a data structure
• For example, in 68K
– d7(a0+8) +d7
COMP9032 Week1 48
Address Register Indirect with Index and Displacement
• The address of the data is sum of a base address and an index address and an offset
– Used to access elements in an array
• For example, in 68K
– d7(a0 + d3+8) + d7
– With a0 as an initial address and d3 varied to dynamically point to different elements plus a constant for a certain member of an element of an array.
COMP9032 Week1 49
Suggested Readings
– Cady “Microcontrollers and Microprocessors”, Chapter 1.1, Chapter 2.2-2.4
– Cady “Microcontrollers and Microprocessors”, Appendix A
– Week 1 reference: “Number Conversion”
• available on the course website
COMP9032 Week1 50
1. Install Atmel Studio at home and complete lab0
• Available on the Labs page on the course website
2. Complete Quiz 1
– Released after the lecture
– Available on the Activities page on the course website
COMP9032 Week1 51