2441-LT3 ARM Assembly Instr 2023-24
2441-LT3 ARM Assembly Instr 2023-24
Today’s Quote
Chapter 3 1
3. Instructions of the ARM Assembly
Learning Outcomes:
Chapter 3 2
• The VisUAL2 ARM simulator is a cross-platform
and user-friendly educational tool that make it
very easy for beginners to write small and
medium-sized ARM assembler programs;
https://github.com/tomcl/V2releases
https://scc416.github.io/Visual2-doc/download
Chapter 3 3
3.1 Introduction of An Instruction Set
Chapter 3 4
types can be stored in memory as numbers,
leading to the stored-program computers;
o The ARM Assembly Language (or Instruction
Set) : the most popular 32-bit instruction set in the
world with 4 billion ARM processors shipped in
2008, 15 billion shipped in 2015, and upto 21.3
billion in 2017.
Chapter 3 5
3.2 Basic Operations of the ARM Assembly
Language
“There must certainly be instructions for performing the
fundamental arithmetic operations.”
ADD a, b, c
Chapter 3 6
o A high-level C, C++ or Java statement will always
be compiled into several ARM instructions.
Chapter 3 7
B 2500; unconditional branch to [PC] + 8 +
(2500 words*4)
Chapter 3 8
After this simple assembly program is executed,
r0 = $200 (i.e. address of the Num1 integer);
r3 = Num1 + Num2 = $64 (= 100 in decimal) + $c8 = $12c
r4 = r3 – r1 = $12c - $64 = $c8
Chapter 3 9
3.3 Operands of the ARM Assembly Language
1) Registers
o Registers are the most primitive and frequently used
operands in any assembly program;
o The size of a register in the ARM architecture is 32
bits, with groups of 32 bits (i.e. 4 bytes) forming a
computer word for an ARM processor;
o The various versions / generations of ARM
processors have different number of registers
(typically 16 to 32 registers on current computers).
For consistency and backward compatibility with
most ARM processors, we simply assume the limit of
16 registers in all the following discussion.
o Effective uses of all these 16 registers (named as r0,
r1,…, r15 ) is very critical to the overall program
performance !
§ An example of compiling a C assignment
statement to a ARM program using registers:
Chapter 3 10
The following ARM instructions would achieve the
same assignment task:
ADD r5, r0, r1; register r5 contains g+h
ADD r6, r2, r3; register r6 contains i+j
SUB r4, r5, r6; register r4=r5-r6 = (g+h) – (i+j)
2) Memory Operands
o Simple variables may contain single data elements (as
the Num1 in the above “simple.s” in Sec. 3.2) or more
complex data structures like arrays and structures;
o In either case, values are preloaded into specific
memory addresses that will be used as “memory
operands” in the relevant assembly language;
o ARM must include instructions to transfer data
between the memory and registers. Such instructions
are called data transfer instructions that will include
the memory addresses of any single data / array
element as its “memory operand”;
o The data transfer instructions in ARM to transfer a
data from the main memory to a register is called
“load” (LDR – load word into register) whereas the
instruction to transfer from a register to memory
address is called “store”) (STR – store word from a
register);
o As each computer word is 32-bit (i.e. 4 bytes), the
ARM processors address each sequentially stored
Chapter 3 11
computer word with a difference of 4 bytes in the
main memory as shown in the following diagram.
… …
12 100
8 10
4 100
0 1
Byte Content
Address (Data)
ARM Processor
Memory
Chapter 3 12
üLet’s consider another example with an array
named A with its base address already loaded into
r3, to load the value of A[8] from the main
memory into the register r5 of an ARM processor,
one may specify the following ARM instruction:
LDR r5, [r3,#32] ; Temp. reg. r5 gets A[8]
Chapter 3 13
operand address specified as [base register, offset].
For the following C assignment statement
A[12] = h + A[8];
assume r2 stores the value of h, and the base
register r3 holds the base address of A[0], the
above C statement can be compiled into the
following 3 ARM instructions
LDR r5, [r3,#32] ; temp. reg. r5 gets A[8]
ADD r5, r2, r5 ; r5 gets h+A[8]
STR r5, [r3,#48] ; store the result from r5
to A[12]
Chapter 3 14
3) Constant (or Immediate) Operands
o An ARM assembly program may use a constant in
various operations, e.g. to specify the offset part of a
targeted address as below.
LDR r5, [r1,#8] ; r1+8 is the address of a constant
4 to load into r5
ADD r3, r3, r5 ; r3 = r3 + 4
o To avoid using the LDR instruction in the above
example, an alternative way is to have one operand
of the arithmetic instruction (like ADD) be a
constant, called an immediate operand as below.
Chapter 3 15
Below are useful summaries of ARM operands
and instructions
a) ARM operands
Name Example Comments
16 registers r0,r1,r2,….,r11,r12, sp, lr, Fast storage
pc for data. In
ARM, data
must be in
registers to
perform
arithmetic.
232 (~ 4G) Memory[0],Memory[4],…, Accessed only by
memory Memory[4294967292] data transfer
instructions. ARM
addresses
uses byte addresses
(i.e. each address
holds 1 byte), so
sequential word
addresses differ by
4. Memory holds
data structures,
arrays, and spilled
registers.
Chapter 3 16
b) ARM instructions
Category Instruction Example Meaning Comments
add ADD r1, r2, r3 r1 = r2 + r3 3 register
Arithmetic operands
register
memory to
register
load register byte LDRB r1, [r2, #20] r1 = Memory[r2+20] Byte from
memory to
register
Chapter 3 17
load register byte LDRBS r1, [r2, r1 = Memory[r2+20] Byte from
store register byte STRB r1, [r2, #20] Memory[r2+20]=r1 Byte from
register to
memory
operands;
bit-by-bit OR
logical shift left LSL r1, r2, #10 r1 = r2 << 10 Shift left by
(optional operation) constant
logical shift right LSR r1, r2, #10 r1 = r2 >> 10 Shift right by
(optional operation) constant
Chapter 3 18
Conditional compare CMP r1, r2 cond. flag = r1 – r2 Compare for
Branch conditional
branch
MI, PL
Chapter 3 19
3.4 Signed and Unsigned Numbers
Chapter 3 20
1111 1111 1111 1111 1111 1111 1111 1111 =
4,294,967,29510
Chapter 3 22
§ An example: The following 32-bit signed integer
(represented in the 2’s complement format) was
loaded from a specific memory location to the
register r2 of an ARM processor. What’s the
decimal value of this 32-bit signed integer?
Chapter 3 23
branch, ADD instructions contain a 2’s
complement 16-bit value, ranging from
-32,768 (-2 15) to 32,767 (+2 15-1). To add this
immediate value to a 32-bit register, the ARM
processor must convert this 16-bit 2’s complement
integer into its 32-bit equivalent as follows.
§ An example: Assume the 16-bit signed integer is
0000 0000 0000 11002 = 1210
Chapter 3 24
ü Overflow : the ALU of any computer, including the
ARM architecture, is designed to perform binary
arithmetic like add, subtract, multiply, and divide
on signed/unsigned integers. When the result of
such operation cannot be represented by all the
H/W bits (say the 32 bits of an ARM register),
overflow occurs. In general, it’s up to the
programming language, the O.S., and the program
to decide what to do if overflow occurs;
ü Specifically for the 2’s complement scheme, when
overflow occurs, the sign bit is incorrect. That is :
sign bit = 0 when the signed integer is actually –ve
or sign bit = 1 when the signed integer is +ve.
Chapter 3 25
ü The following table shows the C and Python
logical operators with their corresponding ARM
instructions:
Logical C Python ARM
operations operators operators instructions
Bit-by-bit & & AND
AND
Bit-by-bit | | ORR
OR
Bit-by-bit ~ ~ MVN
NOT
Shift left << << LSL
Shift right >> >> LSR
§ For example, if r1 contains
1111 0000 1111 0000 1111 0000 1111 01102
and r2 contains
0000 1111 0000 1111 0000 1111 0000 11102
Chapter 3 26
Yet for the following ARM instruction
ORR r5, r1, r2 ; reg. r5 = r1 | r2
Chapter 3 27
ü In ARM, shift operations are NOT separate
instructions. Typically, they are bundled with
other microprocessor instruction set. That is ARM
offers the ability to shift the 2nd operand as part of
any data processing (DP) instruction!
Chapter 3 28
Summary:
¨ The ARM Assembly Language (or Instruction Set) is the most
popular 32-bit instruction set with its simplicity & efficiency as
based on the stored-program concept.
Chapter 3 29
the “constant” offset as in “LDR r0, [r1, #8]” for a fixed offset
of 8 bytes from the base address stored in r1.
Chapter 3 30