Assembly Language Programming

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

EE251: Tuesday September 5

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

Systems Architecture The ARM Processor

ARM Cortex-M4 Programming Model

Instruction : A command to the microprocessor to perform a given task on specified data. Each instruction has two parts

Subroutines and the Stack

ELEG3924 Microprocessor

ELEG3923 Microprocessor Ch.2 Assembly Language Programming

The 8051 Microcontroller and Embedded Systems

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

ARM PROGRAMMING. When use assembly

EE4144: ARM Cortex-M Processor

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

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

Program SoC using C Language

CodeWarrior. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

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

Assembly Language programming (1)

TOPIC 3 INTRODUCTION TO PIC ASSEMBLY LANGUAGE. E4160 Microprocessor & Microcontroller System. Prepared by : Puziah Yahaya JKE, POLISAS / DEC 2010

ARM Cortex A9. ARM Cortex A9

Advanced Assembly, Branching, and Monitor Utilities

Chapter 15. ARM Architecture, Programming and Development Tools

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

ARM ASSEMBLY PROGRAMMING

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

MIPS (SPIM) Assembler Syntax

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

Microprocessors and Microcontrollers Prof. Santanu Chattopadhyay Department of E & EC Engineering Indian Institute of Technology, Kharagpur

ECE251: Thursday September 13

ORG ; TWO. Assembly Language Programming

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

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

ARM Processor. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CPE 323 MSP430 INSTRUCTION SET ARCHITECTURE (ISA)

8051 Microcontrollers

Four Categories Of 8085 Instructions That >>>CLICK HERE<<<

CS401 - Computer Architecture and Assembly Language Programming Glossary By

ARM ARCHITECTURE. Contents at a glance:

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

Assembly Language. CS2253 Owen Kaser, UNBSJ

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

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

Menu Computer Organization Programming Model for the an example microprocessors (the G-CPU & Motorola 68HC11) Assembly Programming Look into my...

ENE 334 Microprocessors

E85 Lab 8: Assembly Language

EE 5340/7340 Motorola 68HC11 Microcontroler Lecture 1. Carlos E. Davila, Electrical Engineering Dept. Southern Methodist University

Computer Organization CS 206 T Lec# 2: Instruction Sets

Computer Organization and Assembly Language. Lab Session 3

ARM Ltd. ! Founded in November 1990! Spun out of Acorn Computers

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

Writing ARM Assembly. Steven R. Bagley


SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

Exam 1. EE319K Spring 2013 Exam 1 (Practice 1) Page 1. Date: February 21, 2013; 9:30-10:45am. Printed Name:

Assembly Language Manual for the STACK Computer

Chapter 7 Central Processor Unit (S08CPUV2)

Computer Organization


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

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

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

68000 Assembler by Paul McKee. User's Manual

Computer Architecture and Organization. Instruction Sets: Addressing Modes and Formats

Lecture 5. EEE3410 Microcontroller Applications Department of Electrical Engineering Assembly Language Programming (1)

Chapter 4. MARIE: An Introduction to a Simple Computer

CC411: Introduction To Microprocessors

Computer Architecture 1 ح 303

Laboratory: Introduction to Mechatronics. Instructor TA: Edgar Martinez Soberanes Lab 2. PIC and Programming

1. INTRODUCTION TO MICROPROCESSOR AND MICROCOMPUTER ARCHITECTURE:

Job Posting (Aug. 19) ECE 425. ARM7 Block Diagram. ARM Programming. Assembly Language Programming. ARM Architecture 9/7/2017. Microprocessor Systems

Instruction Sets: Characteristics and Functions Addressing Modes

Summer 2003 Lecture 14 07/02/03

Computers Programming Course 5. Iulian Năstac

ECE 471 Embedded Systems Lecture 5

Processor Status Register(PSR)

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

Practical Malware Analysis

EC 413 Computer Organization

We will study the MIPS assembly language as an exemplar of the concept.

ARM Assembly Language. Programming

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

Lecture 5 Assembly Programming: Arithmetic

Implementing Secure Software Systems on ARMv8-M Microcontrollers

Addressing Modes. Immediate Direct Indirect Register Register Indirect Displacement (Indexed) Stack

STRUCTURE OF DESKTOP COMPUTERS

Instruction Sets: Characteristics and Functions

Figure Programming model

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

Computer Organization & Assembly Language Programming. CSE 2312 Lecture 15 Addressing and Subroutine

Lecture #2 January 30, 2004 The 6502 Architecture

4. Structure of a C++ program

Exam 1. Date: February 23, 2016

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

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

Instruction Set Architecture

ME4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 7

8051 Overview and Instruction Set

3.0 Instruction Set. 3.1 Overview

Transcription:

Experiment 3 Assembly Language Programming Every computer, no matter how simple or complex, has a microprocessor that manages the computer s arithmetical, logical and control activities. A computer program is a collection of numbers stored in memory in the form of ones and zeros. CPU reads these numbers one at a time, decodes them and perform the required action. We term these numbers as machine language. Although machine language instructions make perfect sense to computer but humans cannot comprehend them. A long time ago, someone came up with the idea that computer programs could be written using words instead of numbers and a new language of mnemonics was developed and named as assembly language. An assembly language is a low-level programming language and there is a very strong (generally one-to-one) correspondence between the language and the architecture s machine code instructions. We know that computer cannot interpret assembly language instructions. A program should be developed that performs the task of translating the assembly language instructions to machine language. A computer program that translates the source code written with mnemonics into the executable machine language instructions is called an assembler. The input of an assembler is a source code and its output is an executable object code. Assembly language programs are not portable because assembly language instructions are specific to a particular computer architecture. Similarly, a different assembler is required to make an object code for each platform. The ARM Architecture The ARM is a Reduced Instruction Set Computer(RISC) with a relatively simple implementation of a load/store architecture, i.e. an architecture where main memory (RAM) access is performed through dedicated load and store instructions, while all computation (sums, products, logical operations, etc.) is performed on values held in registers. ARM supports a small number of addressing modes with all load/store addresses being determined from registers and instruction fields only. The ARM architecture provides 16 registers, numbered from R0 to R15, of which the last three have special hardware significance. R13, also known as SP, is the stack pointer, that is it holds the address of the top element of the program stack. It is used automatically in the PUSH and POP instructions to manage storage 32

33 label opcode operand1, operand2, ; Comment and recovery of registers in the stack. R14, also known as LR is the link register, and holds the return address, that is the address of the first instruction to be executed after returning from the current function. R15, also known as PC is the program counter, and holds the address of the next instruction. Assembly Language Syntax ARM assembler commonly uses following instruction format: Normally, the first operand is the destination of the operation. The number of operands in an instruction depends on the type of instruction, and the syntax format of the operand can also be different. Label Label is an optional first field of an assembly statement. Labels are alphanumeric names used to define the starting location of a block of statements. Labels can be subsequently used in our program as an operand of some other instruction. When creating the executable file the assembler will replace the label with the assigned value. Labels must be unique in the executable file because an identical label encountered by the Assembler will generate an error. ARM assembler has reserved first character of a line for the label field and it should be left blank for the instructions with no labels. In some compilers, labels can be optionally ended with colon(:) but it is not accepted by the ARM assembler as an end of the label field. Defining appropriate labels makes your program look more legible. Program location can be easily found and remembered using labels. It is easier to use certain functionality of your program in an entirely different code.i.e., your code can be used as a library program. You do not have to figure out memory addresses because it is a tedious task especially when the instruction size in your microcontroller is not fixed. Opcode (Mnemonics) Opcode is the second field in assembly language instruction. Assembly language consists of mnemonics, each corresponding to a machine instruction. Using a mnemonic you can decide

34 CHAPTER 3. ASSEMBLY LANGUAGE PROGRAMMING what operation you want to perform on the operands. Assembler must translate each mnemonic opcode into their binary equivalent. Operands Next to the opcode is the operand field which might contain different number of operands. Some of the instructions in Cortex-M3 will have no operand while other might have as many as four operands. The number of operands in an instruction depends on the type of instruction, and the syntax format of the operand can also be different. Normally, the first operand is the destination of the operation. Comments Comments are messages intended only for human consumption. They have no effect on the translation process and indeed are not acted on by the ARM Assembler. The comment field of an assembly language instruction is also optional. A semicolon signies that the rest of the line is a comment and is to be ignored by the assembler. If the semicolon is the rst non-blank character on the line, the entire line is ignored. If the semicolon follows the operands of an instruction, then only the comment is ignored by the assembler. The purpose of comments is to make the program more comprehensible to the human reader. If you want to modify the program in response to a product update and you haven t used that code before then comments go a long way to helping comprehension. A First Assembly Program In this section we learn to write a simple assembly language program with and without the startup file. This program can be used as a template for writing any assembly language program. An assembler is requires to compile an assembly language program. Keil has an ARM assembler which can compile and build ARM assembly language programs. Assembly language is only a set of instructions for specific hardware resources. To drive the assembly and linking process, we need to us directives, which are interpreted by the assembler. Some commonly used directives are explained in section - Template for ARM Assembly Program ; F i r s t ARM A s s e m b l y l a n g u a g e p r o g r a m PRESERVE8 THUMB ; m a r k s t h e THUMB mode o f o p e r a t i o n AREA. t e x t, CODE, READONLY, ALIGN =2 ENTRY ; s t a r t i n g p o i n t o f t h e code e x e c u t i o n

35 EXPORT m a i n ; d e c l a r a t i o n o f i d e n t i f i e r main m a i n ; a d d r e s s o f t h e main f u n c t i o n ; U s e r Code S t a r t s f r o m t h e n e x t l i n e END ; End o f t h e p r o g r a m, m a t c h e d w i t h ENTRY k e y w o r d Template for ARM Assembly Program Example 3.1: Assembly Program with Startup File ; T h i s i s t h e f i r s t ARM A s s e m b l y l a n g u a g e p r o g r a m. D e s c r i b e t h e ; f u n c t i o n a l i t y o f t h e p r o g r a m a t t h e t o p. You can a l s o i n c l u d e ; t h e i n f o r e g a r d i n g t h e a u t h o r and t h e d a t e ; ; ; D i r e c t i v e s PRESERVE8 THUMB ; M a r k s t h e THUMB mode o f o p e r a t i o n ; ; ; ; ; ; ; ; ; ; The u s e r code ( p r o g r a m ) i s p l a c e d i n CODE AREA AREA. t e x t, CODE, READONLY, ALIGN =2 ENTRY ; M a r k s t h e s t a r t i n g p o i n t o f code e x e c u t i o n EXPORT m a i n m a i n ; U s e r Code S t a r t s f r o m t h e n e x t l i n e MOV R5, #0 x 1 2 3 4 ; S t o r e some a r b i t r a r y n u m b e r s MOV R3, #0 x 1 2 3 4 ADD R6, R5, R3 ; Add t h e v a l u e s i n R5 and R3 and s t o r e t h e ; r e s u l t i n R6 STOP B STOP ; E n d l e s s l o o p END ; End o f t h e p r o g r a m, m a t c h e d w i t h ; ENTRY k e y w o r d Example 3.2: Assembly Language Program without Startup File ; ; ; D i r e c t i v e s PRESERVE8 THUMB ; M a r k s t h e THUMB mode o f o p e r a t i o n S t a c k EQU 0 x 0 0 0 0 0 1 0 0 ; D e f i n e s t a c k s i z e t o be 256 b y e s ; A l l o c a t e s p a c e f o r t h e s t a c k. AREA STACK, NOINIT, READWRITE, ALIGN =3

36 CHAPTER 3. ASSEMBLY LANGUAGE PROGRAMMING StackMem SPACE S t a c k ; I n i t i a l i z e t h e t w o e n t r i e s o f v e c t o r t a b l e. AREA RESET, DATA, READONLY EXPORT V e c t o r s V e c t o r s DCD StackMem + S t a c k ; s t a c k p o i n t e r v a l u e when s t a c k i s e m p t y DCD R e s e t _ H a n d l e r ; r e s e t v e c t o r ALIGN ; ; ; ; ; ; ; ; ; ; The u s e r code ( p r o g r a m ) i s p l a c e d i n CODE AREA AREA. t e x t, CODE, READONLY, ALIGN =2 ENTRY ; M a r k s t h e s t a r t i n g p o i n t o f t h e code e x e c u t i o n EXPORT R e s e t _ H a n d l e r R e s e t _ H a n d l e r ; U s e r Code S t a r t s f r o m t h e n e x t l i n e MOV R5, #0 x 1 2 3 4 ; Move some a r b i t r a r y number i n R5 MOV R3, #0 x 1 2 3 4 ADD R6, R5, R3 ; Add t h e v a l u e s i n R5 and R3 and s t o r e t h e ; r e s u l t i n R6 STOP B STOP ; E n d l e s s l o o p END ; End o f t h e p r o g r a m, m a t c h e d w i t h ; ENTRY k e y w o r d Assembler Directives Every program to be executed by a computer is a sequence of statements that can be read by the humans. An assembler must differentiate between the statements that will be converted into executable code and that instruct the assembler to perform some specific function. Assembler directives are commands to the assembler that direct the assembly process. Assembler directives are also called pseudo opcodes or pseudo-operations. They are executed by the assembler at assembly time not by the processor at run time. Machine code is not generated for assembler directives as they are not directly translated to machine language. Some tasks performed by these directives are: 1. Assign the program to certain areas in memory. 2. Define symbols. 3. Designate areas of memory for data storage. 4. Place tables or other fixed data in memory. 5. Allow references to other programs.

37 Attribute CODE DATA READONLY READWRITE NOINIT Explanation Contains machine instructions. READONLY is the default. Contains data, not instructions. READWRITE is the default. Indicates that this area should not be written to. Indicates that this area may be read from or written to. Indicates that the data area is initialized to zero. It contains only reservation directives with no initialized values. ARM assembler supports a large number of assembler directives but in this lab manual we will discuss only some important directives which are required for this lab. AREA Directive AREA directive allows the programmer to specify the memory location to store code and data. Depending on the memory configuration of your device, code and data can reside in different areas of memory. A name must be specified for an area directive. There are several optional comma delimited attributes that can be used with AREA directive. Some of them are discussed below. Example 3.3 AREA Example, CODE, READONLY ; An e x a m p l e code s e c t i o n. ; u s e r c ode ENTRY and END Directives The first instruction to be executed within an application is marked by the ENTRY directive. Entry point must be specified for every assembly language program. An application can contain only a single entry point and so in a multi-source module application, only a single module will contain an ENTRY directive. This directive causes the assembler to stop processing the current source file. Every assembly language source module must therefore finish with this directive. Example 3.4 AREA MyCode, CODE, READONLY ENTRY ; E n t r y p o i n t f o r t h e a p p l i c a t i o n S t a r t

38 CHAPTER 3. ASSEMBLY LANGUAGE PROGRAMMING ; u s e r c ode END ; I n f o r m s t h e a s s e m b l e r a b o u t t h e end o f a s o u r c e f i l e EXPORT and IMPORT Directives A project may contain multiple source files. You may need to use a symbol in a source file that is defined in another source file. In order for a symbol to be found by a different program file, we need to declare that symbol name as a global variable. The EXPORT directive declares a symbol that can be used in different program files. GLOBAL is a synonym for EXPORT. The IMPORT directive provides the assembler with a name that is not defined in the current assembly. Example 3.5 AREA Example, CODE, READONLY IMPORT U s e r _ C o d e ; I m p o r t t h e f u n c t i o n name f r o m ; o t h e r s o u r c e f i l e. EXPORT DoAdd ; E x p o r t t h e f u n c t i o n name ; t o be used by e x t e r n a l ; m o d u l e s. DoAdd ADD R0, R0, R1 ARM, THUMB Directives The ARM directive instructs the assembler to interpret subsequent instructions as 32-bit ARM instructions. If necessary, it also inserts up to three bytes of padding to align to the next word boundary. The ARM directive and the CODE32 directive are synonyms. The THUMB directive instructs the assembler to interpret subsequent instructions as 16-bit Thumb instructions. If necessary, it also inserts a byte of padding to align to the next halfword boundary. In files that contain a mixture of ARM and Thumb code Use THUMB when changing from ARM state to Thumb state. THUMB must precede any Thumb code. Use ARM when changing from Thumb state to ARM state. ARM must precede any ARM code. Example 3.6

39 AREA C h a n g e S t a t e, CODE, READONLY ARM ; T h i s s e c t i o n s t a r t s i n ARM s t a t e LDR r0, = s t a r t +1 ; Load t h e a d d r e s s and s e t t h e ; l e a s t s i g n i f i c a n t b i t BX r 0 ; B r a n c h and e x c h a n g e i n s t r u c t i o n s e t s ; N o t n e c e s s a r i l y i n same s e c t i o n THUMB ; F o l l o w i n g i n s t r u c t i o n s a r e Thumb s t a r t MOV r1, # 1 0 ; Thumb i n s t r u c t i o n s ALIGN Directive Use of ALIGN ensures that your code is correctly aligned. By default, the ALIGN directive aligns the current location within the code to a word (4-byte) boundary. ALIGN 2 can also be used to align on a halfword (2-byte) boundary in Thumb code. As a general rule it is safer to use ALIGN frequently through your code. PRESERVE8 Directive The PRESERVE8 directive specifies that the current file preserves 8-byte alignment of the stack. LDRD and STRD instructions (double-word transfers) only work correctly if the address they access is 8-byte aligned. If your code preserves 8-byte alignment of the stack, use PRESERVE8 to inform the linker. The linker ensures that any code that requires 8-byte alignment of the stack is only called, directly or indirectly, by code that preserves 8-byte alignment of the stack. Data Reservation Directives (DCB, DCD, DCW) ARM assembler supports different data definition directives to insert constants in assembly code. This directive allows the programmer to enter fixed data into the program memory and treats that data as a permanent part of the program. Different variants of these directives are: 1. DCB (Define Constant Byte) to define constants of byte size. 2. DCW (Define Constant Word) allocates one or more halfwords of memory, aligned on two-byte boundaries. 3. DCD (Define Constant Data) allocates one or more words of memory, aligned on four-byte boundaries. Example 3.7

40 CHAPTER 3. ASSEMBLY LANGUAGE PROGRAMMING d a t a DCD 0, 0, 0 ; D e f i n e s 3 w o r d s i n i t i a l i z e d t o z e r o s SPACE Directive The SPACE directive reserves a zeroed block of memory. ALIGN directive must be used to align any code following a SPACE directive. Example 3.8 AREA MyData, DATA, READWRITE d a t a 1 SPACE 255 ; d e f i n e s 255 b y t e s o f z e r o e d s t o r e