Qt C++ Project with non-inline assembly

吃可爱长大的小学妹 提交于 2019-12-14 03:52:37

问题


I am using Windows XP, and Newest Qt Creator with QtSDK and the built-in gcc compiler.

The question is, how to use full assembly in a C++ Qt Project. I know how to use inline assembly, but I don't know how to do non-inline(written in a separate .asm file) full assembly, in a Qt C++ project.

Is this possible with a Qt C++ project, and if so, how?

EDIT:

*pro file

TEMPLATE = app
CONFIG += console
CONFIG -= qt

SOURCES += \
    calc.S

calc.S

section .data
        hello: db 'Hello world!', 10
        helloLen: equ $-hello

section .text
        global _start

_start:
        mov eax, 4
        mov ebx, 1
        mov ecx, hello
        mov edx, helloLen
        int 80h

proexit:
        mov eax, 1
        mov ebx, 0
        int 80h

Compile errors

..\plain_cpp\calc.S: Assembler messages:
..\plain_cpp\calc.S:1: Error: no such instruction: `section .data'
..\plain_cpp\calc.S:2: Error: no such instruction: `db 72ello world!4410'
..\plain_cpp\calc.S:3: Error: no such instruction: `equ $-hello'
..\plain_cpp\calc.S:5: Error: no such instruction: `section .text'
..\plain_cpp\calc.S:6: Error: no such instruction: `global _start'
..\plain_cpp\calc.S:9: Error: too many memory references for `mov'
..\plain_cpp\calc.S:10: Error: too many memory references for `mov'
..\plain_cpp\calc.S:11: Error: too many memory references for `mov'
..\plain_cpp\calc.S:12: Error: too many memory references for `mov'
..\plain_cpp\calc.S:13: Error: junk `h' after expression
..\plain_cpp\calc.S:13: Error: suffix or operands invalid for `int'
..\plain_cpp\calc.S:16: Error: too many memory references for `mov'
..\plain_cpp\calc.S:17: Error: too many memory references for `mov'
..\plain_cpp\calc.S:18: Error: junk `h' after expression
..\plain_cpp\calc.S:18: Error: suffix or operands invalid for `int'

EDIT 2 - AT&T Style

PRO File

TEMPLATE = app
CONFIG += console
CONFIG -= qt

SOURCES += \
    calc.S

calc.S

.data
hello:
    .string "Hello World\n"

.globl  main
main:
    movl $4, %eax
    movl $1, %ebx
    movl $hello,%ecx
    movl $12,%edx
    int $0x80

    ret

ERRORS

undefined reference to `WinMain@16'
collect2: ld returned 1 exit status

回答1:


While @karlphillip method correct in general, you should take in mind some windows specified stuff:

  1. While you compile @karlphillip's code you get linker error produced by MinGw standard library file:

    c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../libmingw32.a(main.o): In function `main':
    C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to `WinMain@16'
    

    That is not your main, that is libmingw32.a's main. And it expects WinMain as entry point of your code. The thing is that WinMain is default entry point for windows application defined by Microsoft. libmingw32.a defines actual main, that is called from actual entry point. That main does some stuff and than calls WinMain. But you have no WinMain defined, obviously.

    In this simple example you needn't standard library actually, then you can drop standard library and compile above code with command line

    gcc -Wl,-subsystem,console 1.S -nostdlib
    

    It should be equivalent Qt setting QMAKE_CXXFLAGS+=-nostdlib

    Then code above compiles, and... segfaults. Then go to point 2:

  2. int 80h is specific linux system call. Seems it doesn't work on windows. You should invoke WriteConsole on windows to write to stdin. But as a "proof of concept" you can run the following code:

    .text
    .globl  main
    main:
        movl $1, %eax
    ret
    

    This will set exit code of program to 1.

EDIT If you want Hello world example, compiled with standard library included, you can try this:

.data
hello:
    .string "Hello World\n"

.text
.global _WinMain@16
_WinMain@16:
    push $hello
    call _puts
    add $4, %esp

    ret

Compile with gcc 1.S




回答2:


I suggest you read this tutorial to properly setup Qt Creator for assembly.

EDIT:

Your problem is that qmake will call gcc to compile your assembly code, and you are using Intel Syntax. You need to convert your assembly code to use AT&T Syntax:

calc.S:

.data
hello:
    .string "Hello World\n"

.globl  main
main:
    movl $4, %eax
    movl $1, %ebx
    movl $hello,%ecx
    movl $12,%edx
    int $0x80

    ret

calc.pro:

TEMPLATE = app
CONFIG += console
CONFIG -= qt

SOURCES += \
    calc.S

Paste these 2 files in the same directory then execute qmake , followed by make.

Output:

$ ./calc 
Hello World



回答3:


Have you tried SOURCES += yourfile.asm in your *.pro file?



来源:https://stackoverflow.com/questions/9727831/qt-c-project-with-non-inline-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!