ecs642u embedded systems arm cpu and assembly code william marsh

19
ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Upload: ginger-jenkins

Post on 14-Jan-2016

232 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

ECS642U Embedded Systems

ARM CPU and Assembly Code

William Marsh

Page 2: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

2ARM University ProgramCopyright © ARM Ltd 2013

Acknowledgement

•Some slides from ARM University Program lab-in-a-box•Copyright acknowledged

Page 3: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Outline

• Aims• ARM processor and registers• ARM instructions• Issues for compilation

– Code size instruction length– Memory use: read or write?– Location

• Example compilation

Page 4: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Aims

• Learn to read ARM assembly code in overview

• Most programmers do not write assembly code but …

• Reading helpful for– Optimising speed (occasionally)– Debugging

• Illustrative not comprehensive– Look up opcodes as required

Page 5: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Core Concepts (Recap)

• Variable – location in memory• Code processes

– Addresses – get the right variable– Data – get the right value

• Control flow – if, loops, subroutines – Go to correct address– Branch– … or call / return

Page 6: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

ARM Architecture

Page 7: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Microcontroller vs. Microprocessor

• Both have a CPU• Microcontroller

has peripherals– Analog– Digital– Timing– Clock generators– Communications

• point to point• network

– Reliability and safety

Page 8: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Cortex-M0+ Core

Page 9: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

ARM Processor Core Registers

Page 10: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

ARM Processor Core Registers (32 bits each)

• R0-R12 - General purpose, for data processing

• SP - Stack pointer (R13)– Can refer to one of two SPs

• Main Stack Pointer (MSP)• Process Stack Pointer (PSP)

• LR - Link Register (R14)– Holds return address when called with Branch

& Link instruction (B&L)

• PC - program counter (R15)

Page 11: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

ARM Instructions

ARM Architecture

Page 12: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh
Page 13: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Instruction Set Summary

Instruction Type InstructionsMove MOVLoad/Store LDR, LDRB, LDRH, LDRSH, LDRSB, LDM,

STR, STRB, STRH, STMAdd, Subtract, Multiply

ADD, ADDS, ADCS, ADR, SUB, SUBS, SBCS, RSBS, MULS

Compare CMP, CMNLogical ANDS, EORS, ORRS, BICS, MVNS, TSTShift and Rotate LSLS, LSRS, ASRS, RORSStack PUSH, POPConditional branch IT, B, BL, B{cond}, BX, BLXExtend SXTH, SXTB, UXTH, UXTBReverse REV, REV16, REVSHProcessor State SVC, CPSID, CPSIE, SETEND, BKPTNo Operation NOPHint SEV, WFE, WFI, YIELD

Page 14: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Code Size and Thumb

• 32 bit processor– Longer addresses– Larger code

• Thumb and thumb-2– Most instructions 16 bits– High code density

Page 15: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Load/Store Register

• ARM is a load/store architecture, so must process data in registers, not memory

• LDR: load register from memory– LDR <Rt>, source address

• STR: store register to memory – STR <Rt>, destination address

Page 16: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Addressing Memory

• Offset Addressing– [<Rn>, <offset>] accesses address

<Rn>+<offset>– Base Register <Rn> can be register R0-R7, SP or

PC

• <offset> is added or subtracted from base register to create effective address– Can be an immediate constant– Can be another register, used as index <Rm>

• Auto-update– Write effective address back to base register– Pre-indexing– Post-indexing

Page 17: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Example

Page 18: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

void redOn(void){ // set red on without changing anything else // LED is actve low PTB->PCOR |= MASK(RED_LED_POS) ;}

Page 19: ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh

Summary

• Addresses in code• Loaded using PC offset addressing

• Ok to read assembly code