CHAPTER 3 - 1 - Ver2-Intro To Assembly Language PDF
CHAPTER 3 - 1 - Ver2-Intro To Assembly Language PDF
1: ARM
ASSEMBLY LANGUAGE
PROGRAMMING
BENM 2123: MICROPROCESSOR
TECHNOLOGY
Operand
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:
• 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
• 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
• 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
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
• 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
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)
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