ARM程序集分支分段错误

时间:2018-02-06 18:24:16

标签: linux assembly raspberry-pi arm branch

我是汇编新手,我在执行以下操作时遇到了分段错误:

    .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

我使用调试器发现分段错误的原因是两个分支sett和setf,我知道这是由程序试图写入非法内存位置引起的。

但是,我不明白为什么这些分支无法写入R1,或者我应该怎么做才能解决这个问题。任何帮助是极大的赞赏。

1 个答案:

答案 0 :(得分:0)

问题不在于指令本身。问题是,在执行了例如setf的指令之后,执行继续到未定义的存储器。您需要确保setfsett之后的执行回到v_bool的代码。