EE251: Tuesday September 5

Similar documents
Chapters 3. ARM Assembly. Embedded Systems with ARM Cortext-M. Updated: Wednesday, February 7, 2018

ARM Cortex-M4 Programming Model Logical and Shift Instructions

ARM Cortex-M4 Programming Model

Bitwise Instructions

ECE 471 Embedded Systems Lecture 5

ARM Assembly Language. Programming

Assembly Language Programming

Processor Status Register(PSR)

ECE 498 Linux Assembly Language Lecture 5

Assembly Language. CS2253 Owen Kaser, UNBSJ

University of California, San Diego CSE 30 Computer Organization and Systems Programming Winter 2014 Midterm Dr. Diba Mirza

Chapter 2 Instructions Sets. Hsung-Pin Chang Department of Computer Science National ChungHsing University

Systems Architecture The ARM Processor

Lecture 4 (part 2): Data Transfer Instructions

ARM Assembly Programming

LAB 1 Using Visual Emulator. Download the emulator Answer to questions (Q)

ECE251: Tuesday September 18

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

ARM Cortex-M4 Programming Model Memory Addressing Instructions

Binary Logic (review)

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

ECE251: Tuesday Aug. 29

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

MNEMONIC OPERATION ADDRESS / OPERAND MODES FLAGS SET WITH S suffix ADC

Advanced Assembly, Branching, and Monitor Utilities

ARM Cortex-M4 Architecture and Instruction Set 2: General Data Processing Instructions

Exam 1 Fun Times. EE319K Fall 2012 Exam 1A Modified Page 1. Date: October 5, Printed Name:

Outline. ARM Introduction & Instruction Set Architecture. ARM History. ARM s visible registers

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

(2) Part a) Registers (e.g., R0, R1, themselves). other Registers do not exists at any address in the memory map

Overview COMP Microprocessors and Embedded Systems. Lecture 11: Memory Access - I. August, 2003

Overview COMP 3221 Bitwise Logical Operations

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

A block of memory (FlashROM) starts at address 0x and it is 256 KB long. What is the last address in the block?

Writing ARM Assembly. Steven R. Bagley

ECE251: Thursday September 13

ECE251: Tuesday September 12

ECE251: Intro to Microprocessors Name: Solutions Mid Term Exam October 4, 2018

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

Exam 1. Date: Oct 4, 2018

EE319K Exam 1 Summer 2014 Page 1. Exam 1. Date: July 9, Printed Name:

ARM Assembly Language

EE319K Fall 2013 Exam 1B Modified Page 1. Exam 1. Date: October 3, 2013

STEVEN R. BAGLEY ARM: PROCESSING DATA

Chapter 15. ARM Architecture, Programming and Development Tools

ECE251: Thursday September 27

ECE 571 Advanced Microprocessor-Based Design Lecture 3

Assembler: Basics. Alberto Bosio October 20, Univeristé de Montpellier

Lab5 2-Nov-18, due 16-Nov-18 (2 weeks duration) Lab6 16-Nov-19, due 30-Nov-18 (2 weeks duration)

The ARM Instruction Set

Chapter 15. ARM Architecture, Programming and Development Tools

ARM Cortex-M4 Architecture and Instruction Set 3: Branching; Data definition and memory access instructions

EE319K Spring 2015 Exam 1 Page 1. Exam 1. Date: Feb 26, 2015

EE319K Spring 2016 Exam 1 Solution Page 1. Exam 1. Date: Feb 25, UT EID: Solution Professor (circle): Janapa Reddi, Tiwari, Valvano, Yerraballi

Architecture. Digital Computer Design

Embedded assembly is more useful. Embedded assembly places an assembly function inside a C program and can be used with the ARM Cortex M0 processor.

ARM Assembler Workbook. CS160 Computer Organization Version 1.1 October 27 th, 2002 Revised Fall 2005

ECE251: Tuesday September 11

The PAW Architecture Reference Manual

CS 310 Embedded Computer Systems CPUS. Seungryoul Maeng

ARM Processors ARM ISA. ARM 1 in 1985 By 2001, more than 1 billion ARM processors shipped Widely used in many successful 32-bit embedded systems

ECE 471 Embedded Systems Lecture 6

18-349: Introduction to Embedded Real- Time Systems Lecture 3: ARM ASM

ARM Instruction Set 1

Raspberry Pi / ARM Assembly. OrgArch / Fall 2018

Subroutines and the Stack

Introduction to the ARM Processor Using Intel FPGA Toolchain. 1 Introduction. For Quartus Prime 16.1

Assembly Language Programming

Introduction to Embedded Systems EE319K (Gerstlauer), Spring 2013

Comparison InstruCtions

ARM PROGRAMMING. When use assembly

Chapters 5. Load & Store. Embedded Systems with ARM Cortex-M. Updated: Thursday, March 1, 2018

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

The ARM Cortex-M0 Processor Architecture Part-2

Chapter 3: ARM Instruction Set [Architecture Version: ARMv4T]

Assembly language Simple, regular instructions building blocks of C, Java & other languages Typically one-to-one mapping to machine language

Mark II Aiken Relay Calculator

We will begin our study of computer architecture From this perspective. Machine Language Control Unit

CMPSCI 201 Fall 2004 Midterm #1 Answers

Introduction to C. Write a main() function that swaps the contents of two integer variables x and y.

ARM-7 ADDRESSING MODES INSTRUCTION SET

The ARM processor. Morgan Kaufman ed Overheads for Computers as Components

Computer Organization & Systems Exam I Example Questions

ARM7TDMI Instruction Set Reference

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

Computer Architecture Prof. Smruthi Ranjan Sarangi Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Exam 1. Date: February 23, 2018

VE7104/INTRODUCTION TO EMBEDDED CONTROLLERS UNIT III ARM BASED MICROCONTROLLERS

Assembly language Simple, regular instructions building blocks of C, Java & other languages Typically one-to-one mapping to machine language

AVR ISA & AVR Programming (I)

Problem Sheet 1: ANSWERS (NOT ) (NOT ) 3. 37, ,

AND SOLUTION FIRST INTERNAL TEST

Introduction to the ARM Processor Using Altera Toolchain. 1 Introduction. For Quartus II 14.0

Cortex M3 Programming

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

Developing StrongARM/Linux shellcode

ARM Cortex-M4 Programming Model Flow Control Instructions

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

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

Transcription:

EE251: Tuesday September 5 Shift/Rotate Instructions Bitwise logic and Saturating Instructions A Few Math Programming Examples ARM Assembly Language and Assembler Assembly Process Assembly Structure Assembler Directives Reading: Chapters 3 and 7 Lab #2 finishes this week. Report due next week. Lab #3 starts next week. Online now. Note Lab #3 prework! Homework #1 due Thursday 4:30 pm in box in BC Infill. Lecture #5 1

But First, look at LDR Pseudo Instruction 18: LDR R3,=0x87654321 0x00000008 4B07 LDR r3,[pc,#28] 19: MOV R0, #0... 29: done B done 0x00000024 E7FE B 0x00000024 0x00000026 0000 DCW 0x0000 0x00000028 4321 DCW 0x4321 0x0000002A 8765 DCW 0x8765 Lecture #5 2

Shift and Rotate Instructions Multiple bit positions with single instruction Logical Shift Left (LSL) Arithmetic Shift Right (ASR) Logical Shift Right (LSR) Rotate Right (ROR) Rotate Right Extended (RRX) Lecture #5 3

Shift - Rotate Instruction Examples We are assuming registers are 8 bits wide for easier examples. They are actually 32 bits wide. Examples: LSL r1, r0, #3 Assume [r0] = 0x0F = 0b00001111 = 15 r1 = 0b01111000 = 0x78 = 120 = 15*2 3 LSR r3, r4, #2 Assume [r4]=0xf0=0b11110000=240 r3 = 0b00111100 = 0x3C = 60 = 240 2 2 ASR r3, r4, #2 Assume [r4]=0xf0=0b11110000= -16 r3 = 0b11111100 = 0xFC = -4 = -16 2 2 Signed numbers cause different results for LSR vs. ASR! Other examples? Lecture #5 4

Using Barrel Shifter in Arithmetic The second operand of ALU has a special hardware called Barrel shifter which allows more powerful instruction when shifting is combined with math operations Example: ADD r1, r0, r0, LSL #3 r1 = r0 + r0 << 3 = r0 + 8 r0 = 9 r0 Note: << means shift left and >> means shift right Lecture #5 5

Barrel Shift Examples Examples: ADD r1, r0, r0, LSL #2 r1 = r0 + r0 << 2 = r0 + 4 r0 ADD r1, r0, r0, LSR #3 r1 = r0 + r0 >> 3 = r0 + r0/8 (unsigned) ADD r1, r0, r0, ASR #3 r1 = r0 + r0 >> 3 = r0 + r0/8 (signed) Use Barrel shifter for better performance: compute 9*r0 ADD r1, r0, r0, LSL #3 vs. MOV r2, #9 r2 = 9 MUL r1, r0, r2 r1 = r0*9 Lecture #5 6

Bitwise Logic AND {Rd,} Rn, Op2 Bitwise logic AND. Rd Rn & Op2 ORR {Rd,} Rn, Op2 Bitwise logic OR. Rd Rn Op2 EOR {Rd,} Rn, Op2 Bitwise logic exclusive OR. Rd Rn EOR Op2 ORN {Rd,} Rn, Op2 Bitwise logic NOT OR. Rd Rn (NOT Op2) BIC {Rd,} Rn, Op2 Bit clear. Rd Rn & NOT Op2 BFC Rd, #lsb, #width Bit field clear. Rd[(width+lsb 1):lsb] 0 BFI Rd, Rn, #lsb, #width Bit field insert. Rd[(width+lsb 1):lsb] Rn[(width-1):0] MVN Rd, Op2 Move NOT, logically negate all bits. Rd 0xFFFFFFFF EOR Op2 AND, ORR, EOR behavior is straightforward. See examples Section 4.6 of text. Other examples (showing just 8 bits of the 32 bit registers): ORN R2, R1, R0 R1=10101010 and R0=11001100, not(r0)=00110011 and 10111011 R2 BIC R2 R1 R2=10101010 and R1=11001100, not(r1)=00110011 and 00100010 R2 Lecture #5 7

Example: BFC and BFI Bit Field Clear (BFC) and Bit Field Insert (BFI). Syntax BFC Rd, #lsb, #width BFI Rd, Rn, #lsb, #width Examples BFC R4, #8, #12 Clear bit 8 to bit 19 (12 bits) of R4 to 0 BFI R9, R2, #8, #12 Replace bit 8 to bit 19 (12 bits) of R9 with bit 0 to bit 11 from R2. Lecture #5 8

Examples: Set or Clear bit Set a bit: ORR R0, #1 << n Sets n th bit of R0 Clear a bit: BIC R0, #1 << n Clears nth bit of R0 More Examples? Lecture #5 9

Example Program to do really simple math Write a program to add the word values starting at memory locations at 0x2000.0800, 0x2000.0804, and 0x2000.0808, and save the result starting at 0x2000.080C. Solution: Step 1: Address of first number R0 Step 2: First number into R1 [R0] R1 Step 3: Add second number to first number into R1 Step 4: Add third number to second number into R1 Step 5: Store result at destination address Step 6: End the program ADR R0, 0x20000800 First operand address in R0 LDR R1, [R0], #4 First Operand in R1 & increment R0 ADD R1, [R0], #4 Add second operand & increment R0 ADD R1, [R0], #4 Add third operand & increment R0 STR R1, [R0] Store sum Done BR Done Finished. Infinite Loop (standard) END Does this program do unsigned or signed arithmetic? What if we wanted to save result starting at 0x2000.081C? Lecture #5 10

Second Example Program Write a program to find the inner product of two 2-element vectors. The first vector begins at 0x2000.0400, and the second begins at 0x2000.0600. Store the result at address 0x2000.0700. Inner product is defined as IP=sum_for_all_i( x(i)*y(i)). Note: This is a very common operation in much of engineering (e.g. ECE311) Solution: Inner product of two very short vectors with word-length data ADR R0, 0x20000400 First vector address in R0 ADR R1, 0x20000600 Second vector address in R1 LDR R2, [R0], #4 x(1) in R2 & increment R0 LDR R3, [R1], #4 y(1) in R3 & increment R1 MUL R4, R2, R3 Multiply first elements together Running sum of products is now in R4 LDR R2, [R0] x(2) in R2 LDR R3, [R1] y(2) in R3 MLA R4, R2, R3, R4 Muliply second elements and add to previous ADR R0, 0x20000700 Inner product address STR R4, [R0] Store Inner product Done BR Done Finished. Infinite Loop END Does this program do unsigned or signed arithmetic? How would you write this if each vector had 1000 elements? Lecture #5 11

ARM Assembly Programs Three Components 1. Assembler Directives tell assembler actions to take - Define data and symbols - Reserve and initialize memory locations, ROM and RAM - Allows linking files - Specifies the end of a program - Etc. 2. Assembly Language Instructions ARM executes - Load/Store, Math, Shift/Rotate, Logical, etc. - Plus a few Pseudo-Instructions 3. Comments - Explain the meaning of single or grouped instructions Lecture #5 12

Anatomy of an Assembly Program: AREAs Programs include CODE (ROM) And DATA (RAM) AREAs. Lecture #5 13

Assembly Program: Code and Data Areas Lecture #5 14

Assembly Program: More Details Lecture #5 15

Assembly Directives Text Section 3.6 Directives are not instructions. Instead, they are used to provide key information for assembly. AREA ENTRY ALIGN DCB DCW DCD SPACE FILL EQU RN EXPORT IMPORT INCLUDE/GET PROC ENDP END Make a new block of data or code Declare an entry point where the program execution starts Align data or code to a particular memory boundary Allocate one or more bytes (8 bits) of data Allocate one or more half-words (16 bits) of data Allocate one or more words (32 bits) of data Allocate a zeroed block of memory with a particular size Allocate a block of memory and fill with a given value. Give a symbol name to a numeric constant Give a symbol name to a register Declare a symbol and make it referable by other source files Provide a symbol defined outside the current source file Include a separate source file within the current source file Declare the start of a procedure Designate the end of a procedure Designate the end of a source file Key directives are in BOLD above. Lecture #5 16

AREA Directive Details Array AREA mydata, DATA, DCD 1, 2, 3, 4, 5 READWRITE Define a data section Define an array with five integers main AREA mycode, CODE, EXPORT main ENTRY PROC... ENDP END READONLY Define a code section Make main visible to the linker Mark the entrance to the program PROC marks the begin of a subroutine Assembly program starts here. Mark the end of a subroutine Mark the end of a program The AREA directive indicates to the assembler the start of a new data or code section. Areas are the basic independent and indivisible unit processed by the linker. Each area is identified by a name. Areas within the same source file cannot share the same name. An assembly program must have at least one code area. A code area can only be read (read-only). A data area may be read from and written to (read-write). Lecture #5 17

END Directive Array AREA mydata, DATA, DCD 1, 2, 3, 4, 5 READWRITE Define a data section Define an array with five integers main AREA mycode, CODE, EXPORT main ENTRY PROC... ENDP END READONLY Define a code section Make main visible to the linker Mark the entrance to the program PROC marks the begin of a subroutine Assembly program starts here. Mark the end of a subroutine Mark the end of a program The END directive indicates the end of a source file. Any lines of information after this are ignored by the assembler. Each assembly source file must end with this directive. Lecture #5 18

EXPORT and IMPORT Directives Array AREA mydata, DATA, DCD 1, 2, 3, 4, 5 READWRITE Define a data section Define an array with five integers main AREA mycode, CODE, EXPORT main ENTRY PROC... ENDP END READONLY Define a code section Make main visible to the linker Mark the entrance to the program PROC marks the begin of a subroutine Assembly program starts here. Mark the end of a subroutine Mark the end of a program The EXPORT declares a symbol and makes this symbol visible to the linker. The IMPORT gives the assembler a symbol that is not defined locally in the current assembly file but which must be defined in another file to be used by the linker. Lecture #5 19

Data Allocation Directives Directive Description Memory Space DCB Define Constant Byte Reserve 8-bit values DCW Define Constant Half-word Reserve 16-bit values DCD Define Constant Word Reserve 32-bit values (word=32 bits) DCQ Define Constant Reserve 64-bit values SPACE Defined Zeroed Bytes Reserve a number of zeroed bytes FILL Defined Initialized Bytes Reserve and fill each byte with a value Lecture #5 20

Data Allocation Examples hello AREA DCB mydata, DATA, READWRITE "Hello World!",0 Allocate a string terminated by a zero dollar miles DCB 2,10,0,200 Allocate byte integers ranging from -128 to 255 DCW 100,200,50,0 Allocate integers between 32768 and 65535 p SPACE 255 Allocate 255 bytes of zeroed memory space f FILL 20,0xFF,1 Allocate 20 bytes and set each byte to 0xFF binary octal char DCB 0b01010101 Allocate a byte in binary DCB @73 Allocate a byte in octal DCB A Allocate a byte initialized to ASCII A Lecture #5 21

ALIGN Directive (examples) With ARM, in many cases instructions and data must begin on 2, 4, or 8 byte boundaries. The ALIGN directive instructs the Assembler to cause this to happen. AREA example, CODE, ALIGN = 3 Memory address begins at a multiple of 8 I.e. last 3 bits of address are 0 ADD r0, r1, r2 ADD Instructions start at a multiple of 8 AREA mydata, DATA, ALIGN = 2 Address starts at a multiple of four I.e. last 2 bits of address are 0 a DCB 0xFF The first byte of a 4-byte word ALIGN 4, 3 Align to the last byte (3) of a word (4) b DCB 0x33 Set the fourth byte of a 4-byte word c DCB 0x44 Add a byte to make following data misaligned ALIGN Force the next data to be aligned d DCD 12345 Skip three bytes and store the word Lecture #5 22

INCLUDE Directive main INCLUDE constants.s AREA main, CODE, READONLY EXPORT main ENTRY PROC... ENDP END Load Constant Definitions The INCLUDE (also called GET) directive is to include an assembly source file within another source file. More on this later. It is useful to include constant symbols defined by using EQU and stored in a separate source file. We will soon be using these regularly. Lecture #5 23

Assembly Programming Format See SampleCode.s on ECE251 Lab Code Website Assembly Programming Format: Note: *************************************************************** You are not required to include Student's name sections that are not used in your Date Lab # - Lab title program. For example, you will not SampleCode.s need the INCLUDES section for the Description of the program SampleCode *************************************************************** first several assignments. EQU Directives These directives do not allocate memory *************************************************************** SYMBOL DIRECTIVE VALUE COMMENT SYM_ZERO EQU 0x00 *************************************************************** Directives - This Data Section is part of the code It is in the read only section so values cannot be changed. *************************************************************** LABEL DIRECTIVE VALUE COMMENT AREA odata, DATA, READONLY THUMB MSG1 DCB "My message..." DCB 0x0D New line DCB 0x04 End of message DATA1 DCB 0x00 Useful data *************************************************************** Program section *************************************************************** LABEL DIRECTIVE VALUE COMMENT AREA main main, CODE, READONLY THUMB EXTERN OutStr Reference external subroutine EXPORT your code goes here main Lecture #5 24

Some Simple Coding Examples See EZTEST.s on ECE251 Lab Code Website Very Simple Test Framework Requiring no RAM or ROM Data AREA RESET, READONLY EXPORT Vectors Vectors DCD 0x400 stack pointer for empty stack DCD Reset_Handler reset vector AREA MYCODE, CODE, READONLY EXPORT Reset_Handler Reset_Handler Test Code Goes Here done B done END Lecture #5 25

Some Simple Coding Examples See EZTESTROMRAM.s on ECE251 Lab Code Website EZTESTROMRAM: Very Simple Test Framework LABEL DIRECTIVE VALUE AREA ROM, DATA, READONLY Rom Data goes here LABEL DIRECTIVE VALUE AREA RAM, DATA, READWRITE RAM Data Structures go here AREA RESET, READONLY EXPORT Vectors Vectors DCD 0x400 stack pointer for empty stack DCD Reset_Handler reset vector AREA MYCODE, CODE, READONLY EXPORT Reset_Handler Reset_Handler Test Code Goes Here done B done END Lecture #5 26

Summary Shift/Rotate Instructions Bitwise logic and Saturating Instructions A Few Math Programming Examples ARM Assembly Language and Assembler Bring questions Thursday if you re still confused after reading text, reviewing slides, and studying this! Next Lecture (see Chapter 6 in text): Conditional Branch Instructions Conditional Execution Example flowcharts and examples Programming Practice as time allows Lecture #5 27