linux平台学x86汇编(二十):汇编库的使用
$ ls add.s libcal.a main.c Makefile prt.s sub.s $
add.s文件:
# add.s .type add, @function .globl add add: # add two integer pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax addl 12(%ebp), %eax movl %ebp, %esp popl %ebp ret .type add_inc, @function .globl add_inc add_inc: # add 1 to int parameter pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax inc %eax movl %ebp, %esp popl %ebp ret
sub.s文件:
# sub.s .type sub, @function .globl sub sub: # sub two integer pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax subl 12(%ebp), %eax movl %ebp, %esp popl %ebp ret .type sub_dec, @function .globl sub_dec sub_dec: # sub 1 to int parameter pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax dec %eax movl %ebp, %esp popl %ebp ret .type print_hello, @function .section .data msg: .ascii "hello world!\n" len=.-msg .section .text .global print_hello print_hello: movl $len, %edx movl $msg, %ecx movl $1, %ebx movl $4, %eax int $0x80 movl $0, %ebx movl $1, %eax int $0x80
# main.c #include <stdio.h> int add(int, int); int add_inc(int); int sub(int, int); int sub_dec(int); int print_hello(void); int main(int argc, const char *argv[]) { int ret; ret = add(1989, 711); printf("The add() return is %d.\n", ret); ret = add_inc(1989); printf("The add_inc() return is %d.\n", ret); ret = sub(1989, 711); printf("The sub() return is %d.\n", ret); ret = sub_dec(1989); printf("The sub_dec() return is %d.\n", ret); print_hello(); return 0; }
Makefile:
# Makefile for linux as CFLAGS= -Wall -g ASFLAGS= -gstabs SRC_BIN=target_bin SRC_LIB=libcal.a SRC_C=$(wildcard *.c) SRC_S=$(wildcard *.s) SRC_COBJ=$(SRC_C:.c=.o) SRC_SOBJ=$(SRC_S:.s=.o) SRC_OBJ=$(SRC_COBJ) $(SRC_SOBJ) all: $(SRC_BIN) $(SRC_BIN): $(SRC_COBJ) $(SRC_LIB) $(CC) -o $@ $(SRC_COBJ) -L./ -lcal $(SRC_LIB) : $(SRC_SOBJ) ar rcs $@ $^ clean: $(RM) $(SRC_OBJ) $(SRC_BIN) *~ .PHONY: all clean
cc -Wall -g -c -o main.o main.c
as -gstabs -o add.o add.s
as -gstabs -o prt.o prt.s
as -gstabs -o sub.o sub.s
ar rcs libcal.a add.o prt.o sub.o
cc -o target_bin main.o -L./ -lcal
$ ./target_bin
The add() return is 2700.
The add_inc() return is 1990.
The sub() return is 1278.
hello world!
$ ar t libcal.a add.o prt.o sub.o
$ nm -s libcal.a Archive index: add in add.o add_inc in add.o print_hello in prt.o sub in sub.o sub_dec in sub.o add.o: 00000000 T add 0000000d T add_inc prt.o: 0000000d a len 00000000 d msg 00000000 T print_hello sub.o: 00000000 T sub 0000000d T sub_dec
$ cat Makefile.static # Makefile for linux as CFLAGS= -Wall -g ASFLAGS= -gstabs SRC_BIN=target_bin SRC_LIB=libcal.a SRC_C=$(wildcard *.c) SRC_S=$(wildcard *.s) SRC_COBJ=$(SRC_C:.c=.o) SRC_SOBJ=$(SRC_S:.s=.o) SRC_OBJ=$(SRC_COBJ) $(SRC_SOBJ) all: $(SRC_BIN) $(SRC_BIN): $(SRC_COBJ) $(SRC_LIB) $(CC) -o $@ $(SRC_COBJ) -L./ -lcal $(SRC_LIB) : $(SRC_SOBJ) ar rcs $@ $^ clean: $(RM) $(SRC_OBJ) $(SRC_BIN) *~ .PHONY: all clean
$ make -f Makefile.share cc -Wall -g -c -o main.o main.c as -gstabs -o add.o add.s as -gstabs -o prt.o prt.s as -gstabs -o sub.o sub.s gcc -shared -o libcal.so add.o prt.o sub.o cc -o target_bin main.o -L./ -lcal
$ ldd target_bin linux-gate.so.1 => (0x00dac000) libcal.so => not found libc.so.6 => /lib/libc.so.6 (0x00a42000) /lib/ld-linux.so.2 (0x00a1c000)
$ ./target_bin ./target_bin: error while loading shared libraries: libcal.so: cannot open shared object file: No such file or directory
$ cat /etc/ld.so.conf include ld.so.conf.d/*.conf
$ ls /etc/ld.so.conf.d/ atlas-i386.conf kernel-2.6.32-358.el6.i686.conf mysql-i386.conf qt-i386.conf xulrunner-32.conf上面每个配置文件就是对于每个应用查找其对应共享库的路径。
# ./target_bin The add() return is 2700. The add_inc() return is 1990. The sub() return is 1278. hello world!本系列linux汇编学习到此为止,在这二十节的内容中,可能没有讲解到汇编的所有方面,但还是把汇编语言的比较常用的基本知识通过一些很简单很基础的示例展示了出来,我希望读者在看完本系列文章之后有所有收获,也希望读者在阅读文章是能指出其中的错误和不足。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。