【问题标题】:How to link assembly file(.s file) to a C program如何将程序集文件(.s 文件)链接到 C 程序
【发布时间】:2017-07-10 12:46:24
【问题描述】:

文件assem.s 包含汇编代码中的函数。 test.c 调用该函数,但出现以下错误:

mips-mti-elf-gcc -o test test.c assem.s
In file included from test.c:2:0:
assem.s:1:2: error: expected identifier or '(' before '.' token
  .text
  ^
assem.s:3:22: error: stray '@' in program
  .type asm_memcheck, @function
                      ^

assem.s: Assemble messages:
assem.s:6: Warning: used $at without ".set noat"
assem.s:38:  Warning: .end directive without a preceding .ent directive

我的源文件如下: test.c 请注意,局部变量 a、b、c 和 d 在程序中没有意义。主要用于观察寄存器中的值。

  1 #include<stdio.h>
  2 #include"assem.s"
  3
  4 extern void asm_memcheck();
  5
  6 int main()
  7 {
  8 int a=1;
  9 int b=2;
 10 int c=3;
 11 float d=4.0;
 12
 13 asm_memcheck();
 14
 15 }
 16

组装:

  1         .text
  2         .globl asm_memcheck
  3         .type   asm_memcheck, @function
  4 asm_memcheck:
  5         sw $zero,0xFFF00000
  6         sw $at,0xFFF00004
  7         sw $v0,0xFFF00008
  8         sw $v1,0xFFF0000C
  9         sw $a0,0xFFF00010
 10         sw $a1,0xFFF00014
 11         sw $a2,0xFFF00018
 12         sw $a3,0xFFF0001C
 13         sw $t0,0xFFF00020
 14         sw $t1,0xFFF00024
 15         sw $t2,0xFFF00028
 16         sw $t3,0xFFF0002C
 17         sw $t4,0xFFF00030
 18         sw $t5,0xFFF00034
 19         sw $t6,0xFFF00038
 20         sw $t7,0xFFF0003C
 21         sw $s0,0xFFF00040
 22         sw $s1,0xFFF00044
 23         sw $s2,0xFFF00048
 24         sw $s3,0xFFF0004C
 25         sw $s4,0xFFF00050
 26         sw $s5,0xFFF00054
 27         sw $s6,0xFFF00058
 28         sw $s7,0xFFF0005C
 29         sw $t8,0xFFF00060
 30         sw $t9,0xFFF00064
 31         sw $k0,0xFFF00068
 32         sw $k1,0xFFF0006C
 33         sw $gp,0xFFF00070
 34         sw $sp,0xFFF00074
 35         sw $fp,0xFFF00078
 36         sw $ra,0xFFF0007C
 37
 38         .end asm_memcheck
 39

【问题讨论】:

    标签: c assembly mips


    【解决方案1】:

    如果您只是想将其作为快速 hack 包含在内,您可以内联它。

    #include <stdio.h>
    
    int main()
    {
        int a=1;
        int b=2;
        int c=3;
        float d=4.0;
    
        asm("sw $zero,0xFFF00000\n\t"
            "sw $at,0xFFF00004\n\t"
            "sw $v0,0xFFF00008\n\t"
            "sw $v1,0xFFF0000C\n\t"
            "sw $a0,0xFFF00010\n\t"
            "sw $a1,0xFFF00014\n\t"
            "sw $a2,0xFFF00018\n\t"
            "sw $a3,0xFFF0001C\n\t"
            "sw $t0,0xFFF00020\n\t"
            "sw $t1,0xFFF00024\n\t"
            "sw $t2,0xFFF00028\n\t"
            "sw $t3,0xFFF0002C\n\t"
            "sw $t4,0xFFF00030\n\t"
            "sw $t5,0xFFF00034\n\t"
            "sw $t6,0xFFF00038\n\t"
            "sw $t7,0xFFF0003C\n\t"
            "sw $s0,0xFFF00040\n\t"
            "sw $s1,0xFFF00044\n\t"
            "sw $s2,0xFFF00048\n\t"
            "sw $s3,0xFFF0004C\n\t"
            "sw $s4,0xFFF00050\n\t"
            "sw $s5,0xFFF00054\n\t"
            "sw $s6,0xFFF00058\n\t"
            "sw $s7,0xFFF0005C\n\t"
            "sw $t8,0xFFF00060\n\t"
            "sw $t9,0xFFF00064\n\t"
            "sw $k0,0xFFF00068\n\t"
            "sw $k1,0xFFF0006C\n\t"
            "sw $gp,0xFFF00070\n\t"
            "sw $sp,0xFFF00074\n\t"
            "sw $fp,0xFFF00078\n\t"
            "sw $ra,0xFFF0007C");
    }
    

    如果您希望它是一个单独的文件,我认为您需要使用汇编程序,然后以某种方式链接结果。

    【讨论】:

      【解决方案2】:

      您看到的问题来自#include"assem.s" 行:C 编译器尝试读取汇编代码,因为它是 C 代码。

      所以,不要在你的 c 代码中包含 "assem.s"

      test.c

      #include<stdio.h>
      extern void asm_memcheck();
      
      int main()
      {
          int a=1;
          int b=2;
          int c=3;
          float d=4.0;
          asm_memcheck();
      }
      

      按照你之前写的那样构建:

      mips-mti-elf-gcc -o test test.c assem.s
      

      【讨论】:

        猜你喜欢
        • 2013-05-25
        • 1970-01-01
        • 2019-04-11
        • 2018-11-18
        • 1970-01-01
        • 2011-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多