Professional Documents
Culture Documents
PPT-2 - Data Processing Instructions
PPT-2 - Data Processing Instructions
MICROPROCESSORS
Dr. Anu Shaju Areeckal,
Asst. Professor, Dept of E&C, MIT Manipal
This ppt is for reference only. Use the referred textbooks as the main study material.
ARM Data Processing
Instructions
• Here, the addends are positive in two’s complement notation, but the sum is negative
• Since MSB of result is set, this causes N bit to be set
• Value of the N bit does not matter for unsigned operation
FLAGS AND THEIR USE
• V Flag
• indicates a signed overflow
• Overflow occurs if the result of an add, subtract, or compare is greater than or equal to
231, or less than –231.
• V flag status determined as an EX-OR of the carry bit going into the MSB of the result
with the carry bit coming out of the MSB
• Example 1: 0xA1234567 + 0xB0000000
• Two signed values (in 2’s complement)
are added to produce the sum, which
does not fit into 32 bits.
• Example 2: 0x7B000000 + 0x30000000 • The result fits into 32 bits. But, the result
would be interpreted as a negative
number.
• Both N and V bits would be set in CPSR.
FLAGS AND THEIR USE
• Z Flag
• Set i.e. 1 when the result of an operation produces zero, meaning all 32 bits must be
zero.
• This might be the result of a counter expiring, or a routine might need to examine an
operand before performing some other kind of arithmetic routine, such as division.
The first subtraction operation will set the C flag, since result is positive.
The second subtraction is a subtract with carry (SBC) operation, using the carry bit to
perform a normal subtraction.
If the carry bit had been clear, the SBC instruction would have subtracted one more
from its result.
Data Processing
Instructions
ARM Instruction Set
Instruction classes
1. Data processing instructions
• Group does most of the work
• Sixteen instructions, and they have very similar formats
• Examples - ADD and CMP, which add and compare two numbers respectively
• Operands of these instructions are always in registers (or an immediate
number stored in the instruction itself), never in memory
2. Data transfer instructions
• This is a smaller group of two instructions: load a register and save a register.
Variations include whether bytes or words are transferred, and how the
memory location to be used is obtained.
3. Branching
• Although the PC may be altered using the data operations to cause a change
in the program counter, the branch instruction provides a convenient way of
reaching any part of the 64M byte address space in a single instruction. It
causes a displacement to be added to the current value of the PC. The
displacement is stored in the instruction itself.
DATA PROCESSING INSTRUCTIONS
An ARM instruction is 32 bits long, so there are around 4 billion different binary
machine instructions
Hexadecimal numbers are represented with prefix 0x
Binary numbers with the prefix 0b.
The examples follow the format shown below:
PRE <pre-conditions>
<instruction/s>
POST <post-condition>
In the pre- and post-conditions, memory is denoted as
Mem<data_size>[address]
Example: mem32[1024] is 32-bit value starting at address 1024.
ARM instructions process data held in registers and only access memory with load and
store instructions.
ARM instructions commonly take two or three operands.
DATA PROCESSING INSTRUCTIONS
The data processing instructions manipulate data within registers.
• Data movement instructions
• MOV, MVN
• <opcode>{cond}{S} Rd, <Op2>
• single operand instructions
• Arithmetic instructions
• ADD, ADC, SUB, SBC, RSB, RSC
• <opcode>{cond}{S} Rd, Rn, <Op2>
• Logical instructions
• AND, ORR, EOR, BIC
• <opcode>{cond}{S} Rd, Rn, <Op2>
• Comparison instructions
• CMP, CMN, TST, TEQ
• <opcode>{cond} Rn, <Op2>
• instructions which do not produce a result; only updates flags
• Multiply instructions
• MUL, MLA
DATA PROCESSING INSTRUCTIONS
Most data processing instructions can process one of their operands using the barrel
shifter.
If the S suffix in a data processing instruction is used then it updates the flags in the
CPSR.
Move and logical operations update the carry flag C, negative flag N and zero flag Z.
carry flag = 1, when the result of the barrel shift as the last bit shifted out.
N flag = 1, when the bit 31 of the result is 1.
Z flag is set if the result is zero.
25
Data Processing Instructions: Barrel Shifter
• Barrel shifter operation syntax for data processing instructions
Example
ARITHMETIC
INSTRUCTIONS
Data Processing Instructions: Arithmetic Instructions
The arithmetic instructions perform addition and subtraction of 32-bit signed and unsigned values.
N is the result of the shifter operation. The syntax of shifter operation is shown previously.
Data Processing Instructions:
Arithmetic Instructions
Examples:
• ADD r1, r2, r3 ; r1 = r2 + r3
• ADC r1, r2, r3 ; r1 = r2 + r3 + C
• SUB r1, r2, r3 ; r1 = r2 - r3
• SUBC r1, r2, r3 ; r1 = r2 - r3 + C - 1
• RSB r1, r2, r3 ; r1 = r3 - r2
• RSC r1, r2, r3 ; r1 = r3 - r2 + C - 1
Data Processing Instructions: Arithmetic Instructions
Example - subtraction
PRE r0 = 0x00000000
r1 = 0x00000002
r2 = 0x00000001
SUB r0, r1, r2
POST r0 = 0x00000001
PRE r0 = 0x00000000
r1 = 0x00000005
ADD r0, r1, r1, LSL #1
POST r0 = 0x0000000f
r1 = 0x00000005
Data Processing Instructions: Arithmetic Instructions
The second operand can make use of the barrel shifter when performing addition and
subtraction, such as:
38
To subtract two 64-bit numbers:
0x7000BEEFC0000000
− 0x3000BABE80000000
Ans = 0x4000043140000000
The first subtraction operation will set the C flag, since result is positive.
The second subtraction is a subtract with carry (SBC) operation, using the carry bit to
perform a normal subtraction.
If the carry bit had been clear, the SBC instruction would have subtracted one more
from its result.
Data Processing Instructions: Arithmetic Instructions
Example: Write an ARM7TDMI assembly program to perform
the function of absolute value. Register r0 will contain the
initial value, and r1 will contain the absolute value.
• Solution:
AREA Prog7a, CODE, READONLY
ENTRY
MOVS r1, r0
RSBLT r1, r1, #0
• done B done
END
LOGICAL INSTRUCTIONS
Data Processing Instructions: Logical Instructions
Logical instructions perform bitwise logical operations on the two source registers.
Syntax: <instruction>{<cond>}{S} Rd, Rn, N
Example - This example shows a logical OR operation between registers r1 and r2. r0 holds the
result.
PRE r0 = 0x00000000
r1 = 0x02040608
r2 = 0x10305070
ORR r0, r1, r2
POST r0 = 0x12345678
Data Processing Instructions: Logical Instructions
Example - Logical instruction BIC carries out a logical bit clear.
PRE r1 = 0b1111, r2 = 0b0101 ;(0b indicates binary number)
BIC r0, r1, r2
POST r0 = 0b1010
In this example, register r2 contains a binary pattern where every binary 1 in r2 clears a
corresponding bit location in register r1.
This instruction is particularly useful when clearing status bits and is frequently used to
change interrupt masks in the cpsr.
The logical instructions update the cpsr flags only if the S suffix is present.
These instructions can use barrel-shifted second operands in the same way as the
arithmetic instructions.
This is equivalent to Rd = Rn AND NOT(N).
COMPARISON
INSTRUCTIONS
Data Processing Instructions: Comparison instructions
• There are four instructions that do nothing except set the condition codes or
test for a particular bit in a register: CMP, CMN, TST, TEQ
• CMP—Compare
CMP subtracts a register or an immediate value from a register value and updates the
condition codes.
You can use CMP to quickly check the contents of a register for a particular value, such
as at the beginning or end of a loop.
• CMN—Compare negative
CMN adds a register or an immediate value to another register and updates the
condition codes.
This instruction is actually the inverse of CMP, and the assembler will replace a CMP
instruction when appropriate. For example, if you typed
• CMP r0, #-20
• the assembler will instead generate
• CMN r0, #0x14
Data Processing Instructions: Comparison instructions
• Example:
• Another example:
CMP r0,r1
SUBGT r2,r0,r1
SUBLT r2,r1,r0 ;or RSBLT r2,r0,r1
Data Processing Instructions: Comparison instructions
• TST—Test
TST logically ANDs an arithmetic value with a register value and updates
the condition codes without affecting the V flag.
TST can be used to determine if many bits of a register are all clear or if
at least one bit of a register is set.
• TEQ—Test equivalence
TEQ logically XORs an arithmetic value with a register value and updates
the condition codes without affecting the V flag.
TEQ can be used to determine if two values are the same.
• The syntax for these instructions is
• instruction{<cond>} <Rn>, <operand2>
• where {<cond>} is one of the optional conditions, and
<operand2> can be a register with an optional shift, or an
immediate value.
Data Processing Instructions: Comparison instructions
• Example:
MULTIPLY INSTRUCTIONS
Data Processing Instructions: Multiply
MUL multiplies the values in two registers, truncates the result
to 32 bits, and stores the product in a third register.
• MUL r4, r2, r1 ; r4 = r2 * r1
• MULS r4, r2, r1 ; r4 = r2 * r1, then set the flags
MLA multiplies two registers, truncates the results to 32 bits,
adds the value of a third register to the product, and stores the
result in a fourth register
• MLA r7, r8, r9, r3 ; r7 = r8 * r9 + r3
Both MUL and MLA can optionally set the N and Z condition
code flags.
For multiplications that produce only 32 bits of result, there is
no distinction between signed and unsigned multiplication. Only
the least significant 32 bits of the result are stored in the
destination register, and the sign of the operands does not affect
this value.
Data Processing Instructions: Multiply
Multiply long instructions produce 64-bit results. They multiply the
values of two registers and store the 64-bit result in a third and fourth
register.
SMULL and UMULL are signed and unsigned multiply long
instructions:
• SMULL r4, r8, r2, r3 ; r4 = bits 31-0 of (r2*r3)
; r8 = bits 63-32 of (r2*r3)
• UMULL r6, r8, r0, r1 ; {r8,r6} = r0*r1
The instructions below multiply the values of two registers, add the
64-bit value from a 3rd and 4th register, and store the 64-bit result in
the 3rd and 4th registers:
• SMLAL r4, r8, r2, r3 ; {r8,r4} = r2*r3 + {r8,r4}
• UMLAL r5, r8, r0, r1 ; {r8,r5} = r0*r1 + {r8,r5}
All four multiply long instructions can optionally set N and Z condition
flags.
Data Processing Instructions: Multiply
The inline barrel shifter can be used in conjunction with other
instructions, such as ADD or SUB, in effect getting a multiplication.
This feature is used to its full advantage when certain multiplications
are done using the barrel shifter instead of the multiplier array.
Consider the case of multiplying a number by a power of two. This
can be written using only an LSL instruction, i.e.,
• LSL r0, r1, #2 ; r0 = r1*4
But what if we wanted to multiply two numbers, one of which is not a
power of two, like 5? Examine the following instruction:
• ADD r0, r1, r1, LSL #2 ; r0 = r1 + r1*4 = r1*5
By using only the 32-bit adder and a barrel shifter,
𝑛 𝑛
the ARM 𝑛
processors can generate multiplications by 2 , 2 − 1 and 2 + 1 in a
single cycle, without having to use a multiplier array. This saves some
execution time.
Data Processing Instructions: Multiply
• Example for multiplication by 2𝑛 − 1 :
• RSB r0, r2, r2, LSL #3 ; r0 = r2*8 - r2 = r2*7
• will perform a multiplication by 7 by taking register r2, shifting it left by 3
bits, yielding a multiplication by 8, and then subtracting register r2 from the
product.
Note that the reverse subtract instruction was used here, since an
ordinary subtraction will produce the wrong result.
By chaining together multiplications, for example, multiplying by 5
and then by 7, larger constants can be created.
Examine the following code to see that you can create multiplier
arguments that are not powers of two, say 115:
• ADD r0, r1, r1, LSL #1 ; r0 = r1*3
• SUB r0, r0, r1, LSL #4 ; r0 = (r1*3) − (r1*16) = r1* (− 13)
• ADD r0, r0, r1, LSL #7 ; r0 = (r1* (− 13)) + (r1*128) = r1*115
Homework Exercise:
Read and understand the example on
Hamming codes (given as Example 7.7 in
Reference book by William Hohl)
55
Data processing
instruction- Bit format
Data processing instruction format
31 28 27 26 25 24 21 20 19 16 15 12 11 0
destination register
first operand register
25 11 8 7 0
immediate alignment
11 7 6 5 4 3 0
#shift Sh 0 Rm
Rs 0 Sh 1 Rm
58
Data processing instruction format
Bits [27-26] – 00
Bit 25- Immediate bit
Bits [24 – 21] - Opcode bits, specifies the type of the operation
31 28 2726 25 24 21 20 19 1615 12 11 0
cond 0 0 # opcode S Rn Rd operand 2
destination register
first operand register
set condition codes
arithmetic/logic function
25 11 8 7 0
1 #rot 8-bit immediate
immediate alignment
11 7 6 5 4 3 0
#shift Sh 0 Rm
destination register
first operand register
set condition codes
arithmetic/logic function
25 11 8 7 0
1 #rot 8-bit immediate
immediate alignment
11 7 6 5 4 3 0
#shift Sh 0 Rm
immediate alignment
11 7 6 5 4 3 0
#shift Sh 0 Rm
Rs 0 Sh 1 Rm
62
Data processing instructions
Barrel Shifter
The ARM does not have actual shift instructions
Instead it has a barrel shifter which provides a mechanism to carry out shifts as part
•
of other instructions
So what operations does the barrel shifter support?
63
REFERENCE TEXTBOOKS
• William Hohl, “ARM assembly language
fundamentals and techniques”, CRC press, 2009
• Andrew N Sloss, “ARM System developer’s guide,
designing and optimizing system software”,
Elsevier, 2004
• J. R. Gibson, “ARM Assembly Language-an
Introduction”, Dept. of Electrical Engineering and
Electronics, The University of Liverpool, 2007