简体   繁体   中英

ARM Assembly Branch Segmentation Fault

I'm new to assembly and I'm currently getting a segmentation fault when executing the following:

    .global _start      @ Provide program starting address to linker
    _start: mov R0,#0       @ A value of 1 indicates "True"
            bl  v_bool      @ Call subroutine to display "True" or "False"
            mov R0,#0       @ Exit Status code of 0 for "normal completion"
            mov R7,#1       @ Service command 1 terminates this program
            svc 0       @ Issue Linux command to terminate program

    @   Subroutine v_bool wil display "True" or "False" on the monitor
    @       R0: contains 0 implies false; non-zero implies true
    @       LR: Contains the return address
    @       Registers R0 through R7 will be used by v_bool and not saved

    v_bool: cmp R0,#0       @ Set condition flags for True or False
            beq setf        
            bne sett
            mov R2,#6       @ Number of characters to be displayed at a time.
            mov R0,#1       @ Code for stdout (standard output, monitor)
            mov R7,#4       @ Linux service command code to write.
            svc 0           @ Call Linux command

            bx  LR      @ Return to the calling program

    sett:   ldr R1,=T_msg

    setf:   ldr R1,=F_msg

        .data
    T_msg:  .ascii  "True  "    @ ASCII string to display if true
    F_msg:  .ascii  "False "    @ ASCII string to display if false
        .end

I've used the debugger to find that the causes of the segmentation fault are the two branches sett and setf, and I understand that this is caused by the program trying to write to an illegal memory location.

However, I do not understand why these branches are not able to write to R1, or what I should do to fix this. Any help is greatly appreciated.

The issue is not the instructions themselves. The problem is, after executing the instruction at, for instance setf , the execution continues on to undefined memory. You need to make sure the execution after setf and sett goes back to the code of v_bool .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM