Write an Assembly Language program to find the Sum of First 10 Integers
Program:-
AREA PROGRAM,CODE,READONLY ENTRY MOV R0,#10 MOV R1,#0 SKIP ADD R1,R1,R0 SUBS R0,#1 BNE SKIP STOP B STOP END
Explanation:-
This ARM assembly code sets up a simple loop that repeatedly adds the value in R0
to R1
while decrementing the value in R0
until R0
becomes zero. Let’s break it down step by step:
AREA PROGRAM,CODE,READONLY ENTRY
- These lines define the program area as read-only code and specify the entry point of the program.
MOV R0,#10 MOV R1,#0
- These instructions initialize
R0
with the value 10 andR1
with the value 0. These values will be used in the loop.
SKIP ADD R1,R1,R0
- This instruction adds the value in
R0
toR1
, effectively accumulating the sum inR1
.
SUBS R0,#1
- This instruction subtracts 1 from the value in
R0
. This is used as a loop counter to eventually reach zero and exit the loop whenR0
becomes zero.
BNE SKIP
- This is a conditional branch instruction. It checks the “Z” (zero) flag. If the flag is not set (meaning that the result of the previous subtraction was not zero), it branches to the
SKIP
label, which effectively restarts the loop. This continues untilR0
becomes zero.
STOP B STOP
- This is an infinite loop. It’s an unconditional branch instruction (
B
) that branches to itself (STOP
label). This effectively halts the execution of the program, as it continuously jumps back to the same instruction.
END
- This marks the end of the program.
In summary, this ARM assembly code initializes R0
to 10 and R1
to 0, then enters a loop. In each iteration of the loop, it adds the value in R0
to R1
, decrements R0
by 1, and repeats the process until R0
becomes zero. The program then enters an infinite loop, effectively stopping execution. At the end of the program, R1
will contain the sum of the numbers from 1 to 10.