Académique Documents
Professionnel Documents
Culture Documents
UNIT-3
SYLLABOUS :
Type of Instructions: Arithmetic and Logic Instructions, Branch Instructions, Addressing
Modes, Input/output Operations.
Type of Instructions :
1. Arithmetic Instructions
2. Logic Instructions
3. Branch Instructions
1.ARITHMETIC INSTRUCTIONS : Arithmetic instructions are used to perform arithmetic
operations like addition, subtraction, multiplication, division, etc.
The basic assembly language expression for arithmetic instruction is
OPcode Rd, Rn, Rm
Where the operation specified by the OP code is performed using the operands in general
purpose registers Rn and Rm. The result is placed in register Rd.
i) The ADD Instruction :The ADD instruction adds the contents of source registers and place
the result in destination register.
Examples:
1. The instruction ADD R0,R2,R4 performs the operation
R0 [ R2]+[R4].
This instruction means, add the contents of registers R2 and R4 and place the result in
register R0.
2. The instruction ADD R0,R3,#17 performs the operation
R0 [R3]+17
This instruction means, the immediate operand value 17 is added to the contents of register
R3 and place the result in register R0.
3. The instruction ADD R0,R1,R5, LSL #4
This instruction means, the second operand which is contained in register R5, is shifted left 4 bit
positions and it is then added to the contents of register R1 and sum is placed in register R0.
ii)The SUB Instruction: The SUB instruction subtracts the contents of source registers and
place the result in destination register.
Example : The instruction SUB R0,R6,R5 performs the operation
R0 [ R6] - [R5]
This instruction means, subtracts the contents of registers R6 and R5 and place the result in
register R0.
iii) The MULTIPLY Instruction : The Multiply instruction multiplies the contents of two
registers and place the product in a destination register.
There are two versions of multiply instruction are provided. The first version multiplies the
contents of two registers and place the lower order 32-bits of the product in a third register.
For Example , The instruction MUL R0,R1,R2 performs the operation
R0 [R1] × [R2]
The second version specifies a fourth register whose contents are added to the product before
storing the result in the destination register.
For Example : The instruction MLA R0,R1,R2,R3
R0 [R1] ×[R2]+[R3]
This is called a Multiply Accumulator operation. It is often used in numerical algorithm for
digital signal processing.
2. LOGIC INSTRUCTIONS : The logic instructions are used to performs the logic operations
such as AND, OR,XOR, Bit-Clear and Move Negative etc. These operations are implemented by
instructions with the OP codes AND, ORR, EOR, BIC, and MVN.
i) The AND Instruction : The AND instruction is used for supporting logical expressions by
performing bitwise logical AND operation. The bitwise logical AND operation returns 1, if the
matching bits from both the operands are 1, otherwise it returns 0.
The AND instruction Format is : AND Rd, Rn, Rm performs the operation
Rd [Rn] ^ [Rm]
Which is a bitwise logical AND between the operands in registers Rn and Rm and result is
placed in register Rd.
iii) The XOR Instruction : The XOR instruction implements the bitwise logical XOR operation.
The bitwise logical XOR operation sets the resultant bit to 1, if and only if the bits from the
operands are different. If the bits from the operands are same (both 0 or both 1), the resultant bit
is cleared to 0.
The XOR instruction Format is : EOR Rd, Rn, Rm
The bitwise logical XOR performs the operation between the operands in registers Rn and Rm
and place the result in register Rd.
Example : The instruction EOR R0, R1, R2
The bitwise logical XOR performs the operation between the operands in registers R1 and R2
and place the result in register R0.
R1 = 0 1 1 0
R2 = 0 0 1 1
----------------
After EOR -> R0 : 0101
-----------------
iii) BIC(Bit Clear) Instruction : The BIC instruction performs an AND operation on the bits in
register Rn with the complements of the corresponding bits in the value of register Rm.
v) NAND Instruction : The bitwise logical NAND operation returns the 0 if and only if both
operands are 1, otherwise the result is 1. We will use (x⋅x)′ to designate the NAND operation. It
is also common to use the ‘↑’ symbol or simply “NAND.”
The NAND instruction Format is : NAND Rd, Rn, Rm performs the operation
Rd [Rn] ↑ [Rm]
Which is a bitwise logical NAND between the operands in registers Rn and Rm and result is
placed in register Rd.
Rd [Rn] ↓ [Rm]
Which is a bitwise logical NOR between the operands in registers Rn and Rm and result is
placed in register Rd.
Example: The instruction NOR R0,R1,R2 performs the operation
R0 [R1] ↓ [R2]
The bitwise logical NOR performs the operation between the operands in registers R1 and R2
and place the result in register R0.
R1 = 0 0 1 1
R2 = 0 1 0 1
----------------
After NOR -> R0 : 1000
-----------------
3. BRANCH INSTRUCTIONS :
Branch instructions are used to implement control flow in program loops and
conditionals(executing a particular sequence of instructions only if certain conditions are
satisfied).
A branch instruction can be either an unconditional branch, which always results in
branching or a conditional branch, which may or may not causes branching depending
on some condition.
These instructions can change the flow of control in a program.
Conditional branch instruction contains a signed 2’s complement, 24 bit offset that is
added to update the contents of the program counter to generate the branch target
address.
The BEQ(Branch if equal to zero) causes a branch if Z(Zero) flag is set to 1. The
condition to be tested to determine whether or not branching should takes place is
specified in the higher order 4 bits(b31-28)of the instruction word.
EQ --- Equal
NE --- Not Equal
GT --- Greater than
LE --- Less than
GE --- Greater than or Equal
LE --- Less than or Equal
At the same time that the branch target address is computed ,the content of PC have
been updated to contain the address of the instruction that is two words beyond the
branch instruction it self.
If the branch instruction is at address location 1000, and the branch target address is
1100, then the offset has to be 92. Because the contents of updated PC will be
1000+8=1008 when the address 1100 is computed.
3.1 SETTING CONDITION CODES :
CMP Instruction:
The CMP instruction subtracts the value of operand in register Rm from the value in
register Rn.
The compare instruction is the same as a SUBS instruction except that the result is
discarded and is used in conditional execution.
CMP instruction format is as follows
CMP Rn, Rm which performs the operation [Rn] - [Rm]
have the sole purpose of setting the condition code flags based on the result of the
subtraction operation.
On the other hand, the arithmetic and logic instructions effect the condition code flags
only if explicitly specified to do by a bit in the op code field. This is indicated by
suspending the suffix S to the assembly language op code mnemonic.
For Example that, the instruction
ADDS R0,R1,R2
Sets the condition code flags but
For example, the instruction LDR R0,[R1 – 16 [R2]] performs the operation
R0 [[R1] – 16 × [R2]]
The contents of R2 is multiplied by 16 before being used as an offset and then load the effective
address into R1.
Table 1: ARM Indexed Addressing Modes
Figure 5(a) is an example of the Relative addressing mode. The address of the operand
given symbolically as ITEM in the instruction is 1060. The Relative addressing mode is
implemented by the pre-indexed mode with an immediate offset using PC as the base register.
The offset calculated by the assembler is 52 because the updated PC will contain 1008 when the
offset is added to it during program execution, and the effective address to be generated is
1060=1008 + 52.
Figure5(b) shows an example of the Pre-indexed mode with the offset contained in
register R6 and the base value contained in R5. The Store instruction(STR) stores the contents of
R3 into memory word location 1200.
Figure 5: Examples of ARM memory addressing modes
Figure 6 illustrates the writeback future in the Post-indexed and Pre-indexed addressing
modes. Figure6(a) shows the Post-indexed addressing with writeback. Suppose that R2 is used
as the base register and that it contains the initial address value 1000. Register R10 is used to
hold the offset, and it is loaded with the value 25.
The Load instruction LDR R1,[R2],R10,LSL #2
The load instruction is executed and the effective address is [R2]=1000.The number 6 at
this address , is loaded into R1. Then, the writeback operation changes the contents of R2 from
1000 to 1100, so that it points to the second number -17. It does this by shifting the contents,25
of the offset register R10 left by two bit positions and then adding them to the contents of R2.
The left shift is equivalent to multiplying 25 by 4, generating the required offset 100. After this
offset is added to the contents of R2, the new address 1100 is written back into R2. When the
Load instruction is executed on the second pass through the loop, the second number -17 is
loaded into R1. The third number 321 is loaded into R1 on the third pass, and so on.
7. INPUT/OUTPUT OPERATIONS :
The ARM architecture uses memory-mapped I/O. Reading a character from a keyboard
or sending a character to a display can be done using program-controlled I/O.
Suppose that bit 3 in each of the device status registers INSTATUS(keyboard) and
OUTSTATUS(display) contains the respective control flags SIN and SOUT.
The keyboard DATAIN and display DATAOUT registers are located at address
INSTATUS + 4 and OUTSTATUS + 4, immediately following the status register
locations.
The read and write wait loops can be implemented as follows. Assume that address
INSTATUS has been loaded into register R1. The instruction sequence
READWAIT LDR R3, [R1]
TST R3, #8
BEQ READWAIT
LDRB R3, [R1, #4]
Reads a character into register R3 when a key has been pressed on the keyboard. The
test(TST) instruction performs the bitwise logical AND operation on its two operands and
sets the condition code flags based on the result.
The immediate operand 8 has a single one in the bit 3 position. Therefore , the result of
the TST operation will be zero if bit 3 of INSTATUS is zero and will be nonzero if bit 3
is one, signifying that a character is available in DATAIN.
The BEQ instruction branches back to READWAIT if the result is zero, looping until a
key is pressed, which sets bit 3 of INSTATUS to one.
Assuming that address OUTSTATUS has been loaded into register R2, the instruction
sequence
WRITEWAIT LDR R4, [R2]
TST R4, #8
BEQ WRITEWAIT
STRB R3, [R2, #4]
Sends the character in register R3 to the DATAOUT register when the display is ready
to receive it.
These two routines can be used to read a line of characters from a keyboard, store them in
the memory, and echo them back to a display as shown in Figure 7. Registers R1 through R4
have the same usage as in the READWAIT and WRITEWAIT loops. The Store(STRB)
instruction stores the character read from the keyboard into the memory. The Test if
Equal(TEQ) instruction tests whether or not the two operands are equal and sets the Z condition
code flag accordingly.
Figure 7: An ARM program that reads a line of characters and displays it.