当前位置: 技术问答>linux和unix
急啊!mingw gcc 3.4.5 下,在C语言main函数中,调用汇编程序中定义的函数始终出错!
来源: 互联网 发布时间:2016-05-08
本文导语: 小弟,用GCC 3.4.5定义了一个main函数,并在这个main函数中调用两个在汇编文件中定义的函数,但是始终给出以下错误信息(环境:windowsxp + eclipse + cdt + mingw) as -osrcfunction.o ..srcfunction.s ..srcfunction.s: Assembler messa...
小弟,用GCC 3.4.5定义了一个main函数,并在这个main函数中调用两个在汇编文件中定义的函数,但是始终给出以下错误信息(环境:windowsxp + eclipse + cdt + mingw)
as -osrcfunction.o ..srcfunction.s
..srcfunction.s: Assembler messages:
..srcfunction.s:0: Warning: end of file not at end of a line; newline inserted
..srcfunction.s:4: Error: bad or irreducible absolute expression
..srcfunction.s:4: Error: junk at end of line, first unrecognized character is `,'
..srcfunction.s:5: Error: bad or irreducible absolute expression
..srcfunction.s:5: Error: junk at end of line, first unrecognized character is `,'
..srcfunction.s:6: Warning: unexpected storage class 0
Build error occurred, build is stopped
main.c源代码:
#include
#include
extern void mywrite(int,char *,int );
extern int myadd(int ,int ,int *);
int main(){
char buf[1024];
int a,b,res;
char *mystr = "Calculating...n";
char * emsg = "Error in adding n";
a = 5;b = 10;
mywrite(1,mystr,strlen(mystr));
if(myadd(a,b,&res)){
sprintf(buf,"The result is %dn",res);
mywrite(1,buf,strlen(buf));
}else{
mywrite(1,emsg,strlen(emsg));
}
return 0;
}
function.s源代码:
SYSWRITE = 4
.global mywrite,myadd
.def
.type mywrite ,@function
.type myadd,@function
.endef
.text
mywrite:
pushl %ebp
movl %esp,%ebp
pushl %ebx
movl 8(%ebp),%ebx
movl 12(%ebp),%ecx
movl 16(%ebp),%edx
movl $SYSWRITE,%eax
int $0x80
popl %ebx
movl %ebp,%esp
popl %ebp
ret
myadd:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
movl 12(%ebp),%edx
xorl %ecx,%ecx
addl %eax,%edx
jo 1f
movl 16(%ebp),%eax
movl %edx,(%eax)
incl %ecx
1: movl %ecx,%eax
movl %ebp,%esp
popl %ebp
ret
makefile源代码:
makefile:
all: CallAssembleFunction
clean:
-rm main.o function.o CallAssembleFunction
function.o: function.s
as –o function.o function.s
as -v
CallAssembleFunction: main.o function.o
gcc –o CallAssembleFunction main.o function.o
main.o: main.c
gcc –o main.o main.c
as -osrcfunction.o ..srcfunction.s
..srcfunction.s: Assembler messages:
..srcfunction.s:0: Warning: end of file not at end of a line; newline inserted
..srcfunction.s:4: Error: bad or irreducible absolute expression
..srcfunction.s:4: Error: junk at end of line, first unrecognized character is `,'
..srcfunction.s:5: Error: bad or irreducible absolute expression
..srcfunction.s:5: Error: junk at end of line, first unrecognized character is `,'
..srcfunction.s:6: Warning: unexpected storage class 0
Build error occurred, build is stopped
main.c源代码:
#include
#include
extern void mywrite(int,char *,int );
extern int myadd(int ,int ,int *);
int main(){
char buf[1024];
int a,b,res;
char *mystr = "Calculating...n";
char * emsg = "Error in adding n";
a = 5;b = 10;
mywrite(1,mystr,strlen(mystr));
if(myadd(a,b,&res)){
sprintf(buf,"The result is %dn",res);
mywrite(1,buf,strlen(buf));
}else{
mywrite(1,emsg,strlen(emsg));
}
return 0;
}
function.s源代码:
SYSWRITE = 4
.global mywrite,myadd
.def
.type mywrite ,@function
.type myadd,@function
.endef
.text
mywrite:
pushl %ebp
movl %esp,%ebp
pushl %ebx
movl 8(%ebp),%ebx
movl 12(%ebp),%ecx
movl 16(%ebp),%edx
movl $SYSWRITE,%eax
int $0x80
popl %ebx
movl %ebp,%esp
popl %ebp
ret
myadd:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
movl 12(%ebp),%edx
xorl %ecx,%ecx
addl %eax,%edx
jo 1f
movl 16(%ebp),%eax
movl %edx,(%eax)
incl %ecx
1: movl %ecx,%eax
movl %ebp,%esp
popl %ebp
ret
makefile源代码:
makefile:
all: CallAssembleFunction
clean:
-rm main.o function.o CallAssembleFunction
function.o: function.s
as –o function.o function.s
as -v
CallAssembleFunction: main.o function.o
gcc –o CallAssembleFunction main.o function.o
main.o: main.c
gcc –o main.o main.c
|
7.95 .type
This directive is used to set the type of a symbol.
COFF Version
For COFF targets, this directive is permitted only within .def/.endef pairs. It is used like this:
.type int
This records the integer int as the type attribute of a symbol table entry.
ELF Version
For ELF targets, the .type directive is used like this:
.type name , type description
This sets the type of symbol name to be either a function symbol or an object symbol. There are five different syntaxes supported for the type description field, in order to provide compatibility with various other assemblers. The syntaxes supported are:
.type ,#function
.type ,#object
.type ,@function
.type ,@object
.type ,%function
.type ,%object
.type ,"function"
.type ,"object"
.type STT_FUNCTION
.type STT_OBJECT
在windows下用mingw生成coff格式的文件,因此
.type mywrite ,@function
.type myadd,@function
这两行根本不需要,可以注释掉
此外,你要在C文件中调用S文件里的函数,.s文件中的mywrite和myadd必须改为_mywrite和_myadd,即加上下划线。
This directive is used to set the type of a symbol.
COFF Version
For COFF targets, this directive is permitted only within .def/.endef pairs. It is used like this:
.type int
This records the integer int as the type attribute of a symbol table entry.
ELF Version
For ELF targets, the .type directive is used like this:
.type name , type description
This sets the type of symbol name to be either a function symbol or an object symbol. There are five different syntaxes supported for the type description field, in order to provide compatibility with various other assemblers. The syntaxes supported are:
.type ,#function
.type ,#object
.type ,@function
.type ,@object
.type ,%function
.type ,%object
.type ,"function"
.type ,"object"
.type STT_FUNCTION
.type STT_OBJECT
在windows下用mingw生成coff格式的文件,因此
.type mywrite ,@function
.type myadd,@function
这两行根本不需要,可以注释掉
此外,你要在C文件中调用S文件里的函数,.s文件中的mywrite和myadd必须改为_mywrite和_myadd,即加上下划线。