アセンブリ言語ソースファイルから生成された2つのオブジェクトファイルと、Cソースファイルから生成された2つのオブジェクトファイルのリンクに問題があります。Cオブジェクトファイルをアセンブリ言語オブジェクトファイルにリンクする方法は?
Cソースコード:
//main2.c
extern int strlength(char *);
int main(){
char * test = "hello";
int num = strlength(test);
return num;
}
アセンブリソースコード:
#strlength.s
.include "Linux32.s"
.section .text
.globl strlength
.type strlength, @function
strlength:
pushl %ebp
movl %esp, %ebp
movl $0, %ecx
movl 8(%ebp), %edx
read_next_byte:
movb (%edx), %al
cmpb $END_OF_FILE, %al
jle end
incl %edx
incl %ecx
jmp read_next_byte
end:
movl %ecx, %eax
popl %ebp
ret
私はコンパイルして、このように 'GCC' を使用して実行すると:
gcc main2.c strlength.s -m32 -o test
./test
echo $?
私は5を取得しています正しい。しかし、私はコンパイル時に/別途組み立てた後、このように「LD」 にリンク:私はセグメンテーションフォールトを取得
as strlength.s --32 -o strlength.o
cc main2.c -m32 -o main2.o
ld -melf_i386 -e main main2.o strlength.o -o test
./test
。これを引き起こしているのは何ですか? Cの呼び出し規約に100%正しく従っていませんか?