I am trying to print all doublewords in an array. However, there was an error in execution "Segmentation fault". After trying so many things, I finally made it work by storing and loading registers x8, x9, and x10 (I tested with many registers and kept eliminating registers until the program worked with these three and I can't miss a single one of these).
I have no clue why I even need to store and load these registers, isn't printf supposed to do that? And why does printf even mess these registers up? I am very confused because the parameter and return registers are x0-x7. And if printf uses other registers it should store and load.
Here's my code. (This is armv8 aarch64)
.text
.equ ELEM, 10
.extern printf
.global _start
_start:
.global selec_start
selec_start:
ldr x0, =stack
mov sp, x0
sub sp, sp, #16
ldr x8, =vec // address of vec (first element)
mov x9, #ELEM // index of last element in vec
sub x9, x9, #1
mov x10, #0 // increment i
print:
cmp x10, x9 // compare i with index of last element in vec
bgt end // branch to end if we hit end of loop
lsl x11, x10, #3 // multiply i by 8
add x12, x8, x11 // x12 = address of vec + i*8
// x12 is address of index i in vec
str x8, [sp, #-8]!
str x9, [sp, #-8]!
str x10, [sp, #-8]!
ldr x0, =string
ldr x1, [x12]
bl printf
ldr x10, [sp], #8
ldr x9, [sp], #8
ldr x8, [sp], #8
add x10, x10, #1 // i++
b print
end:
mov w8, #93
svc #0
.data
string:
.ascii "%d\n\0"
vec:
.quad 1,2,3,4,5,6,7,8,9,10
.bss
.align 8
.space 4096
stack:
.space 16
.end