Assembly AT&;T语法

Assembly AT&;T语法,assembly,x86,floating-point,Assembly,X86,Floating Point,基本上,我所要做的就是用一个向量乘以一个矩阵,在汇编中使用浮点运算 我的初稿是这样的: .data mat: .float 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 vek: .float 1.0, 2.0, 3.0 res: .float 0.0, 0.0 .text .globl main main: ; here goes the magic call exit 不幸的是,我所做的一切都不起作用。我知道如何进行基本的算术运算(如fadd

基本上,我所要做的就是用一个向量乘以一个矩阵,在汇编中使用浮点运算

我的初稿是这样的:

  .data
  mat: .float 1.0, 2.0, 3.0, 4.0, 5.0, 6.0
  vek: .float 1.0, 2.0, 3.0
  res: .float 0.0, 0.0
  .text
  .globl main
  main:
  ; here goes the magic
  call exit
不幸的是,我所做的一切都不起作用。我知道如何进行基本的算术运算(如faddp、fmulp等),但我仍然无法有效地存储它
fstpl
似乎根本不起作用

有谁能给我一个大概的草图(不是关于如何进行矩阵-向量乘法)如何使用FPU命令以及如何将计算结果存储在寄存器中


提前感谢,

首先在C中实现它,测试它,然后使用
gcc-S
生成汇编程序源代码,然后按原样使用它,或者将它用作自己代码的模板

例如,这里是一个C代码实现,
mat_vec.C

#include <stdio.h>

int main(void)
{
    // note: use "volatile" qualifier for input data otherwise gcc will
    //       just optimise all the arithmetic away...
    volatile float mat[2][3] = { { 1.0f, 2.0f, 3.0f }, { 4.0f, 5.0f, 6.0f } };
    volatile float vek[3] = { 1.0f, 2.0f, 3.0f };
    float res[2] = { 0.0f, 0.0f };

    res[0] = mat[0][0] * vek[0] + mat[0][1] * vek[1] + mat[0][2] * vek[2]; 
    res[1] = mat[1][0] * vek[0] + mat[1][1] * vek[1] + mat[1][2] * vek[2]; 

    printf("res = { %g, %g }\n", res[0], res[1]);

    return 0;
}
看起来不错,所以让我们生成汇编程序源代码:

$ gcc -Wall -Os -m32 -march=i686 -S mat_vec.c -o mat_vec.S
$ cat mat_vec.S
    .cstring
LC6:
    .ascii "res = { %g, %g }\12\0"
    .text
.globl _main
_main:
    pushl   %ebp
    movl    $0x40000000, %ecx
    movl    %esp, %ebp
    movl    $0x40400000, %edx
    pushl   %esi
    movl    $0x40800000, %eax
    pushl   %ebx
    movl    $0x3f800000, %esi
    subl    $96, %esp
    movl    %esi, -44(%ebp)
    movl    %ecx, -40(%ebp)
    movl    %edx, -36(%ebp)
    movl    %eax, -32(%ebp)
    movl    $0x40a00000, %eax
    movl    %eax, -28(%ebp)
    movl    $0x40c00000, %eax
    movl    %eax, -24(%ebp)
    movl    %esi, -20(%ebp)
    movl    %ecx, -16(%ebp)
    movl    %edx, -12(%ebp)
    flds    -44(%ebp)
    flds    -20(%ebp)
    fstps   -72(%ebp)
    flds    -40(%ebp)
    flds    -16(%ebp)
    fstps   -68(%ebp)
    flds    -36(%ebp)
    fstps   -64(%ebp)
    flds    -12(%ebp)
    fstps   -60(%ebp)
    flds    -32(%ebp)
    flds    -20(%ebp)
    flds    -28(%ebp)
    flds    -16(%ebp)
    fxch    %st(3)
    fmulp   %st, %st(2)
    flds    -24(%ebp)
    flds    -12(%ebp)
    fxch    %st(2)
    fmulp   %st, %st(4)
    call    L3
"L00000000001$pb":
L3:
    popl    %ebx
    fmulp   %st, %st(1)
    fxch    %st(3)
    fmuls   -68(%ebp)
    fxch    %st(1)
    faddp   %st, %st(2)
    fxch    %st(3)
    fmuls   -72(%ebp)
    fxch    %st(1)
    faddp   %st, %st(2)
    fxch    %st(1)
    leal    LC6-"L00000000001$pb"(%ebx), %eax
    fstpl   12(%esp)
    flds    -64(%ebp)
    fmuls   -60(%ebp)
    fxch    %st(1)
    faddp   %st, %st(2)
    faddp   %st, %st(1)
    fstpl   4(%esp)
    movl    %eax, (%esp)
    call    L_printf$stub
    addl    $96, %esp
    xorl    %eax, %eax
    popl    %ebx
    popl    %esi
    leave
    ret
    .section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5
L_printf$stub:
    .indirect_symbol _printf
    hlt ; hlt ; hlt ; hlt ; hlt
    .subsections_via_symbols

您感兴趣的部分是在标签
L3

之前开始的,您有什么理由必须使用(旧版)
x87
说明吗?SSE更易于使用。
$ gcc -Wall -Os -m32 -march=i686 -S mat_vec.c -o mat_vec.S
$ cat mat_vec.S
    .cstring
LC6:
    .ascii "res = { %g, %g }\12\0"
    .text
.globl _main
_main:
    pushl   %ebp
    movl    $0x40000000, %ecx
    movl    %esp, %ebp
    movl    $0x40400000, %edx
    pushl   %esi
    movl    $0x40800000, %eax
    pushl   %ebx
    movl    $0x3f800000, %esi
    subl    $96, %esp
    movl    %esi, -44(%ebp)
    movl    %ecx, -40(%ebp)
    movl    %edx, -36(%ebp)
    movl    %eax, -32(%ebp)
    movl    $0x40a00000, %eax
    movl    %eax, -28(%ebp)
    movl    $0x40c00000, %eax
    movl    %eax, -24(%ebp)
    movl    %esi, -20(%ebp)
    movl    %ecx, -16(%ebp)
    movl    %edx, -12(%ebp)
    flds    -44(%ebp)
    flds    -20(%ebp)
    fstps   -72(%ebp)
    flds    -40(%ebp)
    flds    -16(%ebp)
    fstps   -68(%ebp)
    flds    -36(%ebp)
    fstps   -64(%ebp)
    flds    -12(%ebp)
    fstps   -60(%ebp)
    flds    -32(%ebp)
    flds    -20(%ebp)
    flds    -28(%ebp)
    flds    -16(%ebp)
    fxch    %st(3)
    fmulp   %st, %st(2)
    flds    -24(%ebp)
    flds    -12(%ebp)
    fxch    %st(2)
    fmulp   %st, %st(4)
    call    L3
"L00000000001$pb":
L3:
    popl    %ebx
    fmulp   %st, %st(1)
    fxch    %st(3)
    fmuls   -68(%ebp)
    fxch    %st(1)
    faddp   %st, %st(2)
    fxch    %st(3)
    fmuls   -72(%ebp)
    fxch    %st(1)
    faddp   %st, %st(2)
    fxch    %st(1)
    leal    LC6-"L00000000001$pb"(%ebx), %eax
    fstpl   12(%esp)
    flds    -64(%ebp)
    fmuls   -60(%ebp)
    fxch    %st(1)
    faddp   %st, %st(2)
    faddp   %st, %st(1)
    fstpl   4(%esp)
    movl    %eax, (%esp)
    call    L_printf$stub
    addl    $96, %esp
    xorl    %eax, %eax
    popl    %ebx
    popl    %esi
    leave
    ret
    .section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5
L_printf$stub:
    .indirect_symbol _printf
    hlt ; hlt ; hlt ; hlt ; hlt
    .subsections_via_symbols