Assembly program to find the Sum of first 10 Integers

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 and R1 with the value 0. These values will be used in the loop.
SKIP ADD R1,R1,R0
  • This instruction adds the value in R0 to R1, effectively accumulating the sum in R1.
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 when R0 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 until R0 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.

Leave a Reply

Your email address will not be published. Required fields are marked *