分享
 
 
 

Linux汇编指南

王朝system·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

摘要: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:

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.

/* 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 */

assembly instructions

This code may be assembled with the following command:

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

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:

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.

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:

gdb ./a.out

gdb should start with the following message:

[bjorn@pomade src]$ gdb ./a.out

GNU gdb 4.17

Copyright 1998 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you are

welcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.

There is absolutely no warranty for GDB. Type "show warranty" for details.

This GDB was configured as "i386-redhat-linux"...

(gdb)

The "l" command will list the program sourcecode.

(gdb) l

1 /* linux version of AVTEMP.ASM CS 200, fall 1998 */

2 .data /* beginning of data segment */

3

4 /* hi_temp data item */

5 .type hi_temp,@object /* declare as data object */

6 .size hi_temp,1 /* declare size in bytes */

7 hi_temp:

8 .byte 0x92 /* set value */

9

10 /* lo_temp data item */

(gdb)

The first thing to do is set a breakpoint so it will be possible to step through the code.

(gdb) break main

Breakpoint 1 at 0x80480f7

(gdb)

This sets a breakpoint at the beginning of main. Now run the program.

(gdb) run

Starting program: /home/bjorn/src/./a.out

Breakpoint 1, main () at average.s:31

31 movb hi_temp,%al

Current language: auto; currently asm

(gdb)

values in registers can be checked with either "info registers"

(gdb) info registers

eax 0x8059200 134582784

ecx 0xbffffd94 -1073742444

edx 0x0 0

ebx 0x8097bf0 134839280

esp 0xbffffdd8 0xbffffdd8

ebp 0xbffffdd8 0xbffffdd8

esi 0x1 1

edi 0x8097088 134836360

eip 0x80480f7 0x80480f7

eflags 0x246 582

cs 0x23 35

ss 0x2b 43

ds 0x2b 43

es 0x2b 43

fs 0x2b 43

gs 0x2b 43

(gdb)

...or "p/x $eax" which prints the value in the EAX register in hex. The "e"in front of the register name indicates a 32 bit register. The Intel x86 family has included "extended" 32 bit registers since the 80386. These E registers are to the X registers as the L and H are to the X registers.Linux also uses a "flat" and protected memory model rather that segmentation,thus the EIP stores the entire current address.

(gdb

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有