0% found this document useful (0 votes)
36 views30 pages

L7_ARM Instructions on arithmatic and logical operation

The document provides an overview of looping and branching in ARM programming, detailing the use of the BNE instruction for loops and other conditional branches like BCC. It includes examples of programs that demonstrate how to implement loops for repetitive tasks, such as adding values and finding the maximum in an array. Additionally, it covers comparison instructions and unconditional branches, emphasizing their role in control flow within ARM assembly language.

Uploaded by

mainahibataunga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views30 pages

L7_ARM Instructions on arithmatic and logical operation

The document provides an overview of looping and branching in ARM programming, detailing the use of the BNE instruction for loops and other conditional branches like BCC. It includes examples of programs that demonstrate how to implement loops for repetitive tasks, such as adding values and finding the maximum in an array. Additionally, it covers comparison instructions and unconditional branches, emphasizing their role in control flow within ARM assembly language.

Uploaded by

mainahibataunga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Looping and Branch

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.

unsigned long myData[5]= {69,87,96,45,75};


unsigned long count = 5;
unsigned long max = 0;
unsigned long next;
unsigned long *pointer = myData;
do
{
next = *pointer;
if (max < next)
max = next;
pointer ++;
count —;
}while(count != 0);
• ;searching for highest value
COUNT RN R0 ;COUNT is the new name of R0
MAX RN R1 ;MAX is the new name of R1
;(MAX has the highest value)
POINTER RN R2 ;POINTER is the new name of R2
NEXT RN R3 ;NEXT is the new name of R3
AREA PROG_4_1D, DATA, READONLY
MYDATA DCD 69,87,96,45,75
AREA PROG_4_1, CODE, READONLY
ENTRY
MOV COUNT,#5 ;COUNT = 5
MOV MAX,#0 ;MAX = 0
LDR POINTER,=MYDATA ;POINTER = MYDATA ( address of first data )
AGAIN LDR NEXT,[POINTER] ;load contents of POINTER location to NEXT
CMP MAX,NEXT ;compare MAX and NEXT
BHS CTNU ;if MAX > NEXT branch to CTNU
MOV MAX,NEXT ;MAX = NEXT
CTNU ADD POINTER,POINTER,#4 ;POINTER=POINTER+4 to point to the next
SUBS COUNT,COUNT,#1 ;decrement counter
BNE AGAIN ;branch AGAIN if counter is not zero
HERE B HERE
END
• CMN Rn,OP2;
• Using CMP followed by conditional branches we can make any
comparison on unsigned numbers
• TST (Test)
• TST Rn,Op2 ;Rn AND with Op2 and flag bits are updated
• The TST instruction is used to test the contents of register to see if any
bit is set to HIGH. After the operands are ANDed together the flags
are updated
• After the TST instruction if result is zero, then Z flag is raised
• one can use BEQ (branch equal) to make decision
• TEQ (test equal)
• TEQ Rn,Op2 ;Rn EX-ORed with Op2 and flag bits are set
• The TEQ instruction is used to test to see if the contents of two
registers are equal
• Unconditional branch (jump) instruction
• The unconditional branch is a jump in which control is transferred
unconditionally to the target location
• In the ARM there are two unconditional branches: B (branch) and
BX (branch and exchange).
• B (Branch)
• B (branch) is an unconditional jump that can go to any memory
location in the 32M byte address space of the ARM.
• While loop

You might also like