【问题标题】:Getting bad instructions error when compiling the following code in assembly在汇编中编译以下代码时出现错误指令错误
【发布时间】:2018-07-19 00:09:18
【问题描述】:

我目前正在研究树莓派,但我的汇编代码遇到了问题。当我尝试使用以下命令运行它时:

     as button.o button.s

在终端中。出现以下错误:

Assembler messages:
Error: can't open button.o for reading: No such file or directory
button.s:6: Error: bad instruction `errmsg .asciz "Setup didn't work... 
Aborting...\n"'
button.s:7: Error: bad instruction `pinup .int 3'
button.s:8: Error: bad instruction `pinleft .int 14'
button.s:9: Error: bad instruction `pindown .int 0'
button.s:10: Error: bad size 0 in type specifier
button.s:10: Error: bad instruction `pinright.int 7'
button.s:11: Error: bad instruction `pinpau .int 15'
button.s:12: Error: bad instruction `pinqu .int 2'
button.s:32: Error: bad instruction `blwiringpisetup'
button.s:48: Error: bad arguments to instruction -- `mov r7#1'
button.s:50: Error: bad instruction `be done'

我不确定这是语法错误还是一般代码有问题。 代码如下:

//data Section

        .data
        .balign 4
Intro:  .asciz  "Raspberry Pi - Blinking led test inassembly\n"
ErrMsg  .asciz  "Setup didn't work... Aborting...\n"
pinUp   .int    3
pinLeft .int    14
pinDown .int    0
pinRight.int    7
pinPau  .int    15
pinQu   .int    2
i:  .int    0


//Code section

    .text
    .global main
    .extern printf
    .extern wiringPiSetup
    .extern delay
    .extern digitalRead
    .extern pinMode

main:   push    {ip, lr}
// printf message
    ldr r0, =Intro
    bl  printf

//Check for setup error
    blwiringPiSetup
    mov     r1,#-1
    cmp r0, r1
    bne init
    ldr r0, =ErrMsg
    bl  printf
    b   done
init:
    ldr r0, =pinUp
    ldr r1, =pinLeft
    ldr r2, =pinDown
    ldr r3, =pinRight
    ldr r4, =pinPau
    ldr r5, =pinQu

    mov r6, #1
    mov r7  #1
    cmp r6, r7
    be  done

done:
    pop {ip,pc}

以下代码的后续变量声明:

//---------------------------------------
//  Data Section
// ---------------------------------------




          .data
          .balign 4 
Intro:   .asciz  "Raspberry Pi - Blinking led test in assembly\n"
ErrMsg:  .asciz "Setup didn't work... Aborting...\n"
pin:     .int   0
i:       .int   0
delayMs: .int   1000
OUTPUT   =      1

任何帮助将不胜感激。

【问题讨论】:

  • 您可以在使用之前清除您的寄存器吗?您确定 r7 还没有包含值吗?你大声使用 r7 吗?
  • 也可能是这个button.o的问题。
  • 当我尝试清除寄存器 r6 和 r7 时,我得到:button.s:47: Error: bad instruction clr r6' button.s:48: Error: bad instruction clr r7'
  • 虽然不是你的问题我认为你的意思是as -o button.o button.s -o 后面是输出文件名。
  • 我第一次查看代码时错过了它,但是大多数标签名称没有以冒号结尾。例如 ErrMsg .asciz "Setup didn't work... Aborting...\n" 应该是 ErrMsg: .asciz "Setup didn't work... Aborting...\n" 。您在大多数标签上都犯了类似的错误。当您定义 Introi 时,您确实得到了正确的

标签: assembly raspberry-pi3 gpio instructions wiringpi


【解决方案1】:

您忘记了标签名称后的冒号,因此解析器将它们视为指令助记符。 ErrMsg: 而不是 ErrMsg

另外,只读数据通常放在.rodata,而不是.data。如果您希望它靠近其他东西,您可以将它放在.data 中,这样您就可以从一个基地址全部寻址,而不是为每个标签使用完全独立的ldr。 (您可以使用add 在寄存器中生成地址,而不是加载单独的文字。)

.section .rodata
  @ .balign 4   @ why align before strings?  Normally you'd pad for alignment *after*, unless the total string length is a multiple of 4 bytes or something.
  Intro:  .asciz  "Raspberry Pi - Blinking led test inassembly\n"
  ErrMsg:  .asciz  "Setup didn't work... Aborting...\n"

.data
    .balign 4
    pinUp:    .int    3
    pinLeft:  .int    14
    pinDown:  .int    0     @ zero-int vars could be placed in the .bss
    pinRight: .int    7
    pinPau:   .int    15
    pinQu:    .int    2

    i:  .int    0     @ do you really need / want  static int i  in your program?
                      @ Use a register for a loop counter.

【讨论】:

  • 令人惊讶的是,谷歌搜索错误消息,或者 + labelcolon 没有找到任何重复项。所以我想这是值得回答的,以防将来其他人也有同样的错误。
猜你喜欢
  • 2011-06-11
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多