user11000927
user11000927

Reputation: 23

SegFault when calling function in asm

I started to learn calling a function in assembly. I followed much tutorial in the internet and make some modification to it.

But it doesnot really work as expected.

.data
 hello:  .ascii "hello everyone\n"
 len= . - hello
 .text

.global _start

exit:
       mov %r1,#0
       mov %r2,#0
       mov %r0, #0
       mov %r7, #1
       swi #0

println:
        mov %r7, #4
        swi #0
        mov %pc, %lr
        bx %r7
_start:
        ldr %r1, =hello
        ldr %r2, =len
        b println
        b exit

and the output goes

hello everyone
Segmentation fault

I dont know where i was wrong.

Upvotes: 1

Views: 65

Answers (1)

fuz
fuz

Reputation: 93082

For function calls, use the bl (branch and link) instruction. This sets up lr to contain the return address. Your code uses b (branch) rather than bl, so lr is not set up and returning from println goes to an unpredictable address, likely crashing your program.

To fix this, use bl instead of b for function calls:

    bl println
    bl exit

Upvotes: 2

Related Questions