0
votes

In this exercise I have to set to 0 all even numbers in this array, but when I run the program I got a segmentation fault. Can someone help me? And also, how can I print the result on my raspberry pi? Thank you!

Edit: I have changed comments from italian to english. Hope it is more understandable!

                .data
v:              .word 1,2,3,4,5,6,7,8   @ at the end I have to get 1,0,3,0,5,0,7,0
                .text
                .global main
main:           mov r0, #8              @ dimension of my array
                ldr r1, =v              @ address of my array
                push {lr}               

                mov r3, #4              @ index = 1

loop:           cmp r3, #32             @ condition of the loop
                bge exit                @ if r3 is greather or equal exit from the loop

                ldr r1, [r1, r3]        @ load the element of my array of index r3 in r1
                mov r1, #0              @ set my even element to 0
                str r1, [r1, r3]
                add r3, r3, #8          @ increment my index of 2 position
                b loop                  @ back to loop function

exit:           pop {pc}                @ quit from my loop function and back to main
1
It's not a good idea to destroy your array pointer by mov r1, #0. PS: learn to use a debugger. - Jester
Thank you! As debugger do you suggest to use gbd? - Samuele Calugi
Yeah gdb would work. - Jester

1 Answers

2
votes

Questions on StackOverflow should be written in English, please, and that includes comments in source code (and preferably labels too) because these are as important to the people trying to help you with your code as they are to you. Fortunately, I speak some Italian!

The problem is that you are re-using r1. It holds your array pointer until the line

ldr r1, [r1, r3]

at which point it holds the contents of an array element instead. That would be fine of course if you never needed the array pointer again, but you do; and the line

str r1, [r1, r3]

is a dead giveaway because it is trying to store the value of r1 at the address given by the value of r1+r3 which is unlikely ever to be correct and certainly isn't in your case.