0% found this document useful (0 votes)
139 views35 pages

Chapter 3 Instructions ARM

This document discusses ARM assembly language and programming. It begins by explaining that ARM processors have multiple instruction sets, including the 32-bit ARM instruction set, 16-bit Thumb instruction set, mixed 16/32-bit Thumb-2 instruction set, and others. It then discusses the levels of program code from high-level languages like C to assembly language to machine language. Finally, it provides an overview of ARM assembly instructions, instruction formats, and common directives used in assembly programming.

Uploaded by

Slaney Aguiar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views35 pages

Chapter 3 Instructions ARM

This document discusses ARM assembly language and programming. It begins by explaining that ARM processors have multiple instruction sets, including the 32-bit ARM instruction set, 16-bit Thumb instruction set, mixed 16/32-bit Thumb-2 instruction set, and others. It then discusses the levels of program code from high-level languages like C to assembly language to machine language. Finally, it provides an overview of ARM assembly instructions, instruction formats, and common directives used in assembly programming.

Uploaded by

Slaney Aguiar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapters

3
ARM Assembly
Embedded Systems with ARM Cortext-M
Updated: Wednesday, February 7, 2018
Programming languages -
Categories
Interpreted based on the machine

Less complex, not as efficient

Efficient, more complex


Programming languages -
Interpreter Vs. Compiler

Assembler convers instructions into Machine Language 1s and 0s.


C Compiler
Assemblers and C Compilers
Assemblers and C Compilers
IDE -
Integrated
Development
Environment
ARM Assembly
• Modern ARM processors have several instruction sets:
• The fully-featured 32-bit ARM instruction set,
• The more restricted, but space efficient, 16-bit Thumb instruction set,
• The newer mixed 16/32-bit Thumb-2 instruction set,
• Jazelle DBX for Java byte codes,
• The NEON 64/128-bit SIMD instruction set,
• The VFP vector floating point instruction set.
• à Thumb-2 is the progression of Thumb (strictly it is Thumb v3). It
improves performance whilst keeping the code density tight by allowing a
mixture of 16- and 32-bit instructions.

http://www.davespace.co.uk/arm/introduction-to-arm/instruction-sets.html
Levels of Program Code
C Code à Assembly à Machine Language
C Program Assembly Program Machine Program
0010000100000000
int main(void){ 0010000000000000
int i; 1110000000000001
int total = 0; Compile Assemble 0100010000000001
for (i = 0; i < 10; i++) {
0001110001000000
total += i;
} 0010100000001010
while(1); // Dead loop 1101110011111011
} 1011111100000000
1110011111111110

} High-level language
} Assembly language } Hardware
} Level of abstraction
} Textual representation representation
closer to problem
domain of instructions } Binary digits (bits)
} Provides for productivity } Encoded instructions
and portability and data

8
Levels of Program Code
C Code à Assembly à Machine Language

Machine Program
0010000100000000 2100 ; MOVS r1, #0x00
0010000000000000
1110000000000001 2201 ; MOVS r2, #0x01
0100010000000001 188B ; ADDS r3, r1, r2
0001110001000000
0010100000001010 2000 ; MOVS r0, #0x00
1101110011111011 4770 ; BX lr
1011111100000000
1110011111111110
In Hex
} Hardware representation
} Binary digits (bits)
} Encoded instructions and
data

9
Assembly Instruction Sets for Cortex-M

Examples:
• ADD
• AND
• CMP
• SUB
• MUL
• MOV
• etc.

10
from arm.com
Assembly Instructions Supported
Compare &
• Arithmetic and logic Branch
Data Movement
• Add, Subtract, Multiply, Divide, Shift, Rotate
• Data movement
Misc. Arithmetic
• Load, Store, Move
• Compare and branch
• Compare, Test, If-then, Branch, compare and branch on zero
• Miscellaneous
• Breakpoints, wait for events, interrupt enable/disable, data memory barrier,
data synchronization barrier

11
ARM Instruction Format
label mnemonic operand1, operand2, operand3 ; comments

} Label is a reference to the memory address of this instruction.


} Mnemonic represents the operation to be performed (ADD, SUB, etc.).
} The number of operands varies, depending on each specific instruction.
Some instructions have no operands at all.
} Typically, operand1 is the destination register, and operand2 and operand3 are
source operands.
} operand2 is usually a register.
} operand3 may be a register, an immediate number, a register shifted to a
constant amount of bits, or a register plus an offset (used for memory access).
} Everything after the semicolon “;” is a comment, which is an annotation
explicitly declaring programmers’ intentions or assumptions.

12
ARM Instruction Format
label mnemonic operand1, operand2, operand3 ; comments

target ADD r0, r2, r3 ; r0 = r2 + r3

label mnemonic destination 1st source 2nd source comment


operand operand operand

13
ARM Instruction Format
label mnemonic operand1, operand2, operand3 ; comments

Examples: Variants of the ADD instruction


ADD r1, r2, r3 ; r1 = r2 + r3 Remember:
R has two components:
ADD r1, r3 ; r1 = r1 + r3 • Register Address
ADD r1, r2, #4 ; r1 = r2 + 4 • Register Content

ADD r1, #15 ; r1 = r1 + 15

14
First Assembly

15
INCLUDE constants.s ; Load Constant Definitions
Assembly Directives
} Directives are NOT instruction; allocate space and define
types in many cases. They are used to provide key Start of new data set; At least one code
information for assembly. area is required per program; could be
READONLY or READWRITE
AREA Make a new block of data or code
ENTRY Declare an entry point where the program execution starts
ALIGN Align data or code to a particular memory boundary
Typically 2^N
DCB Allocate one or more bytes (8 bits) of data
DCW Allocate one or more half-words (16 bits) of data
DCD Allocate one or more words (32 bits) of data
SPACE Allocate a zeroed block of memory with a particular size
For Example INCLUDE constants.s
FILL Allocate a block of memory and fill with a given value. ; Load Constant Definitions
EQU Give a symbol name to a numeric constant
RN Give a symbol name to a register
EXPORT Declare a symbol and make it referable by other source files
IMPORT Provide a symbol defined outside the current source file Refers to PROCEDURE and End of
INCLUDE/GET Include a separate source file within the current source file PROCEDURE; it defines the function;
PROC Declare the start of a procedure similar to main() in C
ENDP Designate the end of a procedure
END Designate the end of a source file
END of source file 16
Let’s Practice First ….
Assembly Emulator
• Go to
https://salmanarif.bitbucket.io/vi
sual/index.html
CODE
• Download the appropriate
version for your computer
CPU
• This is the list of supported REGISTORS
instructions:
https://salmanarif.bitbucket.io/vi
sual/supported_instructions.html
Run your Assembly Code Move ONE instruction
at a time

What is your final PC value?


Try This & Answer the Questions…..
Let’s Continue with Some Simple
Commands….
Let’s Take a MOV & MVN Commands
• MOV r0, #42 • <operation>
• Move the constant 42 into register • MOV – move
R0.
• Rd := Operand2
• MOV r2, r3 • MVN – move NOT
• Move the contents of register R3
into register R2. • Rd := 0xFFFFFFFF EOR Operand2

• MVN r1, r0
• R1 = NOT(R0) = -43
• MOV r0, r0
• A NOP (no operation) instruction.
Arithmetic Operation
• <operation> • ADD r0, r1, r2
• ADD – Add • R0 = R1 + R2
• Rd := Rn + Operand2 • SUB r5, r3, #10
• ADC – Add with Carry • R5 = R3 − 10
• Rd := Rn + Operand2 + Carry • RSB r2, r5, #0xFF00
• SUB – Subtract • R2 = 0xFF00 − R5
• Rd := Rn − Operand2
• SBC – Subtract with Carry
• Rd := Rn − Operand2 − NOT(Carry)
• RSB – Reverse Subtract RSB and RSC subtract in reverse order (e.g. y - x not x - y).
• Rd := Operand2 − Rn
• RSC – Reverse Subtract with Carry
• Rd := Operand2 − Rn − NOT(Carry)
Logical Instructions
• AND – logical AND • AND r8, r7, r2
• Rd := Rn AND Operand2 • R8 = R7 & R2
• EOR – Exclusive OR • ORR r11, r11, #1
• Rd := Rn EOR Operand2 • R11 |= 1
• ORR – logical OR • BIC r11, r11, #1
• Rd := Rn OR Operand2 • R11 &= ~1
• BIC – Bitwise Clear • EOR r11, r11, #1
• Rd := Rn AND NOT Operand2 • R11 ^= 1
Compare Instructions:
<operation>{cond} Rn,Operand2
• <operation> • CMP r0, #42
• CMP – compare • Compare R0 to 42.
• Flags set to result of (Rn − Operand2). • CMN r2, #42
• CMN – compare negative • Compare R2 to -42.
• Flags set to result of (Rn + Operand2). • TST r11, #1
• TST – bitwise test • Test bit zero.
• Flags set to result of (Rn AND • TEQ r8, r9
Operand2). • Test R8 equals R9.
• TEQ – test equivalence • SUBS r1, r0, #42
• Flags set to result of (Rn EOR • Compare R0 to 42, with result.
Operand2).
•CMP is like SUB.
•CMN is like ADD – subtract of a negative number is the same as add.
•TST is like AND.
•TEQ is like EOR – exclusive or of identical numbers gives result of zero.
Directive: AREA
AREA myData, DATA, READWRITE ; Define a data section
Array DCD 1, 2, 3, 4, 5 ; Define an array with five integers

AREA myCode, CODE, READONLY ; Define a code section


EXPORT __main ; Make __main visible to the linker
ENTRY ; Mark the entrance to the entire program
__main PROC ; PROC marks the begin of a subroutine
... ; Assembly program starts here.
ENDP ; Mark the end of a subroutine
END ; 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 and areas within the same source file cannot share the
same name.
• An assembly program must have at least one code area.
• By default, a code area can only be read (READONLY) and a data area may be read from
and written to (READWRITE).
26
Directive: ENTRY
AREA myData, DATA, READWRITE ; Define a data section
Array DCD 1, 2, 3, 4, 5 ; Define an array with five integers

AREA myCode, CODE, READONLY ; Define a code section


EXPORT __main ; Make __main visible to the linker
ENTRY ; Mark the entrance to the entire program
__main PROC ; PROC marks the begin of a subroutine
... ; Assembly program starts here.
ENDP ; Mark the end of a subroutine
END ; Mark the end of a program

• The ENTRY directive marks the first instruction to be executed within an application
program.
• There must be exactly one ENTRY directive in an application, no matter how many source
files the application has.

27
Directive: END
AREA myData, DATA, READWRITE ; Define a data section
Array DCD 1, 2, 3, 4, 5 ; Define an array with five integers

AREA myCode, CODE, READONLY ; Define a code section


EXPORT __main ; Make __main visible to the linker
ENTRY ; Mark the entrance to the entire program
__main PROC ; PROC marks the begin of a subroutine
... ; Assembly program starts here.
ENDP ; Mark the end of a subroutine
END ; Mark the end of a program

• The END directive indicates the end of a source file.


• Each assembly program must end with this directive.

28
Directive: PROC and ENDP
AREA myData, DATA, READWRITE ; Define a data section
Array DCD 1, 2, 3, 4, 5 ; Define an array with five integers

AREA myCode, CODE, READONLY ; Define a code section


EXPORT __main ; Make __main visible to the linker
ENTRY ; Mark the entrance to the entire program
__main PROC ; PROC marks the begin of a subroutine
... ; Assembly program starts here.
ENDP ; Mark the end of a subroutine
END ; Mark the end of a program

• PROC and ENDP are to mark the start and end of a function (also called subroutine or
procedure).
• A single source file can contain multiple subroutines, with each of them defined by a pair
of PROC and ENDP.
• PROC and ENDP cannot be nested. We cannot define a function within another function.

29
Directive: EXPORT and IMPORT
AREA myData, DATA, READWRITE ; Define a data section
Array DCD 1, 2, 3, 4, 5 ; Define an array with five integers

AREA myCode, CODE, READONLY ; Define a code section


EXPORT __main ; Make __main visible to the linker
ENTRY ; Mark the entrance to the entire program
__main PROC ; PROC marks the begin of a subroutine
... ; Assembly program starts here.
ENDP ; Mark the end of a subroutine
END ; 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. The symbol must be defined in another file.
• The IMPORT is similar to the “extern” keyword in C.

30
Directive: Data Allocation
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
DCQ Define Constant Reserve 64-bit values
DCFS Define single-precision Reserve 32-bit values
floating-point numbers
DCFD Define double-precision Reserve 64-bit values
floating-point numbers
SPACE Defined Zeroed Bytes Reserve a number of zeroed bytes
FILL Defined Initialized Bytes Reserve and fill each byte with a value

31
Directive: Data Allocation
AREA myData, DATA, READWRITE
hello DCB "Hello World!",0 ; Allocate a string that is null-terminated

dollar DCB 2,10,0,200 ; Allocate integers ranging from -128 to 255

scores DCD 2,3.5,-0.8,4.0 ; Allocate 4 words containing decimal values

miles DCW 100,200,50,0 ; Allocate integers between –32768 and 65535

Pi DCFS 3.14 ; Allocate a single-precision floating number

Pi DCFD 3.14 ; Allocate a double-precision floating number

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 DCB 2_01010101 ; Allocate a byte in binary

octal DCB 8_73 ; Allocate a byte in octal

char DCB ‘A’ ; Allocate a byte initialized to ASCII of ‘A’ 32


Directive: EQU and RN
; Interrupt Number Definition (IRQn)
BusFault_IRQn EQU -11 ; Cortex-M3 Bus Fault Interrupt
SVCall_IRQn EQU -5 ; Cortex-M3 SV Call Interrupt
PendSV_IRQn EQU -2 ; Cortex-M3 Pend SV Interrupt
SysTick_IRQn EQU -1 ; Cortex-M3 System Tick Interrupt

Dividend RN 6 ; Defines dividend for register 6


Divisor RN 5 ; Defines divisor for register 5

• The EQU directive associates a symbolic name to a numeric constant. Similar to the
use of #define in a C program, the EQU can be used to define a constant in an
assembly code.
• The RN directive gives a symbolic name to a specific register.

33
Directive: ALIGN
AREA example, CODE, ALIGN = 3 ; Memory address begins at a multiple of 8
ADD r0, r1, r2 ; Instructions start at a multiple of 8

AREA myData, DATA, ALIGN = 2 ; Address starts at a multiple of four


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 next data misaligned
ALIGN ; Force the next data to be aligned
d DCD 12345 ; Skip three bytes and store the word

34
Directive: INCLUDE or GET
INCLUDE constants.s ; Load Constant Definitions
AREA main, CODE, READONLY
EXPORT __main
ENTRY
__main PROC
...
ENDP
END

• The INCLUDE or GET directive is to include an assembly source file within another
source file.
• It is useful to include constant symbols defined by using EQU and stored in a
separate source file.

35

You might also like