Write an ARM assembly program that tallies the number of 0s,1s, 2s … 9s in the D
ID: 3864260 • Letter: W
Question
Write an ARM assembly program that tallies the number of 0s,1s, 2s … 9s in the DCB array sourceArray and stores the 10 number counts in a 10-element word array starting at location 0x40000000. For example, the 0s count will be in location 0x40000000, the 1s count will be in location 0x40000004, etc. You must first initialize each element of the “count” array to zero.
Use the following data definitions in the program.
sourceArray DCB 2,4,6,6,0,1,4,8,3,0,3,1,2,2,5,2,2,3, 8,1
DCB 9,1,5,3,4,2,6,7,8,9,0,4,4,3,2,3,6,5,4,5,6,7
DCB 4,1,2,1,4,6,7,6,3,3,2,6,8,9,9,9,1,4,2,3,4,5
sourceArraySize DCB (sourceArraySize-sourceArray)
The following memory addressing mode helpful may be helpful: LDR/STR r4, [r5, r6, LSL #2]
Where r5 points to the beginning of the counts array and r6 contains a number from sourceArray.
Explanation / Answer
; SUM = 0 (uses r6 for sum)
; for I = 0 to LENGTH - 1 do (uses r1 for I)
; SUM = SUM + ARRAY[I] (uses r3 for address of
A[I])
; end for
AREA SUMARRAY, CODE, READONLY
ENTRY ; Always needed to indicate where to start pgm
LDR r2, LENGTH
SUB r2, r2, #1 ; r2 contains (LENGTH-1)
MOV r6, #0 ; r6 sum set to 0
FOR_INIT MOV r1, #0 ; r1 index I set to 0
ADR r3, ARRAY ; start r3 with address of A[0]
FOR_CMP CMP r1, r2 ; compare I and (LENGTH-1)
BGT END_FOR ; drop out of loop if I < (LENGTH-1)
LDR r4, [r3],#4 ; load r4 with A[I] then walk r3 dow
n ARRAY
ADD r6, r6, r4 ; update sum with A[I]
ADD r1, r1, #1 ; increment I
B FOR_CMP ; loop back to for-loop check
END_FOR
STR r6, SUM ; store result in SUM
STOP B STOP
AREA SUMARRAY, DATA, READWRITE
ALIGN
SUM DCD 0XFFFFFFFF
ARRAY DCD 5, 10, 15, 20, 30, 40, 50
LENGTH DCD 7
END ; Needed to stop assembly
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.