ARM PROGRAMMING. When use assembly

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

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines

Exam 1. Date: March 1, 2019

Systems Architecture The ARM Processor

Assembly Language Programming

Subroutines and the Stack

Modes and Levels. Registers. Registers. Cortex-M3 programming

(5) Question 2. Give the two most important factors for effective debugging. Jonathan W. Valvano

Support for high-level languages

EE4144: ARM Cortex-M Processor

Exam 1. Date: March 1, 2019

ARM Memory Addressing and Function Calls

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

ARM Assembly Language. Programming

EE251: Tuesday September 5

Exam 1. Date: February 23, 2016

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

Advanced Assembly, Branching, and Monitor Utilities

CprE 288 Translating C Control Statements and Function Calls, Loops, Interrupt Processing. Instructors: Dr. Phillip Jones Dr.

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

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

Interrupt-Driven Input/Output

University of Texas at Austin Electrical and Computer Engineering Department. EE319K, Embedded Systems, Spring 2013 Final Exam

EE319K (Gerstlauer), Spring 2013, Midterm 1 1. Midterm 1. Date: February 21, 2013

ECE 471 Embedded Systems Lecture 5

ECE251: Thursday September 27

ECE251: Thursday September 13

NET3001. Advanced Assembly

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

Computer Systems Lecture 9

E85 Lab 8: Assembly Language

ECE251: Tuesday September 18

Systems Architecture The Stack and Subroutines

CSE /003, Fall 2014, Homework 4 Due October 7, 2014 in Class (at 2:00pm for 002, 3:30pm for 003)

Exam 1. Date: Oct 4, 2018

Optimizing ARM Assembly

ECE 471 Embedded Systems Lecture 8

ARM Assembly Programming II

ARM ASSEMBLY PROGRAMMING

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

EE251: Tuesday October 23

ECE 30 Introduction to Computer Engineering

Application Note. Analyzing HardFaults on Cortex-M CPU

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

Interrupts (Exceptions) (From LM3S1968) Gary J. Minden August 29, 2016

Real-Time Systems / Real-Time Operating Systems EE445M/EE380L.6, Spring 2015

Embedded Systems. October 2, 2017

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

ECE254 Lab3 Tutorial. Introduction to Keil LPC1768 Hardware and Programmers Model. Irene Huang

ARM Cortex-M4 Programming Model

button.c The little button that wouldn t

ECE 498 Linux Assembly Language Lecture 5

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

Architecture. Digital Computer Design

Chapter 15. ARM Architecture, Programming and Development Tools

C-Style Strings. CS2253 Owen Kaser, UNBSJ

Interrupts (Exceptions) Gary J. Minden September 11, 2014

Developing StrongARM/Linux shellcode

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

Exam 1. Date: February 23, 2018

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

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

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Exam 1. Date: February 23, 2018

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

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

CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls

Pointers (2G) Young Won Lim 3/7/18

Comparison InstruCtions

Lecture 3: Instruction Set Architecture

Advanced Operating Systems Embedded from Scratch: System boot and hardware access. Federico Terraneo

Program SoC using C Language

ARM Assembly Programming

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

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

Computer Organization & Assembly Language Programming (CSE 2312)

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

Viewing the tm4c123gh6pm_startup_ccs.c file, the first piece of code we see after the comments is shown in Figure 1.

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

Computer Organization & Assembly Language Programming (CSE 2312)

Assembler Programming. Lecture 10

Computer System Architecture

ARM Assembly Programming

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

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

EECS 373 Winter 2017 Homework #3

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

Input/Output Programming

Developing a Generic Hard Fault handler for ARM Cortex-M3/Cortex-M4

Processor Status Register(PSR)

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

EE445M/EE380L.6 Quiz 2 Spring 2017 Solution Page 1 of 5

Branch Instructions. R type: Cond

Topic 10: Instruction Representation

VE7104/INTRODUCTION TO EMBEDDED CONTROLLERS UNIT III ARM BASED MICROCONTROLLERS

A QUICK INTRO TO PRACTICAL OPTIMIZATION TECHNIQUES

Cortex M3 Programming

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

ARM Cortex-M4 Architecture and Instruction Set 1: Architecture Overview

Chapter 15. ARM Architecture, Programming and Development Tools

Transcription:

ARM PROGRAMMING Bùi Quốc Bảo When use assembly Functions that cannot be implemented in C, such as special register accesses and exclusive accesses Timing-critical routines Tight memory requirements, causing part of the program to be written in assembly to get the smallest memory size 1

Example of assembly program STACK_TOP EQU 0x20002000 ; constant for SP starting value AREA RESET, CODE THUMB DCD STACK_TOP ; Stack top DCD Start ; Reset vector AREA Vect, CODE ENTRY ; Indicate program execution start here Start ; Start of main program MOV r0, #10 ; Starting loop counter value MOV r1, #0 ; starting result ; Calculated 10+9+8+...+1 loop ADD r1, r0 ; R1=R1 + R0 SUBS r0, #1 ; Decrement R0, update? ag ( S suf? x) BNE loop ; If result not zero jump to loop, Result is now in R1 deadloop B deadloop ; Infinite loop END C Programming 2

C programming #include "inc/lm3s9b96.h" int main(void) volatile unsigned long ulloop; SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOF; ulloop = SYSCTL_RCGC2_R; GPIO_PORTF_DIR_R = 0x08; GPIO_PORTF_DEN_R = 0x08; while(1) GPIO_PORTF_DATA_R = 0x08; for(ulloop = 0; ulloop < 200000; ulloop++); GPIO_PORTF_DATA_R &= ~(0x08); for(ulloop = 0; ulloop < 200000; ulloop++); Startup code AREA RESET, CODE, READONLY THUMB EXPORT Vectors Vectors DCD StackMem + Stack ; Top of Stack DCD Reset_Handler ; Reset Handler DCD NmiSR ; NMI Handler DCD FaultISR ; Hard Fault Handler DCD IntDefaultHandler ; The MPU fault handler DCD IntDefaultHandler ; The bus fault handler DCD IntDefaultHandler ; The usage fault handler DCD 0 ; Reserved DCD 0 ; Reserved DCD 0 ; Reserved DCD 0 ; Reserved DCD IntDefaultHandler ; SVCall handler DCD IntDefaultHandler ; Debug monitor handler 3

Startup code EXPORT Reset_Handler Reset_Handler IMPORT main B main Calling assembler from C First four arguments to a function are passed in registers r0 to r3 (any further parameters being passed on the stack) A single word result is returned in r0 4

Main.c #include <stdio.h> extern void strcopy(char *d, char *s); int main() char *srcstr = "First string - source "; char *dststr = "Second string - destination "; strcopy(dststr,srcstr); return (0); 5

Func.s AREA asm_func, CODE, READONLY THUMB EXPORT strcopy strcopy ; copy first string over second LDRB r2, [r1],#1 ; load byte and update address STRB r2, [r0],#1 ; store byte and update ;address; CMP r2, #0 ; check for zero terminator BNE strcopy ; keep going if not stop BX LR ALIGN END Local variable type int checksum_v1(int *data) char i; int sum = 0; for (i = 0; i < 64; i++) sum += data[i]; return sum; ARM registers are 32 bits Stack entries are at least 32 bits. To implement the i++ operation, the compiler have to check if i = 255 or not. So, declaring is as char is inefficient. i should be declared as integer 6

Function argument type short add_v1(short a, short b) return a+(b>>1); MOV r2,r0 ADD r0,r2,r1,asr #1 SXTH r0,r0 BX LR Function argument type int add_v2(int a, int b) return a+(b>>1); MOV r2,r0 ADD r0,r2,r1,asr #1 BX LR It is more efficient to use the int type for function arguments and return values, even if you are only passing an 8-bit value. 7

Loop structure int checksum_v5(int *data) unsigned int i; int sum=0; for (i=0; i<64; i++) sum += *(data++); return sum; An ADD to increment i A compare to check if i is less than 64 A conditional branch to continue the loop if i <64 PUSH r4,lr MOV r1,r0 MOV r3,#0x00 MOV r0,r3 MOV r2,r3 B check LOOP ADD r3,r1,#0x04 LDR r4,[r1,#0x00] MOV r1,r3 ADD r3,r4,r0 MOV r0,r3 ADD r3,r2,#0x01 MOV r2,r3 check CMP r2,#0x40 BCC LOOP POP r4,pc Loop structure int checksum_v5(int *data) unsigned int i; int sum=0; for (i=64; i!=0; i--) sum += *(data++); return sum; For loop structure, we should use decrementing loop 8

Arguments passing char *queue_bytes_v1 ( char *Q_start, char *Q_end, char *Q_ptr, char *data, unsigned int N) do *(Q_ptr++) = *(data++); if (Q_ptr == Q_end) Q_ptr = Q_start; while (--N); return Q_ptr; Arguments passing queue_bytes_v1 STR r14,[r13,#-4]! LDR r12,[r13,#4] queue_v1_loop LDRB r14,[r3],#1 STRB r14,[r2],#1 CMP r2,r1 MOVEQ r2,r0 SUBS r12,r12,#1 BNE queue_v1_loop MOV r0,r2 LDR pc,[r13],#4 ; save lr on the stack ; r12 = N ; r14 = *(data++) ; *(Q_ptr++) = r14 ; if (Q_ptr == Q_end) ; Q_ptr = Q_start; ; --N and set flags ; if (N!=0) goto loop ; r0 = Q_ptr ; return r0 9

Arguments passing typedef struct char *Q_start; /* Queue buffer start address */ char *Q_end; /* Queue buffer end address */ char *Q_ptr; /* Current queue pointer position */ Queue; void queue_bytes_v2(queue *queue, char *data, unsigned int N) char *Q_ptr = queue->q_ptr; char *Q_end = queue->q_end; Arguments passing do *(Q_ptr++) = *(data++); if (Q_ptr == Q_end) Q_ptr = queue->q_start; while (--N); queue->q_ptr = Q_ptr; To save the time of passing arguments to stack and pulling arguments from stack, one should try to reduce number of arguments to 4. 10