在Cygwin中由GNU Assembler运行hello.s时出现分段错误

use*_*613 2 assembly cygwin gnu windows-7-x64

我在Cygwin中使用gcc(在Windows7-64位中)运行此示例 http://asm.sourceforge.net/howto/build.html

.data                   # section declaration
msg:
    .ascii  "Hello, world!\n"   # our dear string
    len = .-msg         # length of our dear string
.text                   # section declaration
            # we must export the entry point to the ELF linker or
    .global _start  # loader. They conventionally recognize _start as their
            # entry point. Use ld -e foo to override the default.
_start:
# write our string to stdout
    movl    $len,%edx   # third argument: message length
    movl    $msg,%ecx   # second argument: pointer to message to write
    movl    $1,%ebx     # first argument: file handle (stdout)
    movl    $4,%eax     # system call number (sys_write)
    int $0x80       # call kernel
# and exit
    movl    $0,%ebx     # first argument: exit code
    movl    $1,%eax     # system call number (sys_exit)
    int $0x80       # call kernel
Run Code Online (Sandbox Code Playgroud)

通过Cygwin中的命令行 $ as -o hello.o hello.s && ld -s -o hello hello.o && ./hello 结果是 Segmentation fault

你能帮助我吗?

小智 6

Cygwin不会使Windows与Linux二进制兼容,因此使用int $ 0x80的Linux系统调用将失败,因为Windows具有完全不同的系统调用约定.但是,生成的二进制文件应该在实际(或兼容)Linux环境中正常工作.