03 Cpu Overview
03 Cpu Overview
Fall 2024
1
Overview
n CPU overview
n Cortex-M0+ Processor Core
n Cortex-M0+ Processor Core Registers
n Memory System and Addressing
n Thumb Instruction Set
2
CPU OVERVIEW
Central Processing Unit (CPU)
n CPU is the fundamental execution/processing unit
of the computer
n CPU consists of ALU, Control Unit, and Registers
n CPU is characterized by:
n Clock frequency
n Speed
n Data bus width
n Instruction set
n Addressing capability
n Addressing capacity
Internal Structure of CPU
n Intel 4004
n in 1971, commercially available single-chip microprocessor
Example:
Motorola 6802
Registers in the Fetch Unit
n Program Counter: holds the memory location
of the next instruction.
n Instruction Register: holds the current
instruction being executed
Instruction Decoder
n It decodes the instructions and generates the
control signals
Arithmetic Logic Unit (ALU)
n ALU performs all arithmetic and logic
operations in a microprocessor
n ALU has two inputs (A, B) for the operands
and one input for a control signal that selects
the operation
n Operation and Shift control bits determine,
which type of operation to perform (F)
n Output is the result of operation (R) and
status information (D)
n Status information is used to indicate cases
n Zero: if all result lines have value 0
n Overflow: integer overflow of add and
subtract functions
n For unsigned integers, it does not provide any
useful information
Registers
n A register is a storage location in the CPU
n It is used to hold data or a memory address during
the execution of an instruction
n Because the register file is small and close to the
ALU, accessing data in registers is much faster than
accessing data in memory outside the CPU
n The register file makes program execution more
efficient
n The number of registers varies from computer to
computer
Condition Code Register or Flag Register
n Depending on the outcomes of Arithmetic or Logical
operations, we can branch and jump
n The eight-bit Condition Code Register (CCR) provides a
status report on the ALU's activity
n Carry/Borrow
n Half carry from bit 3 to bit 4
n oVerflow
n CCR also provides a status report after loading ACC
n Zero
n Negative
V Z N H C
Condition Code Register (CCR)
n They flag certain conditions resulting from the ALU
outcomes
n Example:
A= 01001000 B= 01111001
A+B:
A 01001000
B +01111001
11000001 V=1 Z=0 N=1 H=1 C=0
BOOK2 BOOK2
BOOK1 BOOK1
n You can also take a book off the top of the stack:
BOOK3
BOOK3
BOOK2 BOOK2
BOOK1 BOOK1
Memory
n Microcontroller has Debug
Interface
Interfaces Timers
n Non-logic level
signals
n Timing
n Clock generators
n Communications
n point to point
n network
n Reliability and safety
Cortex-M0+ Core
An ISA defines the hardware/software interface
27
Arm Architecture
n In addition, the ARM architecture provides:
n control over both the Arithmetic Logic Unit (ALU) and
shifter in most data-processing instructions to maximize
the use of an ALU and a shifter
n auto-increment and auto-decrement addressing modes
throughput
n conditional execution of almost all instructions to
Mode dependent
ARM Processor Core Registers (32 bits each)
n R0-R12 - General purpose registers for data
processing
n SP - Stack pointer (R13)
n Can refer to one of two SPs
n Main Stack Pointer (MSP)
n Process Stack Pointer (PSP)
n Uses MSP initially, and whenever in Handler mode
n When in Thread mode, can select either MSP or PSP
using SPSEL flag in CONTROL register.
n LR - Link Register (R14)
n Holds return address when called with Branch & Link
instruction (B&L)
n PC - program counter (R15)
Operating Modes
Reset
Thread
Mode.
MSP or PSP.
Excep%on Star%ng
Processing Excep%on
Completed Processing
Handler
Mode
MSP
SRAM_U (3/4)
16 KB SRAM
0x2000_0000
SRAM_L (1/4)
0x1FFF_F000
0x0001_FFFF
128KB Flash
0x0000_0000
Endianness
n For a multi-byte
value, in what order
are the bytes stored?
n Little-Endian: Start
with least-significant
byte
n Examples
n ADDS <Rd>, <Rn>, <Rm>
n Add registers: <Rd> = <Rn> + <Rm>
n AND <Rdn>, <Rm>
n Bitwise and: <Rdn> = <Rdn> & <Rm>
n CMP <Rn>, <Rm>
n Compare: Set condition flags based on result of computing
<Rn> - <Rm>
Where Can the Operands Be Located?
n In a general-purpose register R
n Destination: Rd
n Source: Rm, Rn
n Both source and destination: Rdn
n Target: Rt
n Source for shift amount: Rs
n An immediate value encoded in instruction word
n In a condition code flag
n In memory
n Only for load, store, push and pop instructions
Update Condition Codes in APSR?
51
Loading/Storing Smaller Data Sizes
n Some load and store instructions can handle half-word (16
bits) and byte (8 bits)
n Store just writes to half-word or byte
n STRH, STRB
n Loading a byte or half-word requires padding or extension:
What do we put in the upper bits of the register?
n Example: How do we extend 0x80 into a full word?
n Unsigned? Then 0x80 = 128, so zero-pad to extend to word
0x0000_0080 = 128
n Signed? Then 0x80 = -128, so sign-extend to word 0xFFFF_FF80
= -128
Signed Unsigned
Byte LDRSB LDRB
Half-word LDRSH LDRH
In-Register Size Extension
n Can also extend byte or half-word that is already in a
register
n Signed or unsigned (zero-pad)
n How do we extend 0x80 into a full word?
n Unsigned? Then 0x80 = 128, so zero-pad to extend to word
0x0000_0080 = 128
n Signed? Then 0x80 = -128, so sign-extend to word
0xFFFF_FF80 = -128
Signed Unsigned
Byte SXTB UXTB
Half-word SXTH UXTH
Example
n SXTB R0, R1; R0 = Sign Extend (R1[7:0])
n SXTH R0, R1; R0 = Sign Extend (R1 [15:0])
54
Load/Store Multiple
56
Example for LDM/STM
n This example demonstrates how to use the LDM/STM
instructions that do modify the base registers. The only
functional difference between the previous example and this
example is that R0 and R7 both get modified.
59
Move (Pseudo-)Instructions
n Copy data from one register to
another without updating
condition flags
n MOV <Rd>, <Rm>
n Assembler translates pseudo-
instructions into equivalent
instructions (shifts, rotates)
n Copy data from one register to
another
and update condition flags
n MOVS <Rd>, <Rm>
61
Stack Operations
n Push some or all of registers (R0-R7, LR) to stack
n PUSH {<registers>}
n Decrements SP by 4 bytes for each register saved
n Pushing LR saves return address
n PUSH {r1, r2, LR}
n Always pushes registers in same order
n Pop some or all of registers (R0-R7, PC) from stack
n POP {<registers>}
n Increments SP by 4 bytes for each register restored
n If PC is popped, then execution will branch to new PC value
after this POP instruction (e.g. return address)
n POP {r5, r6, r7}
n Always pops registers in same order (opposite of pushing)
Examples
n PUSH { R0, R1, R2 }; Memory [SP- 4]= R2,
; Memory [SP- 8]= R1,
; Memory [SP- 12]= R0,
; SP = SP-12
63
Add Instructions
n Add registers, update condition flags
n ADDS <Rd>,<Rn>,<Rm>
n Add registers and carry bit, update condition
flags
n ADCS <Rdn>,<Rm>
n Add registers
n ADD <Rdn>,<Rm>
n Add immediate value to register
n ADDS <Rd>,<Rn>,#<imm3>
n ADDS <Rdn>,#<imm8>
Add Instructions with Stack Pointer
n Add SP and immediate value
n ADD <Rd>,SP,#<imm8>
n ADD SP,SP,#<imm7>
66
Address to Register Pseudo-Instruction
n Add immediate value to PC, write result in
register
n ADR <Rd>,<label>
n How is this used?
n Enables storage of constant data near program
counter
;First, load register R2 with address of const_data
ADR R2, const_data
;Second, load const_data into R2
LDR R2, [R2]
n Value must be close to current PC value
Subtract
n Subtract immediate from register, update condition
flags
n SUBS <Rd>,<Rn>,#<imm3>
n SUBS <Rdn>,#<imm8>
n Subtract registers, update condition flags
n SUBS <Rd>,<Rn>,<Rm>
n Subtract registers with carry, update condition flags
n SBCS <Rdn>,<Rm>
n Subtract immediate from SP
n SUB SP,SP,#<imm7>
Examples
n 64-bit addition
ADDS R0, R0, R2 ; add the least significant words
ADCS R1, R1, R3 ; add the most significant words with carry
n Multiword values do not have to use consecutive registers.
n Example shows instructions that subtract a 96-bit integer contained
in R1, R2, and R3 from another contained in R4, R5, and R6.
n The example stores the result in R4, R5, and R6.
n 96-bit subtraction
SUBS R4, R4, R1 ; subtract the least significant words
SBCS R5, R5, R2 ; subtract the middle words with carry
SBCS R6, R6, R3 ; subtract the most significant words with carry
n Example shows the RSBS instruction used to perform a 1's complement of
a single register
n Arithmetic negation
RSBS R7, R7, #0 ; subtract R7 from zero
Multiply
n Multiply source registers, save lower word of
result in destination register, update condition
flags
n MULS <Rdm>, <Rn>, <Rdm>
n <Rdm> = <Rdm> * <Rn>
n Signed multiply
n Note: upper word of result is truncated
74
Reversing Bytes
MSB LSB
n REV - reverse all bytes
in word
MSB LSB
n REV <Rd>,<Rm>
n REV16 - reverse bytes in
both half-words MSB LSB
n REV16 <Rd>,<Rm>
n REVSH - reverse bytes MSB LSB
in low half-word
(signed) and sign- MSB LSB
extend Sign extend
n REVSH <Rd>,<Rm> MSB LSB
Examples
n REV R3, R7 ; Reverse byte order of value in R7 and write it to R3
n REV16 R0, R0 ; Reverse byte order of each 16-bit half-word in R0
n REVSH R0, R5 ; Reverse signed half-word
Changing Program Flow - Branches
n Branches (conditional and unconditional)
n Branch without link (i.e. no possibility of return) to target
n The PC is not saved!
n Unconditional Branches
n B <label>
n Target address must be within 2 KB of branch instruction
(-2048 B to +2046 B)
n Conditional Branches
n B<cond> <label>
n <cond> is condition - see next page
n B<cond> target address must be within of branch
instruction
n B target address must be within 256 B of branch
instruction (-256 B to +254 B)
Condition Codes
n Append to branch
instruction (B) to make
a conditional branch
80
Special Register Instructions
n Move to Register from
Special Register
n MSR <Rd>, <spec_reg>
...
start:
movs r0, #1
movs r1, #1
movs r2, #1
sub r0, r1
bne done
movs r2, #2
done:
b done
...
Solution: What is the value of r2 at done?
...
start:
movs r0, #1 // r0 ! 1, Z=0
movs r1, #1 // r1 ! 1, Z=0
movs r2, #1 // r2 ! 1, Z=0
sub r0, r1 // r0 ! r0-r1
// but Z flag untouched
// since sub vs subs
bne done // NE true when Z==0
// So, take the branch
movs r2, #2 // not executed
done:
b done // r2 is still 1
...
An example ARM assembly language program for GNU
.equ STACK_TOP, 0x20000800
.text
.syntax unified
.thumb
.global _start
.type start, %function
_start:
.word STACK_TOP, start
start:
movs r0, #10
movs r1, #0
loop:
adds r1, r0
subs r0, #1
bne loop
deadloop:
b deadloop
.end
What’s it all mean?
_start:
.word STACK_TOP, start /* Inserts word 0x20000800 */
/* Inserts word (start) */
start:
movs r0, #10 /* We’ve seen the rest ... */
movs r1, #0
loop:
adds r1, r0
subs r0, #1
bne loop
deadloop:
b deadloop
.end
86