module2
module2
MODULE 2
Data Processing Instructions
The data processing instructions manipulate data within registers. They are move
instructions, arithmetic instructions, logical instructions, compare instructions and
multiply instructions.
Most data processing instructions can process one of their operands using the barrel
shifter.
If S is suffixed on a data processing instruction, then it updates the flags in the cpsr.
MOVE INSTRUCTIONS:
It copies N into a destination register Rd, where N is a register or immediate value. This
instruction is useful for setting initial values and transferring data between registers.
In the example shown below, the MOV instruction takes the contents of register r5 and
copies them into register r7.
ARITHMETIC INSTRUCTIONS:
The arithmetic instructions implement addition and subtraction of 32-bit signed and
unsigned values.
Syntax: <instruction>{<cond>} {S} Rd, Rn, N
In the following example, subtract instruction subtracts a value stored in register r2 from
a value stored in the register r1. The result is stored in register r0.
In the following example, the reverse subtract instruction (RSB) subtract r1 from the
constant value #0, writing the result in r0.
LOGICAL INSTRUCTIONS:
Logical instructions perform bitwise operations on the two source registers.
Syntax: <instruction> {<cond>} {S} Rd, Rn, N
In the example shown below, a logical OR operation between registers r1 and r2 and the
result is in r0.
COMPARISON INSTRUCTIONS:
The comparison instructions are used to compare or test a register with a 32-bit value.
They update the cpsr flag bits according to the result, but do not affect other registers.
After the bits have been set, the information can be used to change program flow by
using conditional execution.
Syntax: <instruction> {<cond>} Rn, N
Example shown below for CMP instruction, both r0 and r1 are equal before the execution
of the instruction. The value of the z flag prior to the execution is 0 and after the
execution z flag changes to 1 (upper case of Z).
MULTIPLY INSTRUCTIONS:
The multiply instructions multiply the contents of a pair of registers and depending upon
the instruction, accumulate the results in another register.
The long multiplies accumulate onto a pair of registers representing a 64-bit value.
Syntax: MLA {<cond>} {S} Rd, Rm, Rs, Rn
MUL {<cond>} {S} Rd, Rm, Rs
In the following example below shows a multiply instruction that multiplies registers r1
and r2 and places the result into the register r0.
The long multiply instructions (SMLAL, SMULL, UMLAL, and UMULL) produce a 64-
bit result.
BRANCH INSTRUCTIONS
Q2. Explain briefly branch instructions of ARM processor.
Answer:
A branch instruction changes the flow of execution or is used to call a routine.
This type of instruction allows programs to have subroutines, if-then-else structures, and
loops.
The change of execution flow forces the program counter (pc) to point to a new address.
The branch with link (BL) instruction changes the execution flow in addition
overwrites the link register lr with a return address. The example shows below a
fragment of code that branches to a subroutine using the BL instruction.
The branch exchange (BX) instruction uses an absolute address stored in register Rm.
It is primarily used to branch to and from Thumb code. The T bit in the cpsr is updated
by the least significant bit of the branch register.
Similarly, branch exchange with link (BLX) instruction updates the T bit of the cpsr
with the least significant bit and additionally sets the link register with the return
address.
When the processor executes an SWI instruction, it sets the program counter pc to the
offset 0xB in the vector table.
The instruction also forces the processor mode to SVC, which allows an operating system
routine to be called in a privileged mode.
Each SWI instruction has an associated SWI number, which is used to represent a
particular function call or feature.
The example below shows an SWI call with SWI number 0x123456, used by ARM
toolkits as a debugging SWI.
Since SWI instructions are used to call operating system routines, it is required some
form of parameter passing.
This achieved by using registers. In the above example, register r0 is used to pass
parameter 0x12. The return values are also passed back via register.
Coprocessor Instructions
Q5. Explain briefly coprocessor instructions.
Answer:
Coprocessor instructions are used to extend the instruction set.
A coprocessor can either provide additional computation capability or be used to control
the memory subsystem including caches and memory management.
These instructions are used only by core with a coprocessor.
In the syntax of the coprocessor instructions, the cp field represents the number between
p0 and p15. The opcode fields describe the operation to take place on the coprocessor.
The Cn, Cm and Cd fields describe registers within the coprocessor.
For example: The instruction below copies coprocessor CP15 register c0 into a general
purpose register r10.
MRC p15, 0, r10, c0, c0, 0 ; CP15 register-0 is copied into general purpose
register r10.
For example: The instruction below moves the contents of CP15 control register c1 into
register r1 of the processor core.
MRC p15, 0, r1, c1, c0, 0
Loading Constants
Q6. Explain briefly the loading constants.
Answer:
You might have noticed that there is no ARM instruction to move a 32-bit constant into a
register. Since ARM instructions are 32 bits in size, they obviously cannot specify a
general 32-bit constant. To aid programming there are two pseudo instructions to move a
32-bit value into a register.
There are two pseudo instructions to move a 32-bit constant value to a register.
Syntax: LDR Rd, =constant
ADR Rd, label
The example below shows an LDR instruction loading a 32-bit constant 0xff00ffff into
register r0.
LDR r0, =0xff00ffff
The first pseudo instruction writes a 32-bit constant to a register using whatever
instructions are available. It defaults to a memory read if the constant cannot be encoded
using other instructions. The second pseudo instruction writes a relative address into a
register, which will be encoded using a pc-relative expression.