Linux汇编指南Introduction The following is designed to be a Linux equivalent to "Developing Assembly Language Programs on a PC" by Douglas V. Hall. This tutorial requires the following: an i386 family PC running Linux as, the GNU assembler (included with any gcc installation) ld, the GNU linker (also included with gcc) gdb, the GNU debugger The tutorial was developed on a 5.1 Redhat Linux installation running a 2.0.34 version kernel and the version 5 and 6 C language libraries with ELF file format. But I have tried to make the tutorial as general possible with respect to Linux systems. I highly recommend working through this tutorial with "as" and "gdb" documentation close at hand. Overview The process of developing an assembly program under linux is somewhat different from development under NT. In order to accommodate object oriented languages which require the compiler to create constructor and destructor methods which execute before and after the execution of "main", the GNU development model embeds user code within a wrapper of system code. In other words, the user's "main" is treated as a function call. An advantage of this is that user is not required to initialize segment registers, though user code must obey some function requirements. The Code The following is the Linux version of the average temperature program. It will be referred to as "average.s". Note: Assembly language programs should use the ".s" suffix.
assembly instructions This code may be assembled with the following command:
The "-a" option prints a memory listing during assembly. This output gives the location variables and code with respect to the beginnings of the data and code segments. "--gstabs" places debugging information in the executable (used by gdb). "-o" specifies average.o as the output file name (the default is a.out, which is confusing since the file is not executable.) The object file (average.o) can then be linked to the Linux wrapper code in order to create an executable. These files are crt1.o, crti.o and crtn.o. crt1.o and crti.o provide initialization code and crtn.o does cleanup. These should all be located in "/usr/lib" be may be elsewere on some systems. They, and their source, might be located by executing the following find command: find / -name "crt*" -print The link command is the following:
"-m elf_i386" instructs the linker to use the ELF file format. "-static" cause static rather than dynamic linking to occur. And "-lc" links in the standard c libraries (libc.a). It might be necessary to include "-I/libdirectory" in the invocation for ld to find the c library. It will be necessary to change the mode of the resulting object file with "chmod +x ./a.out". It should now be possible to execute the file. But, of course, there will be no output. I recommend placing the above commands in a makefile . debugging The "--gstabs" option given to the assembler allows the assembly program to be debugged under gdb. The first step is to invoke gdb: 上一篇:Uuix汇编语言简介 下一篇:和luster一起学习在linux下使用汇编语言(1) 更多相关文章
|
推荐文章
精彩文章
|