Module III
Module III
Module III
• Unsigned numbers are defined as data in which all the bits are used
to represent data and no bits are set aside for the positive or
negative sign.
The effect of the ADDS instruction on the overflow (V) and N (negative)
flags they are used in signed number operations
Department of EECE-19ECS431-EMBEDDED SYSTEMS 8
8
Arithmetic Instructions
a) LDR R2,=0xFFFFFFF5 ;R2=0xFFFFFFF5 (notice the = sign)
MOV R3,#0x0B
ADDS R1,R2,R3 ;R1=R2 + R3 and update the flags
b) LDR R2,=0xFFFFFFFF
ADDS R1,R2,#0x95 ;R1=R2 + 95 and update the flags
• This instruction is used for multiword (data larger than 32-bit) numbers. The form of
the ADC instruction is
• ADC Rd,Rn,Op2 ;Rd = Rn + Op2 + C
In discussing addition, the following two cases will be examined:
1. Addition of individual word data
2. Addition of multiword data
Solution:
0x4F 0000004F
– 0x39 + FFFFFFC7 2’s complement of 0x39
________ ________________
16 1 00000016 (C = 1 step 4)
• Notice that, unlike x86 CPUs, ARM does not invert the carry flag after
SUBS so C=0 when there is borrow and C=1 when there is no borrow.
• It means that after the execution of SUBS, if C=1, the result is positive;
if C = 0, the result is negative and the destination has the 2’s
complement of the result.
Classification
Multiplication of embedded
and division system
of unsigned numbers
• Not all CPUs have instructions for multiplication and division.
• All the ARM processors have a multiplication instruction but not the
division.
• Some family members such as ARM Cortex have both the division
and multiplication instructions.
Multiplication of unsigned numbers in ARM
• normal multiply - MUL is used when the result is less than 32-bit
• long multiply-MULL must be used when the result is greater than 32-
bit.
AND
AND Rd, Rn, Op2 ;Rd = Rn ANDed Op2
The destination and the first source operands are registers.
Op2 -can be a register or an immediate value of less than 0xFF
ORR
ORR Rd, Rn, Op2 ;Rd = Rn ORed Op2
Result
• The operand is shifted right bit by bit, and for every shift the LSB
(least significant bit) will go to the carry flag (C) and the MSB (most
significant bit) is filled with 0.
Department of EECE-19ECS431-EMBEDDED SYSTEMS 34
Rotate and Barrel Shifter
Classification of embedded system
One can use an immediate operand or a register to hold
the number of times it is to be shifted.
MOV R0,#0x9A ;R0 = 0x9A
MOVS R1,R0,LSR #3 ;shift R0 to right 3 times and then
move (copy) the result to R1
Another way
MOV R0,#0x9A
MOV R2,#0x03
MOV R1,R0,LSR R2 ;shift R0 to right R2 times and move the result
to R1
• In rotate right, as bits are shifted from left to right they exit from the
right end (LSB) and enter the left end (MSB).
• In addition, as each bit exits the LSB, a copy of it is given to the carry
flag.
• In other words, in ROR the LSB is moved to the MSB and is also
copied to C flag, as shown in the diagram.
Department of EECE-19ECS431-EMBEDDED SYSTEMS 37
Rotate and Barrel Shifter
Classification of embedded system
MOV R1,#0x36 ;R1 = 0000 0000 0000 0000 0000 0000 0011 0110
MOVS R1,R1,ROR #1 ;R1 = 0000 0000 0000 0000 0000 0000 0001 1011 C=0
MOVS R1,R1,ROR #1 ;R1 = 1000 0000 0000 0000 0000 0000 0000 1101 C=1
MOVS R1,R1,ROR #1 ;R1 = 1100 0000 0000 0000 0000 0000 0000 0110 C=1
Rotate left
• There is no rotate left option in ARM7
• since one can use the rotate right (ROR) to do the job.
• That means instead of rotating left n bits we can use rotate right
32–n bits to do the job of rotate left.
• Using this method does not give us the proper carry if actual
instruction of ROL was available
• In RRX, as bits are shifted from left to right, they exit from the right
end (LSB) to
• the carry flag, and the carry flag enters the left end (MSB).
• In other words, in RRX the LSB is moved to C and C is moved to the
MSB.
• C flag acts as if it is part of the operand.
• That means the RRX is like 33-bit register since the C flag is 33th bit.
• The RRX takes no arguments and the number of times an operand to
be rotated is fixed at one.
MOV R2,#0x26
MOVSDepartment
R2,R2,RRXof EECE-19ECS431-EMBEDDED SYSTEMS 39
Rotate and Barrel Shifter
Classification of embedded system
•
Classification of embedded system
In the sequence of instructions to be executed, it is often
necessary to transfer program control to a different location.
• Examples: when a function is called, execution of a loop is
repeated, or an instruction executes conditionally
Looping in ARM
Repeating a sequence of instructions or an operation a certain
number of times is called a loop.
CMP Rn,Op2
Classification of ;compare
embeddedRn with Op2 and set the flags
system
• The CMP instruction compares two operands and changes the flags
according to the result of the comparison.
• The operands themselves remain unchanged.
• There is no destination register and the second source operands
can be a register or an immediate value not larger than 0xFF
• Although BCS (branch carry set) and BCC (branch carry clear)
check the carry flag and can be used after a compare instruction, it
is recommended that BHS (branch higher or same) and BLO
(branch below) be used for two reasons.
• One reason is that assemblers will unassemble BCS as BLO, and BCC
as BHS, which may be confusing to beginner programmers.
• Another reason is that “branch higher” and “branch below” are
easier to understand
• The older ARM family members do not have an instruction for division of
unsigned Numbers
• In ARMs with no divide instructions, we can use SUB instruction to
perform the division
TST (Test)
CMP R1,R2
BHS L1
MOV R3,#2
B OVER
L1 MOV R3,#5
OVER
• Classification of embedded
In the 32-bit instruction system
BL, 8 bits are used for the opcode and the
other 24 bits, k23–k0, are used for the address of the target
subroutine
• BL can be used to call subroutines located anywhere within the
32M address space of the ARM
• To make sure that the ARM knows where to come back to after
execution of the called subroutine, the ARM automatically saves in
the link register (LR), the R14, the address of the instruction
immediately below the BL.
In above program, in place of “BX LR” for return, we could have used
“BX R14”, “MOV R15,R14”, or “MOV PC, LR” instructions. All of them
do the same thing; but it is recommended to use the “BX LR”
instruction.
Classification
MOV R1,#1 of embedded
;int n=1 system
CMP R0,#0
BEQ STOP
MOV R1,R0
NEXT SUBS R0,#01
CMP R0,#01
BEQ STOP
MUL R2,R1,R0
MOV R1,R2
B NEXT
STOP NOP
L BL
END Department of EECE-19ECS431-EMBEDDED SYSTEMS 57
Classification of embedded system
Thank You