0

I'm currently trying to store 5 user input values into an array of size 5. I'm having trouble looping the scanf and printf functions into my loop as well as storing the values into an array. I've tried debugging, but am unsure on where to put the breakpoints.

.data
.balign 4
m1: .asciz "User number:"

.balign 4
scan_p: .asciz "%d"

.balign 4
number_read: .word 0

.balign 4
return: .word 0

.balign 4
a: .skip 20

.balign 4
i: .word

.global main
main:
        LDR R1, address_of_return       
        STR LR, [R1]                    
        
        LDR R3, addressOfA
        
        LDR R4, addressOfI
        MOV R5, #0
        STR R5, [R4]

loop:
        LDR R5, [R4]
        CMP R5, #5
        BEQ end
        
        ADD R6, R3, R5, LSL #2

        LDR R0, address_of_m1     
        BL printf                       

        LDR R0, address_of_scan_p 
        LDR R1, address_of_number_read  
        BL scanf                        

        STR R1, [R6] // Store a[i] = *address_of_number_read                     
                   
        ADD R5, R5, #1
        STR R5, [R4]
        b loop

end:
        LDR R1, address_of_return
        LDR R1, [R1]
        BX LR

address_of_m1: .word m1
address_of_scan_p: .word scan_p
address_of_number_read: .word number_read
address_of_return: .word return
addressOfA: .word a
addressOfI: .word i

.global printf
.global scanf

Getting a segmentation fault 139

henwin
  • 1
  • 2
  • `R3` is caller saved, meaning it may be destroyed by functions you call. Hence it's not a good idea to use it for your base address. Also after `scanf` the input will not be in `R1`. That held the address of the number and is also caller saved register. The simple solution would be to pass the address you calculated directly so `scanf` will place the input where it belongs right away. – Jester Feb 13 '21 at 00:11
  • You will find the details of which registers need to be preserved in the [Procedure Call Standard for the Arm® Architecture](https://documentation-service.arm.com/static/5fa187c2b1a7c5445f29028a?token=) document. – Frant Feb 13 '21 at 02:00

0 Answers0