Reputation: 8288
Its just the second day of my learning ARM assembly and I am stuck at a problem where I get segmentation fault at STR.
@P = Q+R+S
@Q=2, R=4, S=5
.global main
main:
adrl r4,vals
ldr r1,[r4,#Q] @load Q=2 into r1
ldr r2,[r4,#R]
ldr r3,[r4,#S]
add r0,r1,r2
add r0,r0,r3
str r0,[r4,#P]
mov r7,#1
svc 0
.equ P,0
.equ Q,4
.equ R,8
.equ S,12
vals: .space 4
.word 2
.word 4
.word 5
.align
.end
can someone please help as why its crashing?
EDIT
I add the variables to the data section.
@P = Q+R+S
@Q=2, R=4, S=5
.data
vals: .space 4
.word 2
.word 4
.word 5
.align
.text
.global main
main:
adrl r4,vals
ldr r1,[r4,#Q] @load Q=2 into r1
ldr r2,[r4,#R]
ldr r3,[r4,#S]
add r0,r1,r2
add r0,r0,r3
@str r0,[r4,#P]
mov r7,#1
svc 0
.equ P,0
.equ Q,4
.equ R,8
.equ S,12
.end
Compiling and linking like below :
$ as -o main.o main.s
main.s: Assembler messages:
main.s:13: Error: symbol .data is in a different section
Upvotes: 1
Views: 465
Reputation: 8288
The segmentation fault happens because the vals
is by default assembled into the text section, hence becoming non-writable.
The solution is to define vals
in the data section like below :
@P = Q+R+S
@Q=2, R=4, S=5
.data
vals: .space 4
.word 2
.word 4
.word 5
.align
.text
.global main
main:
ldr r4,=vals
ldr r1,[r4,#Q] @load Q=2 into r1
ldr r2,[r4,#R]
ldr r3,[r4,#S]
add r0,r1,r2
add r0,r0,r3
@str r0,[r4,#P]
mov r7,#1
svc 0
.equ P,0
.equ Q,4
.equ R,8
.equ S,12
.end
Also adrl r4,vals
is replaced with ldr r4,=vals
because adrl
cannot be used to refer label in a different section, as it may break.
Upvotes: 2