【问题标题】:How to compile in 32 bit assembly?如何在 32 位汇编中编译?
【发布时间】:2020-02-17 21:18:21
【问题描述】:

嘿,我在我的 c 代码中使用了 32 汇编的函数。我用 nasm 和 gcc 编译它。

nasm -f coff array1.asm
gcc -o array1 array1.o array1c.c

我总是得到错误:

A
array1c.c:9:1: warning: ‘cdecl’ attribute ignored [-Wattributes]
 int PRE_CDECL asm_main( void ) POST_CDECL;
 ^~~
array1c.c:10:1: warning: ‘cdecl’ attribute ignored [-Wattributes]
 void PRE_CDECL dump_line( void ) POST_CDECL;
 ^~~~
/usr/bin/ld: i386-Architektur der Eingabedatei »array1.o« ist inkompatibel zur Ausgabe i386:x86-64
collect2: error: ld returned 1 exit status

这是我的代码:

/*
 * Driver file for array1.asm file
 */

#include <stdio.h>

#include "cdecl.h"

int PRE_CDECL asm_main( void ) POST_CDECL;
void PRE_CDECL dump_line( void ) POST_CDECL;

int main()
{
  int ret_status;
  ret_status = asm_main();
  return ret_status;
}

/*
 * function dump_line
 * dumps all chars left in current line from input buffer
 */
void dump_line()
{
  int ch;

  while( (ch = getchar()) != EOF && ch != '\n')
    /* null body*/ ;
}

【问题讨论】:

  • 使用gcc -m32
  • 请注意,cdecl 是默认调用约定,因此无论如何都不需要设置 cdecl
  • 为什么在汇编输出中使用coff二进制格式。 ELF二进制格式有什么问题吗?

标签: c windows gcc assembly x86


【解决方案1】:

您应该使用 gcc -m32 将代码编译为 32 位,如下所示:

nasm -f coff array1.asm
gcc -m32 -o array1 array1.o array1c.c

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-22
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 2011-11-04
    相关资源
    最近更新 更多