CHAPTER 3 - 4-Flags and Data Processing Instructions - 3
CHAPTER 3 - 4-Flags and Data Processing Instructions - 3
BENC2423
FLAGS & DATA PROCESSING INSTRUCTIONS
Introduction
Flags and their use – N, Z, C, V
Comparison instructions
Data movement instructions
Data processing instructions
• Logical instructions - Boolean operations
• Arithmetic instructions -Addition/Subtraction
• Multiplication
• Division
• Shifts and Rotates
Introduction
• The Application Program Status Register (APSR) holds the ALU flags,
with the bits configured as shown in thefigure.
• The top four bits (bits 31, 30, 29, and 28) are the condition code (cc)
bits and are of most interest to us.
• Some instructions can optionally update the Condition Code (cc)
Flags (NZCV) to provide information about the result of executing
the instruction.
• What’s the purpose of flags?
– Tells the microprocessor the current status of arithmetic operation
and bit manipulation.
N: Negative
• N=1 to bit 31 of the result. N=1 if the signed value is negative, and N=0 if
the result is positive or zero.
• The N flag is set by an instruction if the resultis negative.
• In practice, N is set to the 2’s complement sign bit of the result (bit 31).
•E.g: ADDS r0, r1, r2
r1 = -110 = 0xFFFFFFFF
r2 =-210 =0xFFFFFFFE
r0 = (-1) + (-2), the result is -3 so N=1 (N flag isset)
Z: Zero
• Z=1 when the result of the operation is Zero. This is usual to denotean equal
result from a comparison. Ifthe result is non-zero, this flag is cleared.
•E.g: SUBSr0, r1, r2
r1 = 110
r2 =110
r0 = (1) - (1), the result is 0 so Z=1 (Z flag is set)
Result = 010 so Z = 1
Flags and their use: N, V, Z, C(cont.)
C: Carry (known as unsignedoverflow)
• C=1 ifthe result of an unsigned operation overflows the 32-bit result register.
• For other instructions that use shifting, this flag is set to the value of the last bit
shifted out by the shifter.
• Other instructions usually leave this flag alone.
E.g:
1010 0001 0010 0011 0100 0101 0110 0111 Adding two (-ve) numbers
+ 1011 0000 0000 0000 0000 0000 0000 0000 that produces (+ve) results
1 0101 0001 0010 0011 0100 0101 0110 0111 indicates V=1
The carry bit does not indicateV=1
Flags and their use: N, V, Z, C(cont.)
Overflow rule for subtraction
• Overflow occurs if
• (+A) − (−B) = −C (+A) + (B) = -C
• (−A) − (+B) = +C change –A and –B to 2’s complement and then perform addition
• All the “-” numbers need to be changed to 2’s complement numbers. In the end,
addition is performed instead of subtraction.
• Example of overflow for 32 bit subtraction:
LDR r1, =0x70000000
LDR r2, =-0x60000000
SUBS r0, r1, r2
0111 0000 0000 0000 0000 0000 0000 0000 Adding two (+ve) numbers
+ 0110 0000 0000 0000 0000 0000 0000 0000 that produces (-ve) results
1101 0000 0000 0000 0000 0000 0000 0000 indicates V=1
Bit [31] = 1, so N = 1
Flags and their use: N, V, Z, C(cont.)
• Consider the following example:
LDR r1, =0xffffffff
LDR r2, =0x00000001
ADDS r0, r1, r2
• The result of the operation would be 0x100000000, but the top bit is
lost because it does not fit into the 32-bit destination register and so the
real result is 0x00000000. In this case, the flags will be set as follows:
Flag Condition
N= 0 The result is 0, which is considered positive, and so the N (negative) bit is set to 0.
Z=1 The result is 0, so the Z(zero) bit is set to 1.
C= 1 We lost some data because the result did not fit into 32 bits, so the processor
indicates this by setting C(carry) to 1.
V= 0 From a two's complement signed-arithmetic viewpoint, 0xffffffff really means -1, so
the operation we did was really (-1) + 1 = 0. That operation clearly does not overflow,
so V (overflow) is set to 0.
Comparison instructions
• These instructions do not store the result inany register.
• It set the condition code bits (N, Z, C, and V) in the CPSRaccording to the selected
operation.
r3 == 0?
(true) (false)
r3 == 0 r3 != 0
Other flags can also be
c=d+e c=d-e affected not just Z
Exit
Comparison instructions (cont.)
Example:
• Assuming r1 = 0x70000000 and r2 =
0x70000000. What are the CPSRflags
would be after the instructions
Instructions Flags
CMP r1, r2 N = 0, Z= 1, C= 1, V = 0 set cc on r1 – r2
CMN r1, r2 N = 1, Z= 0, C= 0, V = 1 set cc on r1 – (~r2)
TST r1, r2 N = 0, Z= 0, C= 0, V = 0 set cc on r1 and r2
TEQ r1, r2 N = 0, Z= 1, C= 0, V = 0 set cc on r1 xor r2
Exercise 1
S: Is an optional suffix. If S is specified, the condition code flags are updated on the result of the operation.
Example:
MOV r2, #2 ; Load the binary value 0000 0000 0000 0000 0000 0000 0000
0010into registers r2, r3 and r4.
MOV r3, r2 ; This sequence uses immediate data to register and register to
register data transfer.
MOV r4, r3
•These instruction ignore the first operandand
simply move the second operand to the
destination.
MOV r0, r2 ; r0 = r2
–A MOV instruction that has the same source anddestination
registers does not do anything!
Data Movement Instructions (cont.)
MVN r0, r1 ; r0 = not r1
• The ‘MVN’ stands for ‘move negated’; it leaves the result register set
to the value obtained by inverting every bit in the source operand.
r1: 1101 0110 1010 0000 0111 0101 10100011
r0: 0010 1001 0101 1111 1000 1010 01011100
Syntax
MOVT{cond} Rd, #imm16
where:
cond: is an optional condition code.
Rd: is the destination register.
imm16: is a 16-bit immediate constant.
Example:
MOVT r3, #0×ABCD ; Write 0×ABCD to upper halfword ofr3, lower halfword and
; APSRare unchanged
Data Movement Instructions (cont.)
Answer: r4 =0×ABCD5678
Comparison of instructions for
loading constants
MOV rd, constant
• Works for 0 – 65535 (16 bit)
AND
Example:
AND r0, r1, r2 ; r0 = r1 and r2
the result bit is 1 if both input bits are 1
r1: 0101 0011 1010 1111 1101 1010 01101011
r2: 1101 0110 1010 0000 0111 0101 10100011
r0: 0101 0010 1010 0000 0101 0000 00100011
A B OR
0 0 0
0 1 1
1 0 1
1 1 1
Logical Instructions (cont.)
• ORR is used to set specific bits
• Bits ‘OR’ with 1 results in a1 r0 1 1 0 1
+ + + +
• Bits ‘OR’ with 0remains 0x03 0 0 1 1
unchanged
r1 1 1 1 1
• Example: set bits 1 and 2
– Assume r0 = 0x0000000D
• ORR r1, r0, #0x03
– 0x03 = 0011b (set bits 0 and 1)
– 0x03 is known as the bit mask
Logical Instructions (cont.)
EOR
Example:
EOR r0, r1, r2 ; r0 = r1 xor r2
the result bit is set to 1 if one and only one of the inputs bits is 1
r1: 0101 0011 1010 1111 1101 1010 01101011
r2: 1101 0110 1010 0000 0111 0101 10100011
r0: 1000 0101 0000 1111 1010 1111 1100 1000
EORinstructions can be useful for inverting specificbits.
A B XOR
0 0 0
0 1 1
1 0 1
1 1 0
Logical Instructions (cont.)
Example 2:
r1=0x02FA62CA r2=0x0000FFFF
BIC r0, r1, r2
r0=0x02FA0000
Arithmetic Instructions
Addition/Subtraction
– Syntax: <instruction>{<cond>}{S} Rd, Rn, Operand2
Instruction P-code Comment
ADD r0, r1, r2 r0 := r1 +r2 Add two 32-bit values
ADCr0, r1, r2 r0 := r1 + r2 +C Add two 32-bit values andcarry
SUBr0, r1, r2 r0 := r1 - r2 Subtract two 32-bit values
SBCr0, r1, r2 r0 := r1 - r2 + C-1 subtract withcarry of two 32-bit values
RSBr0, r1, r2 r0 := r2 – r1 Reverse subtract of two 32-bit values
RSCr0, r1, r2 r0 := r2 – r1 + C- 1 Reverse subtract with carry of two 32-bitvalues
Arithmetic Instructions (cont.)
If r1 =0×0010
r2 =0×0001
r3 =0×0100
Answer Exercise 6
where
cond: is an optional condition code
S: is an optional suffix. If Sis specified, the conditioncode flags are
updated on the result of theoperation.
Rd: is the destination register. If Rd is omitted, the destination
register is Rn.
Rn, Rm: Are registers holding the values to bemultiplied.
Ra: is a register holding the value tobe added or subtracted from.
Multiplication Instructions (cont.)
Example:
if r1 = 0xFFFFFFFF,r2 = 0x80000000, and r3 = 0x00020700 then
MUL r0, r1, r2 ; r1 * r2, r0 = 0x80000000
MLA r0, r1, r2, r3 ; (r1 * r2) + r3, r0 = 0x80020700
MULS r0, r1, r2 ; r1 * r2, r0 = 0x80000000, N = 1
Multiplication Instructions (cont.)
xn-1 A1
xn-2 A2
xn-3 A3
4x2 + 3x – 5
where
cond: is an optional condition code
Rd: is the destination register. If Rd is omitted, thedestination register is Rn
Rn: is the register holding the value to be divided.
Rm: is a register holding the divisor.
Division Instructions (cont.)
Example:
Result
Shift and Rotate Instructions (cont.)
• Barrel shifter can support 6 operations:
– LSL Logical Shift Left (unsigned multiplication by2n)
• Fill in “0” on the right
– LSR Logical Shift Right (unsigned division by2n)
• Fill in “0” on the left
– ASL Arithmetic Shift Left (unsigned multiplication by2n)
• Fill in “0” on the right
– ASR Arithmetic Shift Right (signed division by2n)
• Fill in “1” on the left
– ROR Rotate Right
• rotate right by 0 to 32 places; the bits which fall off the least significant end of the word are used, in
order, to fill the vacated bits at the most significant end of the word.
– RRX Rotate Right with Extend
• rotate right extended by 1 place; the vacated bit (bit 31) is filled with the old value of the Cflagand
the operand is shifted one place to the right.
ASL and LSL are the same, and may be freely interchanged.
• Shift and rotate operands can be applied to any of the followingARM instructions:
ADC,ADD, AND, BIC, CMN, CMP,EOR,MOV, MVN, ORR,RSB,SBC,SUB, TEQ,TST.
• Shift and rotate operands can also be applied to registers used for calculating
addresses in LDRand STRinstructions.
Shift and Rotate Instructions (cont.)
• Syntax
op{S}{cond} Rd, Rm,Rs
op{S}{cond} Rd, Rm,#n
RRX{S}{cond} Rd, Rm
– Op is one of ASR,LSL,LSRand ROR.
– S is an optional suffix. If S is specified, the cc flag are updated on the result of
the operation.
• these instructions update the N and Z flags according to the result.
• the C flag is updated to the last bit shifted out, except when the shift length is 0.
– Rd specifies the destination register.
– Rm specifies the register holding the value tobe shifted.
– Rs specifies the register holding the shift length to apply to the value Rm. Only
the least significant byte is used and can be in the range 0 to 255.
– n Specifies the shift length. The range of shift length depends on the
instruction:
• ASRshift length from 1 to32
• LSLshift length from 0 to31
• LSRshift length from 1 to32
• RORshift length from 1 to31.
Shift and Rotate Instructions (cont.)
0 1 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 0 1 0 0 1 0 0 0 0
LSL#4
Shift and Rotate Instructions (cont.)
Example: Logical shift left (LSL)
C Register 0
MOV R0, R2, LSL #2 @ R0:=R2<<2
@ R2 unchanged
Example: 0…0 0011 0000
Before R2=0x00000030
After R0=0x000000C0
R2=0x00000030
Shift and Rotate Instructions (cont.)
0 0 0 0 1 0 1 0 0 1 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 0
LSR #4
Shift and Rotate Instructions (cont.)
0 Register C
MOV R0, R2, LSR #2 @ R0:=R2>>2
@ R2 unchanged
Example: 0…0 0011 0000
Before R2=0x00000030
After R0=0x0000000C
R2=0x00000030
Shift and Rotate Instructions (cont.)
MOV r0, r1, ASL#4 ; shift value in r1 4 places left and fill in vacant slots with 0’s
MOV r0, r1, ASLr2 ; shift value in r1 by number of places given in r2 and fill in
vacant slots with 0’s
1 0 1 0 0 1 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 0 1 0 0 1
0 1 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 0 1 0 0 1 0 0 0 0
ASL#4
Shift and Rotate Instructions (cont.)
ASR (Arithmetic Shift Right)
• ASRis an arithmetic shift right by 0 to 32places.
• The vacated bits at the most significant end of the
word are filled with zeros if the original value (the
source operand) was positive.
MOV r0, r1, ASL#4 ; shift value in r1 4 places left and fill in vacant bits with copies
of original MSB.
MOV r0, r1, ASLr2 ; shift value in r1 by number of places given in r2 and fill in
vacant bits with copies of original MSB.
00100101100011010011010001101001
0 0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 0
ASR #4
Shift and Rotate Instructions (cont.)
• The vacated bits are filled with ones if the original
value was negative.
• This is known as "sign extending" because the most
significant bit of the original value is the sign bit for 2's
complement numbers, i.e. 0 for positive and 1 for
negative numbers.
• Arithmetic shifting therefore preserves the sign of
numbers.
10100101100011010011010001101001
1 1 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 0
ASR #4
Shift and Rotate Instructions (cont.)
MSB Register C
10100101100011010011010001101001
10011010010110001101001101000110
ASR #4
Shift and Rotate Instructions (cont.)
Example: Rotate right (ROR)
Register C
C 31 0
0 10100101100011010011010001101001
1 0101001011000110100110100011010 0
RRX
Shift and Rotate Instructions (cont.)
Register C
(a) Based on the above figure, access the values stored in register r0, r1 andr2
after the program completes.
(b) Change the program in the figure above by replacing the lastMOV
instruction with
Access the value in register r2 when the program reaches the infiniteloop
(the Binstruction).
Answer Exercise 12
(ii) r2 = 0×88