代码之家  ›  专栏  ›  技术社区  ›  a_sid

自动生成“Hello World!”的程序集代码。“不基于Windows 10上的NetBeans 8.1构建

  •  0
  • a_sid  · 技术社区  · 6 年前

    我无法在NetBeans 8.1上构建和运行程序集代码。但是,我能够正确地构建和运行我的C代码。

    我的基于C的工具的详细信息如下:

    enter image description here

    我首先选择开发一个C应用程序:

    enter image description here

    我创建了一个名为 Assembly :

    enter image description here

    我试图复制 Darius' link . 我有一个 main.c 我在其中键入的文件 printf("Hello World\n"); 它起作用了。

    enter image description here

    然后我创造了 main.s 通过键入 gcc -S main.c 在Windows命令提示符中创建了main.s。我试图构建它,但失败了,输出如下:

    cd 'C:\Users\XXXX\Documents\NetBeansProjects\AssemblyProject\Assembly_test'
    C:\cygwin64\bin\make.exe -f Makefile CONF=Debug clean
    "/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
    make[1]: Entering directory '/cygdrive/c/Users/XXXX/Documents/NetBeansProjects/AssemblyProject/Assembly_test'
    rm -f -r build/Debug
    rm -f dist/Debug/Cygwin-Windows/assembly_test.exe
    make[1]: Leaving directory '/cygdrive/c/Users/XXXX /Documents/NetBeansProjects/AssemblyProject/Assembly_test'
    
    CLEAN SUCCESSFUL (total time: 533ms)
    cd 'C:\Users\XXXX\Documents\NetBeansProjects\AssemblyProject\Assembly_test'
    C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
    "/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
    make[1]: Entering directory '/cygdrive/c/Users/XXXX/Documents/NetBeansProjects/AssemblyProject/Assembly_test'
    "/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/assembly_test.exe
    make[2]: Entering directory '/cygdrive/c/Users/XXXX/Documents/NetBeansProjects/AssemblyProject/Assembly_test'
    mkdir -p build/Debug/Cygwin-Windows
    as  -g -o build/Debug/Cygwin-Windows/main.o main.s
    mkdir -p dist/Debug/Cygwin-Windows
    gcc     -o dist/Debug/Cygwin-Windows/assembly_test build/Debug/Cygwin-Windows/main.o 
    build/Debug/Cygwin-Windows/main.o:main.s:21:(.stab+0x14): relocation truncated to fit: R_X86_64_32 against `.text'
    build/Debug/Cygwin-Windows/main.o:main.s:23:(.stab+0x20): relocation truncated to fit: R_X86_64_32 against `.text'
    build/Debug/Cygwin-Windows/main.o:main.s:26:(.stab+0x2c): relocation truncated to fit: R_X86_64_32 against `.text'
    build/Debug/Cygwin-Windows/main.o:main.s:26:(.stab+0x38): relocation truncated to fit: R_X86_64_32 against `.text'
    build/Debug/Cygwin-Windows/main.o:main.s:26:(.stab+0x44): relocation truncated to fit: R_X86_64_32 against `.text'
    build/Debug/Cygwin-Windows/main.o:main.s:26:(.stab+0x50): relocation truncated to fit: R_X86_64_32 against `.text'
    build/Debug/Cygwin-Windows/main.o:main.s:26:(.stab+0x5c): relocation truncated to fit: R_X86_64_32 against `.text'
    build/Debug/Cygwin-Windows/main.o:main.s:26:(.stab+0x68): relocation truncated to fit: R_X86_64_32 against `.text'
    build/Debug/Cygwin-Windows/main.o:main.s:26:(.stab+0x74): relocation truncated to fit: R_X86_64_32 against `.text'
    build/Debug/Cygwin-Windows/main.o:main.s:26:(.stab+0x80): relocation truncated to fit: R_X86_64_32 against `.text'
    build/Debug/Cygwin-Windows/main.o:main.s:26:(.stab+0x8c): additional relocation overflows omitted from the output
    collect2: error: ld returned 1 exit status
    make[2]: *** [nbproject/Makefile-Debug.mk:63: dist/Debug/Cygwin-Windows/assembly_test.exe] Error 1
    make[2]: Leaving directory '/cygdrive/c/Users/XXXX
    /Documents/NetBeansProjects/AssemblyProject/Assembly_test'
    make[1]: *** [nbproject/Makefile-Debug.mk:59: .build-conf] Error 2
    make[1]: Leaving directory '/cygdrive/c/Users/XXXX/Documents/NetBeansProjects/AssemblyProject/Assembly_test'
    make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2
    
    BUILD FAILED (exit value 2, total time: 1s)
    

    enter image description here

    enter image description here

    编辑:

    我的装配工的详细情况如下:

    C:\Users\XXXXX>as --version
    
    GNU assembler (GNU Binutils) 2.29.1.20171006 
    Copyright (C) 2017 Free Software Foundation, Inc. 
    This program is free software; you may redistribute it under the terms of 
    the GNU General Public License version 3 or later. 
    This program has absolutely no warranty. 
    This assembler was configured for a target of `x86_64-pc-cygwin'.
    

    编辑:

    打开cygwin的命令提示符并运行编译和运行程序的命令后,我观察到可以运行汇编代码和C代码。

    enter image description here

    编辑:

    我创建了一个新项目,使用 GCC-S主.C 创建文件 主要的 它无法建立在NetBeans上(同样的截断错误也导致了失败)。 我发现使用这个命令更让人困惑 gcc main.s -o MAIN 在Windows命令提示下,创建一个运行良好的可执行文件(main.exe)(从命令行) . 因此,这表明我的自动生成的程序集代码只能在netbeans环境中构建。

    有人能解释一下这种行为的原因吗?

    另外,Ramhound指出我的makefile没有指向正确的位置。如这个问题的第一张图片所示,我选择了自动创建自己的makefile的项目选项。此外,makefile允许我的C代码完美地构建和运行。只有当我尝试在NetBeans上构建汇编程序时,问题才会出现。 我查看了大流士的链接,没有提到仅仅为了汇编程序修改makefile。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Peter Cordes    6 年前

    部分的屏幕截图 main.s 不显示32位绝对寻址的任何使用。您的错误消息包括 main.s:21:(.stab+0x14): relocation truncated to fit: R_X86_64_32 against .text`,但是第21行的指令是一个与RIP相关的lea,因此它应该使用x86_64_PC32重新定位。

    你的GCC正在制定与职位无关的代码。

    但错误提到 .stab 因此,问题可能是使用32位绝对地址的过时调试格式。也许您已经将NetBeans设置为编译/组装 -gstabs 或者别的什么。在ELF目标上,默认格式为 -g 是矮人,但我喜欢窗户。

    无论如何,通过关闭netbeans中的调试符号来测试这个理论,看看错误是否消失。如果是这样,您就知道生成调试信息是问题所在。

        2
  •  1
  •   gone    6 年前

    您试图运行的程序集使用Linux系统调用,不能在Windows系统上运行。

    Windows没有稳定的系统调用接口,因此用纯程序集编程是不可取的。