ECE 571 Advanced Microprocessor-Based Design Lecture 4

Similar documents
ECE 471 Embedded Systems Lecture 6

ECE 471 Embedded Systems Lecture 8

ECE 471 Embedded Systems Lecture 9

ECE 498 Linux Assembly Language Lecture 5

ECE 471 Embedded Systems Lecture 6

ECE 471 Embedded Systems Lecture 7

ECE 471 Embedded Systems Lecture 6

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 5

ECE 471 Embedded Systems Lecture 9

STEVEN R. BAGLEY ARM: PROCESSING DATA

ECE 498 Linux Assembly Language Lecture 1

Writing ARM Assembly. Steven R. Bagley

Lecture 4: Instruction Set Architecture

EC 413 Computer Organization

System calls and assembler

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

ARM Assembly Language. Programming

Writing ARM Assembly. Steven R. Bagley

Instruction Sets: Characteristics and Functions Addressing Modes

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

ECE 471 Embedded Systems Lecture 12

Instruction Set Principles and Examples. Appendix B

Computer Organization CS 206 T Lec# 2: Instruction Sets

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

Architecture. Digital Computer Design

Lecture 4: Instruction Set Design/Pipelining

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

Developing StrongARM/Linux shellcode

ECE 471 Embedded Systems Lecture 12

ECE 471 Embedded Systems Lecture 4

Systems Architecture I

CS4617 Computer Architecture

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

VE7104/INTRODUCTION TO EMBEDDED CONTROLLERS UNIT III ARM BASED MICROCONTROLLERS

Typical Processor Execution Cycle

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

EE4144: ARM Cortex-M Processor

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

EECS 373 Design of Microprocessor-Based Systems

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

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

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

Assembly Language Programming

Computer Organization & Assembly Language Programming (CSE 2312)

ECE 486/586. Computer Architecture. Lecture # 7

ARM Assembly Programming

NET3001. Advanced Assembly

Advanced Computer Architecture

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

ECE 571 Advanced Microprocessor-Based Design Lecture 3

ECE232: Hardware Organization and Design

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

Slides for Lecture 6

Machine Language, Assemblers and Linkers"

EC-801 Advanced Computer Architecture

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

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

ISA and RISCV. CASS 2018 Lavanya Ramapantulu

ECE 571 Advanced Microprocessor-Based Design Lecture 9

Lecture 3: Instruction Set Architecture

Virtual Machines and Dynamic Translation: Implementing ISAs in Software

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

ECE 471 Embedded Systems Lecture 4

ECE 598 Advanced Operating Systems Lecture 11

EECS 373 Design of Microprocessor-Based Systems

Chapter 2: Instructions How we talk to the computer

Instruction Set Design

Chapter 15. ARM Architecture, Programming and Development Tools

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

Practical Malware Analysis

ECE 598 Advanced Operating Systems Lecture 4

Basic Processor Design

ECE 571 Advanced Microprocessor-Based Design Lecture 7

Computer Organization & Assembly Language Programming (CSE 2312)

INSTITUTO SUPERIOR TÉCNICO. Architectures for Embedded Computing

EECS 373 Design of Microprocessor-Based Systems

Module 3 Instruction Set Architecture (ISA)

Sneha Rajguru & Prajwal Panchmahalkar

ECE232: Hardware Organization and Design

F28HS Hardware-Software Interface. Lecture 10: ARM Assembly Language 5

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

ISA: The Hardware Software Interface

Comparison InstruCtions

Chapter 15. ARM Architecture, Programming and Development Tools

Processor Status Register(PSR)

COSC 6385 Computer Architecture. Instruction Set Architectures

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

ARM Compiler and assembly on QEMU

ARM-7 ADDRESSING MODES INSTRUCTION SET

William Stallings Computer Organization and Architecture 8 th Edition. Chapter 11 Instruction Sets: Addressing Modes and Formats

Advanced Assembly, Branching, and Monitor Utilities

C-Style Strings. CS2253 Owen Kaser, UNBSJ

Chapter 2: Instructions:

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

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

ARM Assembly Language

INSTITUTO SUPERIOR TÉCNICO. Architectures for Embedded Computing

Transcription:

ECE 571 Advanced Microprocessor-Based Design Lecture 4 Vince Weaver http://www.eece.maine.edu/ vweaver vincent.weaver@maine.edu 24 January 2013

Low-Level ARM Linux Assembly 1

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. 2

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. 3

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). 4

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 5

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

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 $ 7

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. 8

.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" 9

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. 10

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 11

#==================== # 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 12

@ 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 13

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 14

ARM 32 bit encoding Instruction Sets THUMB 16 bit encoding THUMB-2 THUMB extended with 32-bit instructions THUMB-EE some extensions for running in JIT runtime AARCH64 64 bit. Only currently exists in simulated form 15

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 16

bytes 2560 2048 1536 1024 512 0 Code Density overall ia64 alpha RiSC parisc sparc mblaze mips m88k arm ppc 6502 s390 x86_64 vax sh3 m68k i386 thumb thumb-2 VLIW RISC CISC embedded 8/16-bit avr32 crisv32 z80 pdp-11 8086 17

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

bytes 256 192 128 64 0 Code Density lzss RiSC ia64 alpha mips parisc sparc mblaze 6502 m88k s390 arm ppc pdp-11 vax z80 m68k avr32 thumb thumb-2 sh3 x86_64 crisv32 i386 VLIW RISC CISC embedded 8/16-bit 8086 19

THUMB Most instructions length 16-bit (a few 32-bit) Some operands (sp, lr, pc) implicit Can t always update sp or pc anymore. Only r0-r7 accessible normally add, cmp, mov can access high regs No prefix/conditional execution Only two arguments to opcodes 20

(some exceptions for small constants: add r0,r1,#1) 8-bit constants rather than 12-bit Limited addressing modes 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) BX/BLX instruction to switch mode. 21

If target is a label, always switch mode If target is a register, low bit of 1 means THUMB, 0 means ARM Can use.thumb directive,.arm for 32-bit. 22

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) 23

Can use.thumb directive,.arm for 32-bit. 24

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. All 32-bit ARM instructions have 32-bit THUMB-2 equivalents except ones that use conditional execution. The it instruction was added to handle this. 25

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. 26

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

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

Other THUMB-2 Changes Instructions have wide and narrow encoding. Can force this (add.w vs add.n). rsc (reverse subtract with carry) removed Need to properly indicate s (set flags). Regular THUMB this is assumed. 29

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 30

Compiler gcc -S hello world.c On pandarboard creates Thumb-2 by default. Why? gcc -S -march=armv5t -mthumb hello world.c On my pandaboard, doesn t work. This is because gcc s 16-bit THUMB can t handle the hard floating point ABI that is installed on the system. gcc -S -marm hello world.c On my pandaboard, creates 32-bit ARM code 31