Determining the Sum and Average of 12 inputs in ARM Assembly
<p>Edit 1: After adding</p>

<pre><code>mov r0, #0
pop {pc}
</code></pre>

<p>to the end of <strong>main:</strong>, I now get 'Segmentation fault' at the same location where 'Illegal instruction' used to occur, after the 12 user inputs.</p>

<hr>

<p>Working on an Assembly homework project, and I'm trying to understand where I'm going wrong with this. Currently, the program will execute in my raspberry pi terminal, and it will accept my twelve inputs. After that though, I get the message 'Illegal instruction'. I'm sure something is wrong with the <strong>add:</strong> area, and I haven't been able to test the <strong>avg:</strong> section, to find the average of the months - though I'm sure I wrote it incorrectly.</p>

<p>After I can figure these out, later I'll need to work on the program printing out the highest and lowest input values from the user, which may warrant its own post later.</p>

<p>Any tips or advice is appreciated on this.</p>

<pre><code>.text
.global main

add:
// sum of months
// month amounts stored in r2 (jan) thru r13 (dec)

add r1, r2, r3
add r2, r4, r5
add r3, r6, r7
add r4, r8, r9
add r5, r10, r11
add r6, r12, r13

add r7, r1, r2
add r8, r3, r4
add r9, r5, r6

add r1, r7, r8
add r2, r1, r9

ldr r0, =sum
mov r1, r2
bl printf


average:
// average of months
mov r4, #12
cmp r4, r2, lsr #1

ldr r0, =avg
mov r1, r2
bl printf

Div1:
movls   r4, r4, lsl #1
cmp r4, r2, lsr #1
bls Div1
mov r3, #0

Div2:
cmp r2, r4
subcs   r2, r2, r4
adc r3, r3, r3

mov r4, r4, lsr #1
cmp     r4, #12
bhs Div2


main:
sub sp, sp, #4
str lr, [sp, #0]

ldr r0, =welcome
bl printf

ldr r0, =jan
bl printf

// january
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r2, [sp, #0]
add sp, sp, #4

ldr r0, =feb
bl printf

// february
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r3, [sp, #0]
add sp, sp, #4

ldr r0, =mar
bl printf

// march
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r4, [sp, #0]
add sp, sp, #4

ldr r0, =apr
bl printf

// april
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r5, [sp, #0]
add sp, sp, #4

ldr r0, =may
bl printf

// may
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r6, [sp, #0]
add sp, sp, #4

ldr r0, =jun
bl printf

// june
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r7, [sp, #0]
add sp, sp, #4

ldr r0, =jul
bl printf


// july
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r8, [sp, #0]
add sp, sp, #4

ldr r0, =aug
bl printf

// august
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r9, [sp, #0]
add sp, sp, #4

ldr r0, =sep
bl printf

// september
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r10, [sp, #0]
add sp, sp, #4

ldr r0, =oct
bl printf

// october
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r11, [sp, #0]
add sp, sp, #4

ldr r0, =nov
bl printf

// november
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r12, [sp, #0]
add sp, sp, #4

ldr r0, =dec
bl printf

// december
ldr r0, =scan_pat
sub sp, sp, #4
mov r1, sp
bl scanf
ldr r13, [sp, #0]
add sp, sp, #4

mov r0, #0
pop {pc}

.data
welcome:    .asciz "Welcome to the Rainfall Statistics Program.\nYou will be asked to enter the rainfall values for each month of a year in centimeters.\n"

jan:    .asciz "Please enter the rainfall for January.\n"
feb:    .asciz "Please enter the rainfall for February.\n"
mar:    .asciz "Please enter the rainfall for March.\n"
apr:    .asciz "Please enter the rainfall for April.\n"
may:    .asciz "Please enter the rainfall for May.\n"
jun:    .asciz "Please enter the rainfall for June.\n"
jul:    .asciz "Please enter the rainfall for July.\n"
aug:    .asciz "Please enter the rainfall for August.\n"
sep:    .asciz "Please enter the rainfall for September.\n"
oct:    .asciz "Please enter the rainfall for October.\n"
nov:    .asciz "Please enter the rainfall for November.\n"
dec:    .asciz "Please enter the rainfall for December.\n"

scan_pat:   .asciz "%d"

sum:    .asciz "The total rainfall for the year is %d .\n"
avg:    .asciz "The average rainfall is %d .\n"
</code></pre>