SIGBUS 错误 - 在 RaspberryPi 上组装 ARM 处理器

SIGBUS Error - Assembly for ARM processor on RaspberryPi

我在业余时间学习汇编,因为我对低级操作很感兴趣。 I tried running the code from this tutorial.

The code can be found here及以下:

/******************************************************************************
* @file float.s
* @brief simple example of scalar multiplication using the FPU
*
* Simple example of using the ARM FPU to compute the multiplication result of
* two float values
*
* @author Christopher D. McMurrough
******************************************************************************/

    .global main
    .func main

main:

    LDR R0, =val1           @ load variable address
    VLDR S0, [R0]           @ load the value into the VFP register

    LDR R0, =val2           @ load variable address
    VLDR S1, [R0]           @ load the value into the VFP register

    VMUL.F32 S2, S0, S1     @ compute S2 = S0 * S1

    VCVT.F64.F32 D4, S2     @ covert the result to double precision for printing
    VMOV R1, R2, D4         @ split the double VFP register into two ARM registers
    BL  _printf_result      @ print the result

    B   _exit               @ branch to exit procedure with no return

_exit:  
    MOV R7, #4              @ write syscall, 4
    MOV R0, #1              @ output stream to monitor, 1
    MOV R2, #21             @ print string length
    LDR R1, =exit_str       @ string at label exit_str:
    SWI 0                   @ execute syscall
    MOV R7, #1              @ terminate syscall, 1
    SWI 0                   @ execute syscall

_printf_result:
    PUSH {LR}               @ push LR to stack
    LDR R0, =result_str     @ R0 contains formatted string address
    BL printf               @ call printf
    POP {PC}                @ pop LR from stack and return

.data
result_str:     .asciz      "Multiplication result = %f \n"
exit_str:       .ascii      "Terminating program.\n"
val1:           .float      3.14159
val2:           .float      0.100

我在 Raspberry Pi 中编译代码做:

gcc -o float float.s

它编译没有任何问题,但是当我 运行 它时,我得到以下错误:

Program received signal SIGBUS, Bus error.
0x00010428 in main()

我已经对导致 SIGBUS 错误的原因做了一些研究,我在这里唯一能想到的就是访问未对齐的内存,但是 0x00010428 = 十进制的 66,600 可以被 4 整除,所以我不是确定是什么问题。

运行

cat/proc/cpuinfo

我得到以下信息:

processor: 0
model name: ARMv6-compatible processor rev 7 (v6l)
BogoMIPS: 2.00
Features: half thumb fastmult vfp edsp java tls
CPU implementer: 0x41
CPU architecture: 7
CPU variant: 0x0
CPU part: 0xb76
CPU revision: 7

Hardware: BCM2708
Revision: 0002

运行 gdb下的代码显示问题:

(gdb) b main
Breakpoint 1 at 0x10424: file float.s, line 16.
(gdb) r
Starting program: /home/pi/a.out 

Breakpoint 1, main () at float.s:16
16          VLDR S0, [R0]           @ load the value into the VFP register
(gdb) p /x $r0
 = 0x2064e
(gdb) n

Program received signal SIGBUS, Bus error.
main () at float.s:18
18          LDR R0, =val2           @ load variable address

SIGBUS 被引发,因为地址 0x2064e 不是 4 字节对齐的。

val1val2 不是 4 字节对齐的,因为它们的声明遵循长度不是 4 的倍数的字符串。
解决此问题的一种方法是将 float 声明移动到字符串之前,如下所示:

.data
val1:           .float      3.14159
val2:           .float      0.100
result_str:     .asciz      "Multiplication result = %f \n"
exit_str:       .ascii      "Terminating program.\n"

通过此修改 运行 程序打印:

Multiplication result = 0.314159
Terminating program.