ARM程序集寄存器返回初始状态

时间:2016-05-02 15:57:40

标签: assembly arm

当我尝试运行汇编程序时,我收到错误 - 分段错误(错误139)。据我了解,因为我使用r3以上的寄存器,并且不会将它们返回到初始状态。我怎么能这样做?

以下是我的代码,可能是什么问题?

.text
.align  2
    .global matmul
    .type   matmul, %function
    matmul:
    @-- check if matrices can be multiplied
    cmp r1,r3
    beq  loadvalues @-- matrices can be multiplied
    mov r0,#1
    bx lr

@-- load matrice values
loadvalues:
    stmdb SP!, { r4-r15 }
    ldr r4, [sp, #0]
    ldr r5, [sp, #4]
    ldr r6, [sp, #8]
    mov r7, #0
    mov r8, #0
    mov r9, #0
    mov r10, #4
    b firstloop     
    mov r0, #0
    bx lr

@-- Loop r7 to r0
firstloop:
    cmp r7, r0
    blt firstloopex
firstloopex:
    add r7, r7, #1
    b secondloop
    LDMIA SP!, { r4-r15 }
endfirstloop:
    mov r0, #0
    bx lr   

@-- Loop r8 to r4
secondloop:
    cmp r8, r4
    blt secondloopex
secondloopex:
    add r8, r8, #1
    b thirdloop
endsecondloop:
    mul r11, r7, r8
    mul r11, r10, r11
    sub r11, r11, r10
    str r0, [r6, r11]
    mov r8, #0

@-- Loop r9 to r1
thirdloop:
    cmp r9, r1
    blt thirdloopex
thirdloopex:
    add r9, r9, #1
    @-- load value of matrice 1
    mul r11, r7, r9
    mul r11, r10, r11
    sub r11, r11, r10
    ldr r12, [r2, r11]
    @-- load value of matrice 2
    mul r11, r9, r8
    mul r11, r10, r11
    sub r11, r11, r10
    ldr r13, [r5, r11]
    @-- sum up value
    mul r14, r12, r13
    add r0, r0, r14
endthirdloop:
    mov r9, #0

1 个答案:

答案 0 :(得分:1)

在ARM中,您需要保存并恢复{r4-r11}。过去都是特殊的寄存器。以下是ARM中函数的框架。

my_func:
    push {r4-r11}    # Save callee-saved registers. stmdb sp!, {r4-r11}
    ...
    pop  {r4-r11}    # Restore callee-saved registers. ldmia sp!, {r4-r11}
    mov r0, #0       # Save return value to r0
    bx lr

注意:有时r9也是一个特殊的寄存器,不应该保存 - 恢复。我不会进入细节,因为它很少见。您可以在AAPCS(Arm Architecture Procedure Call Standard)http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042f/IHI0042F_aapcs.pdf中阅读它。