0

So i'm writing an ARMv8 assembly program using subroutines. I'm using the 'bl' command to jump to and return from a specific subroutine, but at the end of the subroutine it is not returning.

main:   stp  x29, x30, [sp,-16]!
        mov  x29, x30

        bl   newPyr
        bl   print

        ldp  x29, x30, [sp], 16
        ret

print:  adrp x0, title
        add  x0, x0, :lo12:title
        bl   printf

        adrp x0, origin
        add  x0, x0, :lo12:origin
        ldr  w1, [p_base,first_start+pyramid_start+origin_x]
        mov  w2, w1
        bl   printf

        ret

there is more to the program that i haven't included but the rest is all working fine. Basically in main it runs the newPyr subroutine and when its done it returns and then runs the print subroutine. newPry works fine, but print does not. It will display the appropriate message, but when it reaches the ret, nothing happens. I'm pretty new to assembly and subroutines. any ideas?

  • Which register is the link register? I forget the ARMv8 register numbers. You are saving/restoring it across the calls to `printf`, right? Actually you aren't, you `bl printf` / `ret`, which can't possibly work because `bl` overwrites the link register. So I think you get an infinite loop in the `ret` at the bottom of `print`. **Use a debugger** to see. – Peter Cordes Nov 07 '17 at 07:31

1 Answers1

0

While at print you are using the address in LR (Link Register) that gets put there by a previous call. Which in your case is the address of the instruction right before ret (that is bl printf) plus 4. So, you trying to ret to the exact address you are currently at. I assume that "nothing happens" is actually an infinite loop.

Anton
  • 1