L7_ARM Instructions on arithmatic and logical operation
L7_ARM Instructions on arithmatic and logical operation
Instructions.
• Looping in ARM
• Repeating a sequence of instructions or an operation a certain
number of times is called a loop
• The loop is one of the most widely used programming techniques.
• Using instruction BNE for looping
• The BNE (branch if not equal) instruction uses the zero flag in the
status register.
• BACK ……… ;start of the loop
• ……… ;body of the loop
• ……… ;body of the loop
• SUB Rn,Rn,#1 ;Rn = Rn - 1,
• BNE BACK
• Write a program to
• (a) clear R0,
• (b) add 9 to R0 a thousand times, then
• (c) place the sum in R4.
• Use the zero flag and BNE instruction.
• Write a program to place value 0x55 into 100 bytes of RAM locations.
• Other conditional Branches
• BCC (branch if carry clear, branch if C = 0)
• In this instruction, the carry flag bit in program status registers (CPSR)
is used to make the decision whether to branch.
• executing “BCC label”, the processor looks at the carry flag to see if it
is cleared (C = 0).
MOV R1,#0 ;clear high word (R1 = 0)
MOV R0,#0 ;clear low word (R0 = 0)
LDR R2,=0x99999999 ;R2 = 0x99999999
ADDS R0,R0,R2 ;R0 = R0 + R2 and set the flags
BCC L1 ;if C = 0, jump to L1 and add next number
ADDS R1,R1,#1 ;ELSE, increment ( R1 = R1 + 1)
L1 ADDS R0,R0,R2 ;R0 = R0 + R2 and set the flags
BCC L2 ;if C = 0, add next number
ADDS R1,R1,#1 ;if C = 1, increment
L2 ADDS R0,R2 ;R0 = R0 + R2 and set the flags
BCC L3 ;if C = 0, add next number
ADDS R1,R1,#1 ;C = 1, increment
L3 ADDS R0,R2 ;R0 = R0 + R2 and set the flags
BCC L4 ;if C = 0, add next number
ADDS R1,R1,#1 ;if C = 1, and set the flags
• Write above program using Loop
• A. Store 10 32 bit numbers in code memory
• B. Find the sum of all those numbers
• C. store the result in the data memory
• AREA mycode, CODE, READONLY
• Entry
• MOV R4,#10; COUNTER
MOV R2, #0; RESULT
MOV R3, #0 ; ACCUMULATE THE CARRY
LDR R0,=Data
UP LDR R1,[R0],#4
ADDS R2, R1
BCC DOWN
ADD R3,#01
DOWN SUB R4,#1
BNE UP
LDR R0,= Result
LDR. R2,[R0]
• Data dcd 0x12345678, 0xabcdef12, 0x44, 0x5555,………..
• AREA data , DATA, READWRITE
• Result dcd 0.
• Comparison of unsigned numbers
• CMP Rn,Op2 ;compare Rn with Op2 and set the flags
• The CMP instruction compares two operands and changes the flags
according to the result of the comparison
• The operands themselves remain unchanged
• Assume that there is a class of five people with the following grades: 69, 87, 96, 45, and 75. Find the highest grade.