Linux下的汇编语言编程

来源:互联网 发布:中国的政治体系 知乎 编辑:程序博客网 时间:2024/05/28 22:11
/* linux version of AVTEMP.ASM CS 200, fall 1998 */
.data /* beginning of data segment */

/* hi_temp data item */
.type hi_temp,@object /* declare as data object */
.size hi_temp,1 /* declare size in bytes */
hi_temp:
.byte 0x92 /* set value */

/* lo_temp data item */
.type lo_temp,@object
.size lo_temp,1
lo_temp:
.byte 0x52

/* av_temp data item */
.type av_temp,@object
.size av_temp,1
av_temp:
.byte 0

/* segment registers set up by linked code */
/* beginning of text(code) segment */
.text
.align 4 /* set 4 double-word alignment */
.globl main /* make main global for linker */
.type main,@function /* declare main as a function */
main:
pushl %ebp/* function requirement */
movl %esp,%ebp /* function requirement */
movb hi_temp,%al
addb lo_temp,%al
movb $0,%ah
adcb $0,%ah
movb $2,%bl
idivb %bl
movb %al,av_temp
leave/* function requirement */
ret/* function requirement */



This code may be assembled with the following command:


as -a --gstabs -o average.o average.s

The link command is the following:

ld -m elf_i386 -static /usr/lib/crt1.o /usr/lib/crti.o -lc average.o /usr/lib/crtn.o

"-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.


The object file (average.o) can then be linked to the Linux wrapper codein order to create an executable. These files are crt1.o, crti.o andcrtn.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
原创粉丝点击