Как я компилирую ассемблерные подпрограммы для использования с программой C (ассемблер GNU)?

Проверьте это:

<!DOCTYPE html>
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <style>
        .window {
            position: absolute;
            width: 100%;
            height: 100%;
            background: #c73030;
            z-index: 9999;
        }

        #box {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 1000px;
            height: 500px;
            background-color: #f7986c;
            z-index: -999;
        }

        #square {
            position: absolute;
            top: 0;
            width: 400px;
            height: 100%;
            background-color: cornflowerblue;
        }

        #btn {
            position: absolute;
            width: 300px;
            height: 70px;
            top: 80%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: #444;
        }
    </style>

</head>

<body>
    <div id="box">

        <div id="square">
            <button id="btn">
                <div id="sI">Sign In</div>
            </button>
        </div>

    </div>


    <script>
        var animated = false;

        function animate() {
            $('#square').animate({
                width: '600',
                right: '0'
            }, 300, 'swing');
            $('#square').animate({
                width: '400'
            }, 300, 'swing');
            $('#square').css("left", "");
            animated = true;
        }

        function reverseAnimate() {
            $('#square').animate({
                width: '600',
                left: '0'
            }, 300, 'swing');
            $('#square').animate({
                width: '400'
            }, 300, 'swing');
            $('#square').css("right", "");
            animated = false;
        }

        $('#btn').click(function() {
            if (!animated) { //If it has not been animated, animate!
                animate();
            } else {
                reverseAnimate();
            }
        });
    </script>
</body>

</html>

Я использовал jQuery и изменил несколько вещей, чтобы они выглядели немного как пример. Кроме того, мне понравилось ваше использование .reverse (), но я не использовал его в моем случае.

9
задан Mr. Shickadance 30 April 2009 в 20:57
поделиться

2 ответа

Based on the files in your question, I managed to compile it. I've changed both the file names and the file contents.

asm_const.h :

#define ASM_CONST_1    0x80
#define ASM_CONST_2    0xaf

asm_functions.h :

#include "asm_const.h"
unsigned char asm_foo( int, int, int );

asm_functions.S (the trailing S must be capital! #include needs it) :

#include "asm_const.h"
.section .text
.globl asm_foo
.type asm_foo, @function
asm_foo:
  mov $ASM_CONST_1, %eax
  /* asm code with proper stack manipulation for C calling conventions */
  ret

test_asm.c :

#include "asm_functions.h"
int main() {
  return asm_foo( 1, 2, 3);
}

Please note that you need the the assembly file extension .S with capital S. With .s, the .s file wouldn't be run through the preprocessor, thus #include wouldn't work, and you wouldn't be able to use ASM_CONST_1 in the .s file.

Compile with a single command:

gcc -o test_asm asm_functions.S test_asm.c

Or, as an alternative, compile with multiple commands, creating .o files:

gcc -c asm_functions.S
gcc -c test_asm.c
gcc -o test_asm asm_functions.o test_asm.o

The single-command gcc takes care of compiling the .S file using gas, the .c file with GCC's C compiler, and linking the resulting temporary .o files together using ld. gcc runs all those commands with the appropriate flags by default.

On some systems (but not on Linux with the default GCC installation) you have to prepend an underscore to the names of exported functions in the .S file (but not in the .c or .h files). So all instances of asm_foo would become _asm_foo only in the .S file.

15
ответ дан 4 December 2019 в 13:49
поделиться

Рассматривали ли вы использование встроенной сборки ? Это было бы намного проще, чем использовать файл на ассемблере.

Редактировать: Кроме того, причина, по которой вы получаете ошибки компоновщика, вероятно, в том, что компилятор C добавляет начальное подчеркивание на идентификаторы для фактических символов. Попробуйте добавить подчеркивание в свой файл сборки.

0
ответ дан 4 December 2019 в 13:49
поделиться
Другие вопросы по тегам:

Похожие вопросы: