100% found this document useful (1 vote)
271 views

CHAPTER 3 - 1 - Ver2-Intro To Assembly Language PDF

The document provides an overview of assembly language programming for the ARM processor. It discusses the basic types of ARM instructions including data transfer, addressing modes, data processing, and control transfer instructions. It also covers ARM assembler encoding, starting to code assembly language programs, commonly used assembler directives like AREA, ENTRY, END, PROC and ENDP, and memory access in ARM. The document is intended to teach basic ARM assembly language programming concepts over several weeks.

Uploaded by

Weehao Siow
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
100% found this document useful (1 vote)
271 views

CHAPTER 3 - 1 - Ver2-Intro To Assembly Language PDF

The document provides an overview of assembly language programming for the ARM processor. It discusses the basic types of ARM instructions including data transfer, addressing modes, data processing, and control transfer instructions. It also covers ARM assembler encoding, starting to code assembly language programs, commonly used assembler directives like AREA, ENTRY, END, PROC and ENDP, and memory access in ARM. The document is intended to teach basic ARM assembly language programming concepts over several weeks.

Uploaded by

Weehao Siow
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/ 34

CHAPTER 3.

1: ARM
ASSEMBLY LANGUAGE
PROGRAMMING
BENM 2123: MICROPROCESSOR
TECHNOLOGY

INTRODUCTION TO ASSEMBLY LANGUAGE


Basic Types of ARM Instructions
Introduction to Assembly Language (chp3.1) – Week 3
• ARM instruction format
• Assembler directives

Data Transfer Instructions: Processor interacts with memory (chp3.2) – Week 3


• load a word from memory into a register – LDR, LDRB, LDRH, LDRSB, LDRSH
• store the contents of a register into a memory word – STR, STRB, STRH

Addressing Mode: 10 addressing mode (chp3.3) – Week 4


• Base register addressing (1)
• Base displacement addressing mode – offset (3), pre-indexed (3), post-indexed (3)

Data Processing Instructions (chp3.4) – Week 4


• Condition code bits - N, Z, C, V
• Comparisons – CMP, CMN, TST, TEQ
• Addition/Subtraction– ADD, ADC, SUB, SBC, RSB, RSC
• Multiplication – MUL, MLA
• Logical/Boolean operations – AND, EOR, ORR, BIC
• Data movement between registers – MOV, MVN
• Shifts and Rotates – LSL, ASL, LSR, ASR, ROR, RRX

Control Transfer Instructions: Loop and Branches (chp3.5) – Week 5


• Branching, Conditional branching
• Conditional structures – if, if-else, switch-case
• Looping – while loops, for loops, do..while loops
• Conditional execution

Discussion on compilation of exam questions – Week 6 & 7


Content Outline
• Assembly Program
• ARM Assembler Encoding
• Starting to code
• Assembler Directives
• Example of assembly language
The Assembler Program
• The assembler program translates a user program, or
“source” program written with mnemonics into a machine
language code or “object” program, which the processor
can execute.
source program  assembler  object program

• Assemblers have their own rules that includes:


• Spaces
• Commas In appropriate places, correct
spelling, correct placement of
• Semicolons
names and numbers.
• colons
ARM Assembler Encoding
• How do we get the assembly language into the computer? We have
to translate it, either into hexadecimal or binary numbers.
• It is possible to translate an assembly language program by hand,
instruction by instruction but it is not practical.
• Assembler will translate the assembly language  machine code.
• Machine code produced by the assembler is stored in memory and
executed by the processor
Instruction Mnemonic Register Memory location Hexadecimal Equivalent
LDR r1, num1 E59F1010
LDR r0, num2 E59F0008
ADD r5, r1, r0 E0815000
STR r5, num3 E58F5008
Source program/assembly code Object code
Assembler
ARM Assembler Encoding (cont.)
ARM Assembler Encoding (cont.)
• Each instruction is encoded into a 32-bit word
• Access to memory is provided only by Load and Store
instructions
• The basic encoding format for the instructions, such as
Load, Store, Move, Arithmetic, and Logic instructions, is
shown below

• An instruction specifies a conditional execution code


(Condition), the OP code, two or three registers (Rn, Rd
and Rm), and some other information.
ARM Assembler Encoding (cont.)
What is opcode?

Operand

• An opcode is short for 'Operation Code'.


• An opcode is a single instruction that can be executed by
the CPU.
• In machine language it is a binary or hexadecimal value
such as 'B6' loaded into the instruction register.
• assembly language mnemonic form an opcode is a
command such as MOV or ADD or JMP ...
What is operand?
• In computer programming, an operand is a term used to describe any object that is
capable of being manipulated. For example, in "1 + 2" the "1" and "2" are the operands
and the plus symbol is the operator.
operands

opcode

Destination
register
1st operand
2nd operand (flexible 2nd operand, can be
either register or constant. In the
QuickReferenceCard, <Operand2> is used.
Example:
ADD r1, r2, r3 ; 2nd source operand = register
ADD r1, r2, #5 ; 2nd source operand = constant
Operand size: 32 bits (all arithmetic/logical instructions)
Operand type: signed or unsigned integer
Starting to code
• As a beginner, you need to know several aspects:
• The structure of a program and directives
• Which are the instructions to the assembler for creating areas of codes
• Aligning data
• Marking the end of your code

• When preparing a program, the registers should be written in lower case such as
r0, r1, r2, etc.

• Uppercase forms such as Rm and Rn indicate that the programmer must select
any of the 16 registers. (normally used in QuickReferenceCard)
• Example: ADD Rd, Rn, Rm
• Rd – destination register
• Rn – 1st operand register for arithmetic operation
• Rm – 2nd operand register for arithmetic operation

• For ARM registers, r0-r12 should be used to hold general purpose program
values. Other registers should be reserved for special functions.
• r15 – is for Program Counter (PC)
• r14 (LR) – is used as the subroutine return address link register.
• r13 – is for Stack Pointer (SP)
Assembly Language Format (cont.)
• Standard assembly language format:

Comment, to make reading


and understanding it easier

• Semicolon indicates everything to the right of it is a comment and should be ignored


by the assembler.
• The second example simply takes the values in two register (r0 and r1), adds them
together, and places the result in a third register (r4).
• Remember to write the operands in the correct order, which is result register first, then
the first operand and lastly the second operand.
Memory access
• Memory in ARM is byte accessible.
• However, we can assume that memory can be accessed in 2 other
ways:
Byte access Half-word access Word access
Address Memory Address Memory Address Memory
0×30 0×06 0×30 0×FC06 0×30 0×FF03FC06
0×31 0×FC 0×32 0×FF03 0×34 0×GHCDABEF
0×32 0×03 0×34 0×ABEF
0×33 0×FF 0×36 0×GHCD
0×34 0×EF
0×35 0×AB
0×36 0×CD
0×37 0×GH
Assembler Directives
• Assembler directives are instructions that direct the assembler to
do something.

• Directive instructions assign the program to


• Designate certain areas in memory to place the program and data blocks
• define constant symbols and numerical values to address labels
• place tables or other fixed data in memory
• allow references to other programs
• Specify the end of the source program text

• The most common directives are:


• AREA - uses the argument CODE or DATA, indicates the beginning of a block
of memory that contains either program instructions or data.
• ENTRY - specifies that program execution is to begin at the following LDR
instruction.
• DEFINE CONSTANT (Data) – DCD/DCB are used to label and initialize the
data operands
• EQUATE (Define) – EQU to equate names with address or data
• DEFINE STORAGE (Reserve)
Assembler Directives (cont.)
AREA
• Allows the programmer to specify the memory locations where
programs, subroutines, or data will reside.
• An area directive causes the assembler to place a new value in
the location counter, just like a Jump instruction that causes the
CPU to place a new value in the program counter.
• The output from the assembler must not only contain
instructions and data, but must also indicate to the loader
program where the instructions and data should be placed in
memory.
• Microprocessor programs often contain several AREA
statements for the following purposes:
- Reset (startup) address - Stack
- Interrupt service addresses - Main program
- Trap (software interrupt) addresses - Subroutines
- RAM storage - Input/Output
Assembler Directives (cont.)
Define an area for
AREA data and code

• 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 and a data area may be read from and
written to.
Assembler Directives (cont.)
ENTRY

• The ENTRY directive marks the first instruction to be executed within an


application.
• There must be one and only one entry directive in an application, no matter
how many source files the application has.
Assembler Directives (cont.)
END

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


• Each assembly program must end with this directive.
Assembler Directives (cont.)
PROC and ENDP

• 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
subroutine within another subroutine.
Assembler Directives (cont.)
EXPORT and IMPORT

• 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 (a variable that is defined in other file).
• The IMPORT is similar to the “extern” keyword in C.
Assembler Directives (cont.)
Directive: Data Allocation
DEFINE CONSTANT (Data) – DCD/DCB are used to label and initialize the
data operands
Assembler Directives (cont.)
• Directive: Data Allocation
Assembler Directives (cont.)
Example: Defining memory areas using DCB
• There are some easy ways to set up tables or constants in your program.
• E.g: if a table of coefficients is needed, and each coefficient is represented in 8
bits, then you might declare an area of memory as

table DCB 0×FE, 0×F9, 0×12, 0×34


DCB 0×11, 0×22, 0×33, 0×44

• Assuming that the table was started in memory at address 0×4000, the memory
will look like
Address Memory
0×4000 0×FE
0×4001 0×F9
0×4002 0×12
0×4003 0×34
0×4004 0×11
0×4005 0×22
0×4006 0×33
0×4007 0×44
Assembler Directives (cont.)
EQU
• The EQU directive allows the programmer to equate
names with addresses or data.
• The names may refer to device addresses, numeric data,
starting addresses, fixed addresses, etc.
• The EQU directive assigns the numeric value in its
operand field to the label in its label field.
• Note that an EQU directive does not cause the assembler
to place anything in memory.
• Examples:
TTY EQU 5
LAST EQU 5000
Assembler Directives (cont.)
EQU and RN

• 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.
Assembler Directives (cont.)
ALIGN

• To ensure that the area is memory aligned


Assembler Directives (cont.)
INCLUDE or GET

• 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.
Example 1
Example of assembly language
AREA MYCODE, CODE, READONLY ; code area (segment)
Assembler directives ENTRY ; start of area MYCODE

EXPORT SystemInit ; export SystemInit symbol


SystemInit ; SystemInit routine

EXPORT __main ; export __main symbol


__main ; __main routine

Code area LDR


LDR
r1, N
r2, POINTER
MOV r0, #0
LOOP LDR r3, [r2], #4
ADD r0, r0, r3 In exam, this is
SUBS r1, r1, #1 the only code
BGT LOOP
STR r0, SUM you need to
SUM DCD 0
write
Assembler directives N DCD 5
POINTER DCD NUM1
- Data area NUM1 DCD 3, -17, 27, -12, 322

ALIGN ; ensure the area is memory aligned


END ; end of assembly file

Ensure that SystemInit and __main starts at the first column of the line (else they would cause an error)
Example 2
Declare status: readwrite or readonly
AREA MYDATA, DATA, READWRITE ; data area (segment)
ENTRY

arr1 SPACE 3*2 ; 3-element halfword array

AREA MYCODE, CODE, READONLY ; code area (segment)


EXPORT SystemInit ; export SystemInit symbol
SystemInit ; SystemInit routine

EXPORT __main ; export __main symbol


__main You can use any name ; __main routine
LDR R1, =arr1 ; put address of arr1 into R1
MOV R2, #0xEFGH
MOVT R2, #0xABCD ; assign 0xABCDEFGH to R2
STRH R2, [R1], #2 ;store arr1[0], arr1[1] and arr1[2] with 0xABCDEFGH
STRH R2, [R1], #2
STRH R2, [R1]

ALIGN ; ensure the area is memory aligned


END ; end of assembly file

• There are two AREAs declared, one for storing the data and another for the code.
• The SPACE assembler directive allocates 6 bytes (3 * 2 bytes) for the symbol arr1. Since arr1 has been
declared, we can use the symbol instead of keeping track of its location in data memory.
• The assembler translates arr1 to an actual value upon assembly.
Example 3
AREA MYCODE, CODE, READONLY ; code area (segment)
ENTRY ; start of area MYCODE

EXPORT SystemInit ; export SystemInit symbol


SystemInit ; SystemInit routine

EXPORT __main ; export __main symbol


__main ; __main routine
MOV R2, #0xC020 ; move 0xC020 into R2
MOVT R2, #0x2009 ; assign 0x2009C020 to R2

MOV R4, #0x0 ; place 0x0 into R4


MOVT R4, #0x00B4 ; place 0x00B40000 into R4
STR R4, [R2] ; set 0x2009C020 to 0x00B4000
MOV R2, #0xC038 ; move 0xC038 into R2
MOVT R2, #0x2009 ; assign 0x2009C038 to R2

MOV R4, #0x0 ; zero R4


MOVT R4, #0x00040000 ; assign 0x00040000 to R4
STR R4, [R2] ; write 0x00040000 to FIO1SET

ALIGN ; ensure the area is memory aligned


END ; end of assembly file
In this example, no data is assigned
Example 4
Assembler Directives (cont.)
Other directives

Assembler derivatives Description


DEFW num Reserves a word of store and puts the initial value “num” in it.

DEFB num Reserves a byte of store and puts the initial value “num” in it.
Example: DEFB ‘w’, ‘o’, ‘r’, ‘l’, ‘d’, 0
DEFS size, fill Reserves a block of store of “size” bytes all initialized to “fill” (fill can be omitted –
values undefined)
ALIGN Leave any blank bytes needed so next thing starts on a word boundary.

ORIGIN addr Put the following code (or data) starting at address “addr” (default 0)

ENTRY This is the starting point of the program (initial PC – default 0)

AREA Chunks of data ($data) or code ($code)

ADR Load address into a register.


Example: ADR R0, BUFFER
Assembler Directives (cont.)
Other directives
Assembler derivatives Description
ALIGN Adjust location counter to word boundary usually after a storage directive

END No more to assemble

DCD Defined word value storage area. It instructs the assembler to reserve a
word of store and to initialize it to the value of the expression to the right.
Example: BOW DCD 1024, 2055, 9051
DCB Defined byte value storage area
Example: BOB DCB 10, 12, 15
% Zeroed out byte storage area.
Example: BLBYTE %30
IMPORT Name of routine to import for use in this routine
Example: IMPORT _printf; C print routine
EXPORT Name of routine to export for use in other routines
Example: add2 routine
EQU Symbol replacement. An EQU directive can be used to define symbolic
names for constants.
Example:
Loopcnt EQU 7
TEN EQU 10
Exercise
• Assuming that the table was started in memory at address
0×4000 and using little-endian format, fill in the memory
locations according to the declarations below:
table DCD 0×AABBCCDD
DCD 0×11223344

Memory
0×4000
0×4001
0×4002
0×4003
0×4004
0×4005
0×4006
0×4007

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy