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
R5to 6 (which will serve as a loop counter) andR0to 0 (which will store the sum of halfwords).
LDR R1,=VALUE
- This instruction loads the address of the
VALUEarray intoR1.R1will 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
R1intoR3and then incrementsR1by 2 bytes (16 bits). This effectively movesR1to the next halfword in the array.
ADD R0,R0,R3
- This instruction adds the value in
R3to the running sum stored inR0. It accumulates the sum of the halfwords.
SUBS R5,R5,#1
- This instruction decrements the loop counter
R5by 1.
CMP R5,#0 BNE LOOP
- The
CMPinstruction compares the loop counterR5to 0. IfR5is not equal to 0 (meaning the loop counter is greater than 0), it branches back to theLOOPlabel 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
RESULTvariable intoR4and stores the sum (R0) into theRESULTvariable.
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
VALUEarray, containing a series of 16-bit halfword values.
AREA INFO,DATA,READWRITE RESULT DCD 0x00000000
- This section defines the
RESULTvariable, 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.
