ECE 471 Embedded Systems Lecture 6

Similar documents
ECE 571 Advanced Microprocessor-Based Design Lecture 4

ECE 471 Embedded Systems Lecture 9

ECE 471 Embedded Systems Lecture 8

ECE 471 Embedded Systems Lecture 7

ECE 471 Embedded Systems Lecture 6

ECE 471 Embedded Systems Lecture 6

ECE 498 Linux Assembly Language Lecture 5

ECE 471 Embedded Systems Lecture 10

ECE 571 Advanced Microprocessor-Based Design Lecture 3

ECE 471 Embedded Systems Lecture 7

ECE 471 Embedded Systems Lecture 9

ECE 471 Embedded Systems Lecture 5

STEVEN R. BAGLEY ARM: PROCESSING DATA

ECE 471 Embedded Systems Lecture 4

Writing ARM Assembly. Steven R. Bagley

ECE 498 Linux Assembly Language Lecture 1

ECE 471 Embedded Systems Lecture 4

ECE 471 Embedded Systems Lecture 6

Comparison InstruCtions

Lecture 4: Instruction Set Architecture

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

ARM Shift Operations. Shift Type 00 - logical left 01 - logical right 10 - arithmetic right 11 - rotate right. Shift Amount 0-31 bits

Hi Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan

ECE 471 Embedded Systems Lecture 5

Chapter 15. ARM Architecture, Programming and Development Tools

Raspberry Pi / ARM Assembly. OrgArch / Fall 2018

ECE 471 Embedded Systems Lecture 12

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley

ARM Assembly Language. Programming

ARM Instruction Set. Computer Organization and Assembly Languages Yung-Yu Chuang. with slides by Peng-Sheng Chen

ARM Instruction Set. Introduction. Memory system. ARM programmer model. The ARM processor is easy to program at the

F28HS2 Hardware-Software Interfaces. Lecture 6: ARM Assembly Language 1

Page 1. Structure of von Nuemann machine. Instruction Set - the type of Instructions

System calls and assembler

Topic 10: Instruction Representation

Chapter 15. ARM Architecture, Programming and Development Tools

ECE 471 Embedded Systems Lecture 12

Instruction Set Principles and Examples. Appendix B

Systems Architecture I

EC 413 Computer Organization

VE7104/INTRODUCTION TO EMBEDDED CONTROLLERS UNIT III ARM BASED MICROCONTROLLERS

Instruction Sets: Characteristics and Functions Addressing Modes

Slides for Lecture 6

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

Architecture. Digital Computer Design

Lecture 4: Instruction Set Design/Pipelining

3 Assembly Programming (Part 2) Date: 07/09/2016 Name: ID:

ARM Assembly Programming

CS4617 Computer Architecture

Advanced Computer Architecture

Computer Organization CS 206 T Lec# 2: Instruction Sets

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

Processor Status Register(PSR)

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

ARM Instruction Set Architecture. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

55:132/22C:160, HPCA Spring 2011

ARM-7 ADDRESSING MODES INSTRUCTION SET

CS 310 Embedded Computer Systems CPUS. Seungryoul Maeng

Basic Processor Design

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

The PAW Architecture Reference Manual

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

ARM Cortex-A9 ARM v7-a. A programmer s perspective Part 2

ISA and RISCV. CASS 2018 Lavanya Ramapantulu

Virtual Machines and Dynamic Translation: Implementing ISAs in Software

sarm User Guide Note that a space must appear before the operation field because any word beginning in the first column is a label

Lecture 5: Instruction Set Architectures II. Take QUIZ 2 before 11:59pm today over Chapter 1 Quiz 1: 100% - 29; 80% - 25; 60% - 17; 40% - 3

ARM Cortex M3 Instruction Set Architecture. Gary J. Minden March 29, 2016

Writing ARM Assembly. Steven R. Bagley

EGC-442 Introduction to Computer Architecture Dr. Izadi Course Design Project (100 Points)

Programming the ARM. Computer Design 2002, Lecture 4. Robert Mullins

Branch Instructions. R type: Cond

Lecture 15 ARM Processor A RISC Architecture

NET3001. Advanced Assembly

Instructions: Language of the Computer

Chapter 2: Instructions:

A Bit of History. Program Mem Data Memory. CPU (Central Processing Unit) I/O (Input/Output) Von Neumann Architecture. CPU (Central Processing Unit)

Computer Organization MIPS ISA

EECS 373 Design of Microprocessor-Based Systems

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

ECE 598 Advanced Operating Systems Lecture 4

ECE 598 Advanced Operating Systems Lecture 11

Chapter 2A Instructions: Language of the Computer

Systems Architecture The Stack and Subroutines

Instructions: Language of the Computer

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

The ARM instruction set

ECE 486/586. Computer Architecture. Lecture # 7

EE4144: ARM Cortex-M Processor

Computer Architecture. MIPS Instruction Set Architecture

Typical Processor Execution Cycle

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

Computer Architecture. The Language of the Machine

Computer Organization & Assembly Language Programming (CSE 2312)

Instruction Set Architecture (ISA)

ECE232: Hardware Organization and Design

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

ARM Assembly Language

button.c The little button that wouldn t

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Transcription:

ECE 471 Embedded Systems Lecture 6 Vince Weaver http://www.eece.maine.edu/ vweaver vincent.weaver@maine.edu 18 September 2014

Announcements I have a cold and my voice is gone! HW#3 will be posted tomorrow 1

ARM Instruction Set Encodings ARM 32 bit encoding THUMB 16 bit encoding THUMB-2 THUMB extended with 32-bit instructions STM32L only has THUMB2 Raspberry PI does not have THUMB2 THUMB-EE some extensions for running in JIT runtime AARCH64 64 bit. Relatively new. 2

Recall the ARM32 encoding ADD{S}<c> <Rd>,<Rn>,<Rm>{,<shift>} 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 cond 0 0 0 0 1 0 0 Opcode S Rn 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Rd Shift imm5 Shift typ Sh Reg Rm 3

THUMB Most instructions length 16-bit (a few 32-bit) Only r0-r7 accessible normally add, cmp, mov can access high regs Some operands (sp, lr, pc) implicit Can t always update sp or pc anymore. No prefix/conditional execution Only two arguments to opcodes (some exceptions for small constants: add r0,r1,#1) 8-bit constants rather than 12-bit 4

Limited addressing modes: [rn,rm], [rn,#imm], [pc sp,#imm] No shift parameter ALU instructions Makes assumptions about S setting flags (gas doesn t let you superfluously set it, causing problems if you naively move code to THUMB-2) new push/pop instructions (subset of ldm/stm), neg (to negate), asr,lsl,lsr,ror, bic (logic bit clear) 5

THUMB/ARM interworking See print string armthumb.s BX/BLX instruction to switch mode. If target is a label, always switchmode If target is a register, low bit of 1 means THUMB, 0 means ARM Can also switch modes with ldrm, ldm, or pop with PC as a destination (on armv7 can enter with ALU op with PC destination) Can use.thumb directive,.arm for 32-bit. 6

THUMB-2 Extension of THUMB to have both 16-bit and 32-bit instructions 32-bit instructions not standard 32-bit ARM instructions. It s a new encoding that allows an instruction to be 32- bit if needed. Most 32-bit ARM instructions have 32-bit THUMB-2 equivalents except ones that use conditional execution. The it instruction was added to handle this. rsc (reverse subtract with carry) removed 7

Shifts in ALU instructions are by constant, cannot shift by register like in arm32 THUMB-2 code can assemble to either ARM-32 or THUMB2 The assembly language is compatible. Common code can be written and output changed at time of assembly. Instructions have wide and narrow encoding. Can force this (add.w vs add.n). Need to properly indicate s (set flags). On regular THUMB this is assumed. 8

THUMB-2 Coding See test thumb2.s Use.syntax unified at beginning of code Use.arm or.thumb to specify mode 9

New THUMB-2 Instructions BFI bit field insert RBIT reverse bits movw/movt 16 bit immediate loads TB table branch IT (if/then) cbz compare and branch if zero; only jumps forward 10

Thumb-2 12-bit immediates top 4 bits 0000 -- 00000000 00000000 00000000 abcdefgh 0001 -- 00000000 abcdefgh 00000000 abcdefgh 0010 -- abcdefgh 00000000 abcdefgh 00000000 0011 -- abcdefgh abcdefgh abcdefgh abcdefgh 0100 -- 1bcdedfh 00000000 00000000 00000000... 1111 -- 00000000 00000000 00000001 bcdefgh0 11

Compiler RASPBERRY PI DOES NOT SUPPORT THUMB2 gcc -S hello world.c By default is arm32 gcc -S -march=armv5t -mthumb hello world.c Creates THUMB (won t work on Rapberry Pi due to HARDFP arch) -mthumb -march=armv7-a Creates THUMB2 12

IT (If/Then) Instruction Allows limited conditional execution in THUMB-2 mode. The directive is optional (and ignored in ARM32) the assembler can (in-theory) auto-generate the IT instruction Limit of 4 instructions 13

it cc addcc r1,r2 itete cc addcc r1,r2 addcs r1,r2 addcc r1,r2 addcs r1,r2 Example Code 14

ll Example Code ittt cs @ If CS Then Next plus CS for next 3 discrete_char: ldrbcs r4,[r3] @ load a byte addcs r3,#1 @ increment pointer movcs r6,#1 @ we set r6 to one so byte bcs.n store_byte @ and store it offset_length: 15

AARCH64 32-bit fixed instruction encoding 31 64-bit GP registers (x0-x30), zero register (x30) PC is not a GP register only branches conditional no load/store multiple No thumb 16

Code Density Overview from my ll ICCD 09 paper Show code density for variety of architectures, recently added Thumb-2 support. Shows overall size, though not a fair comparison due to operating system differences on non-linux machines 17

bytes 3072 2560 2048 1536 1024 512 0 Code Density overall ia64 alpha RiSC pa-risc sparc microblaze mips m88k arm.eabi PowerPC 6502 arm64 s390 x86_64 x86_x32 sh3 m68k i386 vax THUMB Thumb-2 avr32 crisv32 z80 pdp-11 VLIW RISC CISC embedded 8/16-bit 8086 18

lzss compression Printing routine uses lzss compression Might be more representative of potential code density 19

bytes 384 320 256 192 128 64 0 Code Density lzss RiSC ia64 alpha pa-risc mips sparc microblaze 6502 m88k s390 arm.eabi PowerPC pdp-11 z80 arm64 m68k avr32 sh3 THUMB Thumb-2 vax x86_64 x86_x32 crisv32 VLIW RISC CISC embedded 8/16-bit i386 8086 20

Low-Level ARM Linux Assembly 21

Kernel Programming ABIs OABI old original ABI (arm). Being phased out. slightly different syscall mechanism, different alignment restrictions EABI new embedded ABI (armel) hard float EABI compiled with ARMv7 and VFP (vector floating point) support (armhf). Raspberry Pi (raspbian) is compiled for ARMv6 armhf. 22

System call number in r7 Arguments in r0 - r6 Call swi 0x0 System Calls (EABI) System call numbers can be found in /usr/include/arm-linux-gnueabihf/asm/unistd.h They are similar to the 32-bit x86 ones. 23

System Calls (OABI) The previous implementation had the same system call numbers, but instead of r7 the number was the argument to swi. This was very slow, as there is no way to determine that value without having the kernel backtrace the callstack and disassemble the instruction. 24

Manpage The easiest place to get system call documentation. man open 2 Finds the documentation for open. The 2 means look for system call documentation (which is type 2). 25

A first ARM assembly program: hello exit.equ SYSCALL_EXIT, 1 _start :.globl _start exit : #================================ # Exit #================================ mov r0,#5 mov r7,# SYSCALL_EXIT @ put exit syscall number (1) in eax swi 0 x0 @ and exit 26

hello exit example Assembling/Linking using make, running, and checking the output. lecture6$ make hello_exit_arm as -o hello_exit_arm.o hello_exit_arm.s ld -o hello_exit_arm hello_exit_arm.o lecture6$./hello_exit_arm lecture6$ echo $? 5 27

Assembly @ is the comment character. # can be used on line by itself but will confuse assembler if on line with code. Can also use /* */ Order is source, destination Constant value indicated by # or $ 28

Let s look at our executable ls -la./hello exit arm Check the size readelf -a./hello exit arm Look at the ELF executable layout objdump --disassemble-all./hello exit arm See the machine code we generated strace./hello exit arm Trace the system calls as they happen. 29

.equ SYSCALL_EXIT, 1.equ SYSCALL_WRITE, 4.equ STDOUT, 1 hello world example _start : exit :.globl _start mov r0,# STDOUT /* stdout */ ldr r1,= hello mov r2,#13 @ length mov r7,# SYSCALL_WRITE swi 0x0 # Exit mov r0,#5 mov r7,# SYSCALL_EXIT @ put exit syscall number in r7 swi 0 x0 @ and exit.data hello :.ascii " Hello World!\n" 30

New things to note in hello world The fixed-length 32-bit ARM cannot hold a full 32-bit immediate Therefore a 32-bit address cannot be loaded in a single instruction In this case the = is used to request the address be stored in a literal pool which can be reached by PC-offset, with an extra layer of indirection. 31

Put string example.equ SYSCALL_EXIT, 1.equ SYSCALL_WRITE, 4.equ STDOUT, 1 _start : exit :.globl _start ldr r1,= hello bl print_string @ Print Hello World ldr r1,= mystery bl print_string @ ldr r1,= goodbye bl print_string /* Print Goodbye */ #================================ # Exit #================================ mov r0,#5 mov r7,# SYSCALL_EXIT @ put exit syscall number (1) in eax swi 0 x0 @ and exit 32

#==================== # print string #==================== # Null - terminated string to print pointed to by r1 # r1 is trashed by this routine print_string : push {r0,r2,r7, r10 } @ Save r0,r2,r7, r10 on stack mov r2,#0 @ Clear Count count_loop : add r2, r2,#1 @ increment count ldrb r10,[ r1, r2] @ load byte from address r1 + r2 cmp r10,#0 @ Compare against 0 bne count_loop @ if not 0, loop mov r0,# STDOUT @ Print to stdout mov r7,# SYSCALL_WRITE @ Load syscall number swi 0 x0 @ System call pop {r0,r2,r7, r10 } @ pop r0,r2,r7, r10 from stack mov pc, lr @ Return to address stored in 33

@ Link register.data hello :.string " Hello World!\ n" @ includes null at end mystery :.byte 63,0 x3f,63,10,0 @ mystery string goodbye :.string " Goodbye!\ n" @ includes null at end 34

Clarification of Assembler Syntax @ is the comment character. # can be used on line by itself but will confuse assembler if on line with code. Can also use /* */ Constant value indicated by # or $ Optionally put % in front of register name 35