ARM Assembly Language and Machine Code Goal: Blink an LED - - PowerPoint PPT Presentation

arm assembly language and machine code
SMART_READER_LITE
LIVE PREVIEW

ARM Assembly Language and Machine Code Goal: Blink an LED - - PowerPoint PPT Presentation

ARM Assembly Language and Machine Code Goal: Blink an LED Summary You need to understand how processors represent and execute instructions Instruction set architecture often easier to understand by looking at the bits. Encoding instructions


slide-1
SLIDE 1

ARM Assembly Language and Machine Code

Goal: Blink an LED

slide-2
SLIDE 2

Summary

You need to understand how processors represent and execute instructions Instruction set architecture often easier to understand by looking at the bits. Encoding instructions in 32-bits requires trade-offs, careful design Only write assembly when it is needed. Reading assembly more important than writing assembly Allows you to see what the compiler and processor are actually doing Normally write code in C (Starting next lecture)

slide-3
SLIDE 3

Registers ALU DATA ADDR INST

+

Memory ADDR

r2 r1 r0 add r0, r1, r2 r0 = r1 + r2

ALU only operates on registers Registers are also 32-bit words

slide-4
SLIDE 4

Registers ALU DATA ADDR INST

+

Memory ADDR

1 r1 r0 add r0, r1, #1

Immediate Value (#1) stored in INST

slide-5
SLIDE 5

Load and Store Instructions

slide-6
SLIDE 6

ldr r0, [r1] r1

ADDR = r1 DATA = Memory[ADDR]

Registers ALU DATA ADDR INST

+

Memory ADDR

Load from Memory to Register (LDR) Step 1

slide-7
SLIDE 7

r0

r0 = DATA

Registers ALU DATA ADDR INST

+

Memory ADDR

Step 2 Load from Memory to Register (LDR)

slide-8
SLIDE 8

r0 str r0, [r1]

Registers ALU DATA ADDR INST

+

Memory ADDR

Store Register in Memory (STR) DATA = r0 Step 1

slide-9
SLIDE 9

r1

Registers ALU DATA ADDR INST

+

Memory ADDR

Store Register in Memory (STR) ADDR = r1 Memory[ADDR] = DATA Step 2

str r0, [r1]

slide-10
SLIDE 10
slide-11
SLIDE 11

Turning on an LED

slide-12
SLIDE 12

General-Purpose Input/Output (GPIO) Pins 54 GPIO Pins

slide-13
SLIDE 13

1 -> 3.3V 0 -> 0.0V (GND) Connect LED to GPIO 20 3.3V 1k GND

slide-14
SLIDE 14
slide-15
SLIDE 15

GPIO Pins are Peripherals Peripherals are Controlled by Special Memory Locations "Peripheral Registers"

slide-16
SLIDE 16

02000000016 10000000016

Memory Map

4 GB

Ref: BCM2835-ARM-Peripherals.pdf

Peripheral registers are mapped into address space Memory-Mapped IO (MMIO) MMIO space is above physical memory 512 MB

slide-17
SLIDE 17

General-Purpose IO Function 3 bits required to select function

Bit pattern Pin Function 000 The pin in an input 001 The pin is an output 100 The pin does alternate function 0 101 The pin does alternate function 1 110 The pin does alternate function 2 111 The pin does alternate function 3 011 The pin does alternate function 4 010 The pin does alternate function 5

GPIO Pins can be configured to be INPUT, OUTPUT, or ALT0-5

slide-18
SLIDE 18

2 1 5 4 3 8 7 6 9 10 11 14 13 12 15 16 17 20 19 18 21 22 23 26 25 24 27 28 29 30 31 GPIO 0 GPIO 1 GPIO 3 GPIO 4 GPIO 5 GPIO 6 GPIO 7 GPIO 8 GPIO 9 GPIO 2

GPIO Function Select Register 8 functions requires 3 bits to specify 10 pins times 3 bits = 30 bits 32-bit register (2 wasted bits) "Function" is INPUT, OUTPUT (or ALT0-5) 54 GPIOs pins requires 6 registers

slide-19
SLIDE 19

Watch out for … Manual says: 0x7E200000 Replace 7E with 20: 0x20200000

Ref: BCM2835-ARM-Peripherals.pdf

GPIO Function Select Registers Addresses

slide-20
SLIDE 20
slide-21
SLIDE 21

2 1 5 4 3 8 7 6 9 10 11 14 13 12 15 16 17 20 19 18 21 22 23 26 25 24 27 28 29 30 31 34 33 32 37 36 35 40 39 38 41 42 43 46 45 44 47 48 49 52 51 50 53

20 20 00 1C : GPIO SET0 Register 20 20 00 20 : GPIO SET1 Register

GPIO Function SET Register Notes

  • 1. 1 bit per GPIO pin
  • 2. 54 pins requires 2 registers
slide-22
SLIDE 22

// Set GPIO20 to be an output // FSEL2 = 0x20200008 mov r0, #0x20 // r0 = #0x00000020 lsl r1, r0, #24 // r1 = #0x20000000 lsl r2, r0, #16 // r2 = #0x00200000

  • rr r1, r1, r2 // r1 = #0x20200000
  • rr r0, r1, #0x08 // r0 = #0x20200008

mov r1, #1 // 1 indicates OUTPUT str r1, [r0] // store 1 to 0x20200008

Note this also makes GPIO 21-29 into inputs

slide-23
SLIDE 23

Back to the ARM Instruction Set Architecture

slide-24
SLIDE 24

3 Types of Instructions

  • 1. Data processing instructions
  • 2. Loads from and stores to memory
  • 3. Conditional branches to new program

locations

slide-25
SLIDE 25

Data Processing Instructions and Machine Code

slide-26
SLIDE 26

From armisa.pdf

slide-27
SLIDE 27

# data processing instruction # # ra = rb op rc

  • p rb ra rc

1110 00 i oooo s bbbb aaaa cccc cccc cccc

Data processing instruction Always execute the instruction Immediate mode instruction Set condition codes

slide-28
SLIDE 28

Assembly Code Operations AND 0000 ra=rb&rc EOR (XOR) 0001 ra=rb^rc SUB 0010 ra=rb-rc RSB 0011 ra=rc-rb ADD 0100 ra=rb+rc ADC 0101 ra=rb+rc+CARRY SBC 0110 ra=rb-rc+(1-CARRY) RSC 0111 ra=rc-rb+(1-CARRY) TST 1000 rb&rc (ra not set) TEQ 1001 rb^rc (ra not set) CMP 1010 rb-rc (ra not set) CMN 1011 rb+rc (ra not set) ORR (OR) 1100 ra=rb|rc MOV 1101 ra=rc BIC 1110 ra=rb&~rc MVN 1111 ra=~rc

slide-29
SLIDE 29

# data processing instruction # ra = rb op rc #

  • p rb ra rc

1110 00 i oooo s bbbb aaaa cccc cccc cccc # i=0, s=0 add r1 r0 r2 1110 00 0 0100 0 0001 0000 0000 0000 0010

slide-30
SLIDE 30

# data processing instruction # ra = rb op rc #

  • p rb ra rc

1110 00 i oooo s bbbb aaaa cccc cccc cccc # i=0, s=0 add r1 r0 r2 1110 00 0 0100 0 0001 0000 0000 0000 0010 1110 0000 1000 0001 0000 0000 0000 0010 E 0 8 1 0 0 0 2

slide-31
SLIDE 31

E0 81 00 02 02 00 81 E0

ADDR ADDR+1 ADDR+2 ADDR+3

little-endian (LSB first) most-significant-byte (MSB) least-significant-byte (LSB) ARM uses little-endian

slide-32
SLIDE 32

E0 81 00 02 E0 81 00 02

big-endian (MSB first) most-significant-byte (MSB)

ADDR ADDR+1 ADDR+2 ADDR+3

least-significant-byte (LSB)

slide-33
SLIDE 33

# data processing instruction # ra = rb op #imm # #imm = uuuu uuuu add r1 r0 imm 1110 00 1 0100 0 0001 0000 0000 uuuu uuuu add r0, r1, #1 add r1 r0 #1 1110 00 1 0100 0 0001 0000 0000 0000 0001

slide-34
SLIDE 34

# data processing instruction # ra = rb op #imm # #imm = uuuu uuuu add r1 r0 imm 1110 00 1 0100 0 0001 0000 0000 uuuu uuuu add r0, r1, #1 add r1 r0 #1 1110 00 1 0100 0 0001 0000 0000 0000 0001 1110 0010 1000 0001 0000 0000 0000 0001 E 2 8 1 0 0 0 1

slide-35
SLIDE 35

Registers ALU

Shift

DATA ADDR INST

+

Memory ADDR

slide-36
SLIDE 36

Rotate Right (ROR) - Rotation amount = 2x

slide-37
SLIDE 37

# data processing instruction # ra = rb op imm # imm = (uuuu uuuu) ROR (2*rrrr)

  • p rb ra ror imm

1110 00 1 oooo 0 bbbb aaaa rrrr uuuu uuuu ROR means Rotate Right (imm>>>rotate)

slide-38
SLIDE 38

# data processing instruction # ra = rb op imm # imm = (uuuu uuuu) ROR (2*rrrr)

  • p rb ra ror uuu

1110 00 1 oooo 0 bbbb aaaa rrrr uuuu uuuu add r0, r1, #0x10000 add r1 r0 0x01>>>2*8 1110 00 1 0100 0 0001 0000 1000 0000 0001 0x01>>>16 0000 0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 0001 0000 0000 0000 0000

slide-39
SLIDE 39

# data processing instruction # ra = rb op imm # imm = (uuuu uuuu) ROR (2*rrrr)

  • p rb ra ror imm

1110 00 1 oooo 0 bbbb aaaa rrrr uuuu uuuu add r0, r1, #0x10000 add r1 r0 0x01>>>2*8 1110 00 1 0100 0 0001 0000 1000 0000 0001 1110 0010 1000 0001 0000 1000 0000 0001 E 2 8 1 0 8 0 1

slide-40
SLIDE 40

/// SET0 = 0x2020001c mov r0, #0x20 // r0 = 0x00000020 lsl r1, r0, #24 // r1 = 0x20000000 lsl r2, r0, #16 // r2 = 0x00200000

  • rr r0, r1, r2 // r0 = 0x20200000
  • rr r0, r0, #0x1c // r0 = 0x2020001c

// SET0 = 0x2020001c mov r0, #0x20000000 // 0x20>>>8

  • rr r0, #0x00200000 // 0x20>>>16
  • rr r0, #0x0000001c // 0x1c>>>0
slide-41
SLIDE 41

/// SET0 = 0x2020001c mov r0, #0x20 // r0 = 0x00000020 lsl r1, r0, #24 // r1 = 0x20000000 lsl r2, r0, #16 // r2 = 0x00200000

  • rr r0, r1, r2 // r0 = 0x20200000
  • rr r0, r0, #0x1c // r0 = 0x2020001c

// SET0 = 0x2020001c mov r0, #0x20000000 // 0x20>>>8

  • rr r0, #0x00200000 // 0x20>>>16
  • rr r0, #0x0000001c // 0x1c>>>0

Using the barrel shifter lets us make the code 40% shorter (and 40% faster)

slide-42
SLIDE 42

ldr r0, [r1, #4] r1

ADDR = r1+4 DATA = Memory[ADDR] Load from Memory to Register (LDR)

4

Registers ALU DATA ADDR INST

+

Memory

slide-43
SLIDE 43

// configure GPIO 20 for output ldr r0, [pc + 20] mov r1, #1 str r1, [r0] // set bit 20 ldr r0, [pc + 12] mov r1, #0x00100000 str r1, [r0] loop: b loop .word 0x20200008 .word 0x2020001C

slide-44
SLIDE 44

// configure GPIO 20 for output ldr r0, =0x20200008 mov r1, #1 str r1, [r0] // set bit 20 ldr r0, =0x2020001C mov r1, #0x00100000 str r1, [r0] loop: b loop

slide-45
SLIDE 45

Fetch 3 steps to run an instruction Decode Execute

slide-46
SLIDE 46

Fetch 3 instructions takes 9 steps Decode Execute Fetch Decode Decode Execute

slide-47
SLIDE 47

Fetch Decode Execute Fetch Decode Execute Fetch Decode Execute

To speed things up, steps are overlapped ("pipelined")

slide-48
SLIDE 48

Fetch

To speed things up, steps are overlapped ("pipelined")

Decode Execute Fetch Decode Execute Fetch Decode Execute PC value in the executing instruction is equal to the pc value of the instruction being fetched - which is 2 instructions ahead (PC+8)

slide-49
SLIDE 49

Blink

slide-50
SLIDE 50

mov r1, #(1<<20) // Turn on LED connected to GPIO20 ldr r0, SET0 str r1, [r0] // Turn off LED connected to GPIO20 ldr r0, CLR0 str r1, [r0]

2 1 5 4 3 8 7 6 9 10 11 14 13 12 15 16 17 20 19 18 21 22 23 26 25 24 27 28 29 30 31 34 33 32 37 36 35 40 39 38 41 42 43 46 45 44 47 48 49 52 51 50 53

slide-51
SLIDE 51

// Configure GPIO 20 for OUTPUT loop: // Turn on LED // Turn off LED b loop

slide-52
SLIDE 52

Loops and Condition Codes

slide-53
SLIDE 53

// define constant .equ DELAY, 0x3f0000 mov r2, #DELAY loop: subs r2, r2, #1 // s set cond code bne loop // branch if r2 != 0

slide-54
SLIDE 54

Orthogonal Instructions

Any operation Register vs. immediate operands All registers the same** Predicated/conditional execution Set or not set condition code Orthogonality leads to composability

slide-55
SLIDE 55

Further Reading

If you want to learn more about high-level computer

  • rganization and instructions,

Chapter 2 of Computer Organization and Design: The Hardware/Software Interface (Patterson and Hennessy) is an excellent place to start. Or take EE180 in Spring!

slide-56
SLIDE 56

The Fun Begins …

Lab1

■ Install tool chain before lab ■ Read lab1 instructions (now online) ■ Assemble Raspberry Pi Kit ■ Bring USB-C to USB-A adapter (if you need it)

Assignment 1

■ Larson scanner ■ YEAH office hours Thu 3-4pm in B21

slide-57
SLIDE 57

Definitive References

BCM2835 peripherals document + errata Raspberry Pi schematic ARM11/ARMv6 reference manual see Resources on cs107e.github.io

slide-58
SLIDE 58

Extra Material on Branches

slide-59
SLIDE 59

Branch Instructions

slide-60
SLIDE 60

Condition Codes

Z - Result is 0 N - Result is <0 C - Carry generated V - Arithmetic overflow Carry and overflow will be covered later

slide-61
SLIDE 61

Condition Codes

Z - the result is 0 N - the result is negative C - a carry was generated V - an arithmetic overflow occurred

slide-62
SLIDE 62

# branch cond addr cccc 101L oooo oooo oooo oooo oooo oooo b = bal = branch always cond addr 1110 101L oooo oooo oooo oooo oooo oooo bne cond addr 0001 101L oooo oooo oooo oooo oooo oooo