Program to add array of 16 bit number to store 32-bit result in Internal Ram

Write a program to add an array of the 16-bit number to store 32-bit result in internal RAM

Program:-

    AREA ADDITION,CODE,READONLY
    ENTRY
    MOV R5,#6
    MOV R0,#0
    LDR R1,=VALUE
LOOP LDRH R3,[R1],#2
    ADD R0,R0,R3
    SUBS R5,R5,#1
    CMP R5,#0
    BNE LOOP
    LDR R4,=RESULT
    STR R0,[R4]
STOP B STOP
VALUE DCD 0x0000,0x1111,0x2222,0x3333,0xAAAA,0xBBBB,0xCCCC
    AREA INFO,DATA,READWRITE
RESULT DCD 0x00000000
END

Explanation:-

This ARM assembly code appears to calculate the sum of 16-bit halfword values stored in the VALUE array and stores the result in the RESULT variable. Here’s a step-by-step breakdown of how the code works:

AREA ADDITION,CODE,READONLY
ENTRY
  • These lines define the program area as read-only code and specify the entry point of the program.
MOV R5,#6
MOV R0,#0
  • These instructions initialize R5 to 6 (which will serve as a loop counter) and R0 to 0 (which will store the sum of halfwords).
LDR R1,=VALUE
  • This instruction loads the address of the VALUE array into R1. R1 will be used as a pointer to access the halfwords in the array.
LOOP LDRH R3,[R1],#2
  • This is the start of a loop. It loads a 16-bit halfword from the memory location pointed to by R1 into R3 and then increments R1 by 2 bytes (16 bits). This effectively moves R1 to the next halfword in the array.
ADD R0,R0,R3
  • This instruction adds the value in R3 to the running sum stored in R0. It accumulates the sum of the halfwords.
SUBS R5,R5,#1
  • This instruction decrements the loop counter R5 by 1.
CMP R5,#0
BNE LOOP
  • The CMP instruction compares the loop counter R5 to 0. If R5 is not equal to 0 (meaning the loop counter is greater than 0), it branches back to the LOOP label to continue the loop. This loop processes 6 halfwords in total.
LDR R4,=RESULT
STR R0,[R4]
  • After the loop completes, this section loads the address of the RESULT variable into R4 and stores the sum (R0) into the RESULT variable.
STOP B STOP
  • This is an infinite loop, effectively halting the program.
VALUE DCD 0x0000,0x1111,0x2222,0x3333,0xAAAA,0xBBBB,0xCCCC
  • This section defines the VALUE array, containing a series of 16-bit halfword values.
AREA INFO,DATA,READWRITE
RESULT DCD 0x00000000
  • This section defines the RESULT variable, which will store the sum of the halfwords.
END
  • This marks the end of the program.

In summary, this ARM assembly code loads 16-bit halfwords from the VALUE array, accumulates their sum in R0, and stores the result in the RESULT variable. It uses a loop controlled by R5 to process all the halfwords in the array.

Leave a Reply

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