2023 FALL ELEC334 L03 STD
2023 FALL ELEC334 L03 STD
11/1/2023
1 2
3 4
Student Copy. For personal studies only. Do not distribute. 11/1/2023
5 6
7 8
Student Copy. For personal studies only. Do not distribute. 11/1/2023
Review: ARM Cortex M0+ 2‐stage pipeline Review: ARM M0+ Regular & Special Registers
In Cortex‐M0+, there are 16x 32‐bit registers (13 general
and 3 special use)
○ R0 ‐ R15 with R0‐R12
being general purpose
registers.
○ due to size, mostly only
low registers can be used
with 16‐bit Thumb code.
○ data needs to be loaded
from memory to
registers to be
processed (load/store)
9 10
Review: ARM Cortex M0+ Special Use Registers Review: ARM Cortex M0+ Special Registers
R13 ‐ Stack Pointer (SP) ‐ points to the top of the stack
‐ POP and PUSH instructions are used to access memory.
‐ Banked ‐ Main (MSP) and Process Stack Pointer (PSP). Can be
changed, but only one of them is visible
‐ Special register access instructions MRS and MSR is used to
change between them.
‐ Access is 32‐bits so lowest 2 bits are always 0.
R14 ‐ Link Register (LR) ‐ keeps the return address of a subroutine
or function call
‐ Lowest bit is usually set to 1 to indicate Thumb state. Otherwise
it can imply an attempt to switch the processor to ARM state ‐
which is not supported ‐ thus can cause a fault.
R15 ‐ Program Counter (PC) ‐ points to the next instruction in line
‐ Lowest bit is usually set to 1 to indicate Thumb state. Otherwise
it can imply an attempt to switch the processor to ARM state ‐
which is not supported ‐ thus can cause a fault.
Microprocessors By Dr. Ihsan Cicek 11 Microprocessors By Dr. Ihsan Cicek 12
11 12
Student Copy. For personal studies only. Do not distribute. 11/1/2023
Review: ARM Cortex M0+ Memory System Review: Stack Memory Operation
• Cortex‐M processors have 4 GB of memory address space. ● Stack memory is a memory usage mechanism that allows the
• Memory space is architecturally defined into a number of regions system memory to be used as temporary data storage that behaves
as a first‐in‐last‐out (FILO) buffer.
• As a result, all the Cortex‐M devices have the same programming
model for interrupt control and debug. ● Content can be added with PUSH instruction and can be acquired
using POP instruction
● Usually located at the top of SRAM, and grows downwards ‐
meaning if data is added it gets filled toward lower addresses (full‐
descending)
● Stack Pointer shows the top of the stack
13 14
15 16
Student Copy. For personal studies only. Do not distribute. 11/1/2023
17 18
19 20
Student Copy. For personal studies only. Do not distribute. 11/1/2023
21 22
23 24
Student Copy. For personal studies only. Do not distribute. 11/1/2023
25 26
27 28
Student Copy. For personal studies only. Do not distribute. 11/1/2023
29 30
31 32
Student Copy. For personal studies only. Do not distribute. 11/1/2023
Exercise Exercise
Q1. R1 holds 0x13 and R2 holds 0x24 Swap them. Q1. R1 holds 0x13 and R2 holds 0x24 Swap them.
MOV R3, R1 /* R3 = 0x13 */
Q2. Write 0x24 into R9. MOV R1, R2 /* R1 = 0x24 */
MOV R2, R3 /* R2 = 0x13 */
33 34
35 36
Student Copy. For personal studies only. Do not distribute. 11/1/2023
Exercise Exercise
Q1. Change Main Stack Pointer to Process Stack Q1. Change Main Stack Pointer to Process Stack
Pointer Pointer
MSR is used to write to CONTROL Reg, but we need a MSR is used to write to CONTROL Reg, but we need a
reg to hold data
reg to hold data
MOVS R7, #0x02 //0x0000_0010, bit1=1
MSR CONTROL, R7
Q2. Change Process Stack Pointer to Main Stack
Pointer
Q2. Change Process Stack Pointer to Main Stack
Pointer
37 38
39 40
Student Copy. For personal studies only. Do not distribute. 11/1/2023
41 42
Exercise Exercise
Q1. Read from memory location 0x24 to R0 using Q1. Read from memory location 0x24 to R0 using
different offset methods different offset methods
MOVS R1, #0x24/* R1 = 0x24 */
LDR R0, [R1,0] /* R0 = [0x24+0] = LDR R0, [R1] */
43 44
Student Copy. For personal studies only. Do not distribute. 11/1/2023
Exercise Exercise
Q1. Read from memory location 0x24 to R0 using Q1. Read from memory location 0x24 to R0 using
different offset methods different offset methods
MOVS R1, #0x24/* R1 = 0x24 */
MOVS R1, #0x24/* R1 = 0x24 */
LDR R0, [R1,0] /* R0 = [0x24+0] = LDR R0, [R1] */
LDR R0, [R1,0] /* R0 = [0x24+0] = LDR R0, [R1] */
MOVS R1, #0x20/* R1 = 0x24 */
MOVS R1, #0x20/* R1 = 0x24 */ LDR R0, [R1,#1] /* 1 << 2 will be 0b100=4 so R0 = [0x20+4] */
LDR R0, [R1,#1] /* 1 << 2 will be 0b100=4 so R0 = [0x20+4] */
MOVS R1, #0x24/* R1 = 0x24 */
MOVS R2, #0x00/* R2 = 0x00 */
LDR R0, [R1,R2] /* R0 = [R1+R2] so R0 = [0x24+0] */
45 46
47 48
Student Copy. For personal studies only. Do not distribute. 11/1/2023
Exercise Exercise
Q1. Memory address 0x20000024 holds data 1124 Q1. Memory address 0x24 holds data 1124
Memory address 0x28 holds data 749812
Memory address 0x20000028 holds data 749812
mem[0x20000024] = 1124,
mem[0x20000024] = 1124, mem[0x20000028] = 749812
mem[0x20000028] = 749812 Swap them.
Swap them. LDR R0, =0x20000024
LDR R1, =0x20000028
LDR R2, [R0] //Load from mem to R2 = 1124
LDR R3, [R1] // Load from mem to R3 = 749812
STR R3, [R0] // mem[0x20000024] = 749812
STR R2, [R1] // mem[0x20000028] = 1124
49 50
51 52
Student Copy. For personal studies only. Do not distribute. 11/1/2023
53 54
Exercise Exercise
Q1. mem[0x20000024] = 15, mem[0x2000028] = 18, Q1. mem[0x20000024] = 15, mem[0x20000028] = 18,
mem[0x2000002C] = 17, mem[0x20000030] = 16
mem[0x2000002C] = 17, mem[0x20000030] = 16 Find their sum and save the result into mem[0x20000034]
Find their sum and save the result into LDR R0, =0x20000024
mem[0x20000034] LDR R1, [R0] //Load from mem to R1 = 15
LDR R2, [R0, 1] //Load from mem+4 to R2=18
ADDS R1, R1, R2 //R1 = R2 + R1 = 33
LDR R2, [R0, 2] //Load from mem+8 to R2 = 17
ADDS R1, R1, R2 //R1 = R2 + R1 = 50
LDR R2, [R0, 3] //Load from mem+12 to R2 = 16
ADDS R1, R1, R2 //R1 = R2 + R1 = 66
STR R1, [R0, 4] // Store R1 = 66 into mem+16 mem[0x20000034]
55 56
Student Copy. For personal studies only. Do not distribute. 11/1/2023
57 58
59 60
Student Copy. For personal studies only. Do not distribute. 11/1/2023
Exercise Exercise
Q1. mem[0x50002024] = 0x14142424, Set bit 4 Q1. mem[0x50002024] = 0x14142424, Set bit 4
61 62
Exercise Exercise
Q2. mem[0x50002024] = 0x141C2424, Clear bit 19 Q2. mem[0x50002024] = 0x141C2424, Clear bit 19
63 64
Student Copy. For personal studies only. Do not distribute. 11/1/2023
65 66
Exercise Exercise
Q1. mem[0x50002024] = 0x141C2424, Set bit 12 Q2. mem[0x50002024] = 0x141C2424, Clear bit 19
67 68
Student Copy. For personal studies only. Do not distribute. 11/1/2023
69 70
Exercise Exercise
Q1. mem[0x20000024] = 15, mem[0x20000028] = 18, Q1. mem[0x20000024] = 15, mem[0x20000028] = 18, …
mem[0x20000040] = 11, Find the sum and save it in mem[0x20000044].
mem[0x2000002C] = 17, mem[0x20000030] = 16, LDR R0, =0x20000024 //Load base address to R0
mem[0x20000034] = 14, mem[0x20000038] = 12, … LDR R1, [R0] // R1 = 15
mem[0x2000003C] = 13, mem[0x20000040] = 11, MOVS R7, #0 // Loop counter = 0
Find the sum and save it in mem[0x20000044]. loop:
ADDS R7, R7, #4 // Increment loop counter for the next address
LDR R2, [R0, R7] // Load the data at the loop counter offset
ADDS R1, R1, R2 // Accumulate sum at R1
CMP R7, #28 // Compare if we have reached the last address
BNE loop // Go to loop if not reached
LDR R0, =0x20000044 // Load the write back address to R0
STR R1, [R0] // Store Sum back into mem pointed by R0
71 72
Student Copy. For personal studies only. Do not distribute. 11/1/2023
Exercise Exercise
Q1. mem[0x20000024] = 15, mem[0x20000028] = 18, Q1. mem[0x20000024] = 15, mem[0x20000028] = 18, …
mem[0x20000040] = 11, Find the sum and save it in mem[0x20000044].
mem[0x2000002C] = 17, mem[0x20000030] = 16, MOVS R0, #0 //Accumulator
mem[0x20000034] = 14, mem[0x20000038] = 12, … MOVS R7, #28 // Loop counter
mem[0x2000003C] = 13, mem[0x20000040] = 11, LDR R2, =0x20000024 // R2 = 15
Find the sum and save it in mem[0x20000044]. loop:
LDR R1, [R2, R7] // Load the data at the loop counter offset
ADDS R0, R0, R1 // Accumulate sum at R0
SUBS R7, R7, #4 // Decrement loop counter for the next addr
CMP R7, #0
BNE loop // Go to loop if not reached
LDR R1, =0x20000044 // Load the write back address to R1
STR R0, [R1] // Store Sum back into mem pointed by 1
73 74
75 76
Student Copy. For personal studies only. Do not distribute. 11/1/2023
77 78
Exercise Exercise
mem[0x50000800] = 0xDEADABAB
mem[0x50000800] = 0xDEADABAB
Q1. Write 01 to bits[13:12] to 0x50000800 without changing the rest of the
Q1. Write 01 to bits[13:12] to 0x50000800 without bits.
changing the rest of the bits.
LDR R0,=0x50000800 //Load address to R0
LDR R1, [R0] //Load from mem[R0] to R1 = 0xXXXXXXXX
MOVS R7, #3 // Load R7 with 3 and R7 = 0x00000003
LSLS R7, R7, #12 // shift left 12 times R7 = 0x0003000 12th 13rd bits are 0
BICS R1, R1, R7// Invert Bit MASK
79 80
Student Copy. For personal studies only. Do not distribute. 11/1/2023
Exercise Exercise
mem[0x50000814] = 0xDEADABAB mem[0x50000814] = 0xDEADABAB
Q2. Write 1 to bit[6] to 0x50000814 without changing Q2. Write 1 to bit[6] to 0x50000814 without changing
the rest of the bits. the rest of the bits.
81 82
83 84