Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I exit main subroutine in ARM assembly?

How can I finish program as below code continuously loops between __mainCode and funcOne subroutines?

__mainCode  PROC 
            MOV R0, 5                       ;0x0800 0008
            LDR R1, =0xA                    ;0x0800 000C
            BL funcOne                      ;0x0800 0010
            POP {R3}                        ;0x0800 0014
            ENDP ; end of function

            
funcOne     PROC
            MOV R2, #11                     ;0x0800 0018
            PUSH {R2}                       ;0x0800 001c
            BX LR                           ;0x0800 001e
            ENDP

            ALIGN ; fill rest of bytes with 0s
            END

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Bare metal code does not have any "exit" routine as it does not have anything to pass the control to.

When you write "normal" C program when you return from the main function the startup code simply disables the interrupts and enters the infinitive loop.

Below fragment of the startup code generated by the STMCube (they did not even care abut the interrupts)

/* Call the application's entry point.*/
    bl  main

LoopForever:
    b LoopForever

What is your code doing?

  1. When BX LR is executed, it jumps to POP {R3} and continues execution
  2. It reaches again BX LR (LR was not modified) and cycle repeats.

As you push R2 and next pop R3 the stack pointer remains the same and never reaches not legal addresses which could raise the BusError potentially breaking this dead loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading