【问题标题】:Assembly error when compiling with GCC使用 GCC 编译时出现汇编错误
【发布时间】:2015-02-03 10:54:38
【问题描述】:

使用此命令编译 .s 文件时出现“没有这样的指令”错误:

$ gcc -s -o scall scall.s
scall.s: Assembler messages:
scall.s:2: Error: no such instruction: `section '
scall.s:4: Error: no such instruction: `global _start'
scall.s:7: Error: unsupported instruction `mov'
scall.s:8: Error: unsupported instruction `mov'
scall.s:11: Error: operand size mismatch for `int'
scall.s:13: Error: no such instruction: `section .data'
scall.s:15: Error: no such instruction: `msglength .word 12'

这是文件的代码:

section .text
    global _start

_start:
    mov 4,%eax
    mov 1,%ebx
    mov $message,%ecx
    mov $msglength,%edx
    int  $0x80

section .data
   message: .ascii "Hello world!"
   msglength .word 12

我怎样才能摆脱错误?

【问题讨论】:

  • 为什么要调用gcc 来编译它? gcc 是 C 编译器,使用 as 或类似工具。
  • 这就是我们现在在学校使用的。
  • unsupported instruction 'mov' 没有意义。来自绝对地址的mov 是应该汇编的有效指令,它不是您想要的。最近的 gcc / binutils 只抱怨实际错误(section 和全局,以及在msglength 之后缺少的:

标签: c gcc assembly


【解决方案1】:

我认为下面的代码会编译(“gcc”可以编译.s和.S文件并默认将它们与C库链接,但“as”做同样的事情并且不要将代码与C库链接) 如:

.section .text
    .global _start
_start:
    mov $4,%eax
    mov $1,%ebx
    mov $message,%ecx
    mov msglength,%edx
    int  $0x80

    mov $1, %eax
    mov $0, %ebx
    int $0x80
.section .data
    message: .ascii "Hello world!"
    msglength: .word 12

gcc:

.section .text
    .global main
main:
    mov $4,%eax
    mov $1,%ebx
    mov $message,%ecx
    mov msglength,%edx
    int  $0x80

    mov $1, %eax
    mov $0, %ebx
    int $0x80
.section .data
    message: .ascii "Hello world!"
    msglength: .word 12

【讨论】:

  • 是的,消除了 no 这样的错误,但我得到了 In function _start': (.text+0x0): multiple definition of _start' /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux -gnu/crt1.o(.debug_info): 重定位 0 的符号索引 11 无效
  • 如果你使用 gcc,你必须使用 main 而不是 _start。
  • 谢谢,成功了。当我现在运行它时,虽然我得到了分段错误(核心转储)。我指的是不存在的东西吗?
  • 我认为您必须将以下行 mov $msglength, %edx 更正为 mov msglength, %edx
  • 不,我仍然遇到同样的错误。也许我应该为此创建一个新线程,否则模组会发疯。
【解决方案2】:

如下更正,用-c param gcc -c test.s -o test编译

.text

_start:
.global main

main: 
    mov 4,%eax
    mov 1,%ebx
    mov $message,%ecx
    mov $msglength,%edx
    int  $0x80

.data
   message: .ascii "Hello world!"
   msglength: .word 12

【讨论】:

  • -c 的正确扩展名是 .o。您只是在没有链接的情况下组装。而且您没有修复 asm 中的任何错误,而是将 OP 的 _start 更改为 main:使用 gcc -m32 -nostdlib -static test.s -o test 组装+链接 32 位静态可执行文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多