Linux内核编程(一)-----------AT&T汇编语法格式

写在前面:又是一个新的系列,学习的过程就是成长的过程,重要的是坚持,当然跟之前一样,也是一边学习一边整理总结。用一段在某篇文章看到话来开始这个系列吧:那些跑得更远的人,都是因为别人放弃时,他们还在跑。 所以就这么一直勇敢地跑下去吧。 毕竟,你是一个,连放弃,也做不到的人啊。QQ:993650814

第一篇主要介绍几个常用的汇编语法,后面会用一个嵌入式汇编例子来讲解通常汇编在C中内嵌的用法。

 

正文:

1、寄存器的引用
   引用寄存器要在寄存器前加%,如 mov %eax , %ebx        //把寄存器eax中的内容放到ebx寄存器中去

2、操作数顺序: 从源(左)到目的(右),例如上述,eax是源,ebx是目的。

3、常数/立即数的格式
  立即数要在前面加$,如mov $4,%ebx
  符号常数直接引用,如 mov value, %ebx
  引用符号地址要在符号前加$,如 mov $value, %ebx

4、操作数的长度

  操作数的长度用加在指令后的符号表示,
  b(byte,1个字节) w(word,2个字节) l(long,4个字节),如 mov w %ax, %bx  //把ax寄存器的内容复制到bx寄存器中去,长度2Byte
  
5、嵌入式汇编语句: _asm_("asm statements":outputs:inputs:registers-modified); 
     asm statements: 加载的代码 汇编语句,可以有多条,会变语句后面用\n\t结束
     outputs:inputs:registers-modified :这三者都是可选项。
     outputs:输出寄存器
     inputs:输入寄存器,
     registers-modified:被修改的寄存器
     "a"、"b"、"c"、"d"分别表示eax、ebx、ecx、edx
     "S"和"D",代表esi、edi
     "r":任何寄存器
    "0":与上一个寄存器使用同一个寄存器
 

6、example:

   代码中对例子中的汇编每行语句都会进行详细分析:

int main(void)
{
	int a1 = 10,b1 = 0;
	
	_asm_("movl %1, %%eax;\\n\r"
	"movl %%eax,%%ecx;"
	:"=a"(b1)              
	:"b"(a1)
	:"%eax");
	
	printf("a1 = %d,b1 = %d\n",a1,b1);
	
/*
	这段汇编代码中,先看参数部分:
	1、输出部分:"=a"(b1)  输出的eax寄存器的内容给了b1;
	2、输入部分:"b"(a1)   把输入的a1放到了ebx寄存器中; 即 ebx = a1 =10;
	3、会改变的部分:"%eax"    eax寄存器会被改变
	再来看代码部分
	1、"movl %1, %%eax;\\n\r" 在这段代码中, %0代表 "=a"(b1),%1代表 "b"(a1),%2代表 "%eax",
		其他代码部分以此类推。
		所以这段代码的意思:将ebx的内容给了eax即eax的内容成了10;
	2、"movl %%eax,%%ecx;" :这段代码的意思:将eax寄存器的内容放到了ecx中。
	
	整体分析:
	输出的寄存器eax的内容是10;
	
	所以打印结果为:a1 = 10,b1 = 10
	
*/
	
	return 0;
}

另外,还有一处细节需要补充一下,代码中寄存器前面都有两个%%例如"movl %%eax,%%ecx;",为什么是两个%呢?

gcc编译器在编译的时候会自动拿掉一个%,而寄存器引用的时候需要%,所以这里寄存器前面都是两个%。

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
***请支持开源*** 汇编大师Richard Blum的经典AT&T汇编教程英文原版(没有中文版) 下面是书中的介绍 Introduction Assembly language is one of the most misunderstood programming languages in use. When the term assembly language is used, it often invokes the idea of low-level bit shuffling and poring over thousand- page instruction manuals looking for the proper instruction format. With the proliferation of fancy high- level language development tools, it is not uncommon to see the phrase “assembly language programming is dead” pop up among various programming newsgroups. However, assembly language programming is far from dead. Every high-level language program must be compiled into assembly language before it can be linked into an executable program. For the high- level language programmer, understanding how the compiler generates the assembly language code can be a great benefit, both for directly writing routines in assembly language and for understanding how the high-level language routines are converted to assembly language by the compiler. Who This Book Is For The primary purpose of this book is to teach high-level language programmers how their programs are converted to assembly language, and how the generated assembly language code can be tweaked. That said, the main audience for this book is programmers already familiar with a high-level language, such as C, C++, or even Java. This book does not spend much time teaching basic programming principles. It assumes that you are already familiar with the basics of computer programming, and are interested in learning assembly language to understand what is happening underneath the hood. However, if you are new to programming and are looking at assembly language programming as a place to start, this book does not totally ignore you. It is possible to follow along in the chapters from the start to the finish and obtain a basic knowledge of how assembly language programming (and programming in general) works. Each of the topics presented includes example code that demonstrates how the assem- bly language instructions work. If you are completely new to programming, I recommend that you also obtain a good introductory text to programming to round out your education on the topic. What This Book Covers The main purpose of this book is to familiarize C and C++ programmers with assembly language, show how compilers create assembly language routines from C and C++ programs, and show how the gener- ated assembly language routines can be spruced up to increase the performance of an application. All high-level language programs (such as C and C++) are converted to assembly language by the com- piler before being linked into an executable program. The compiler uses specific rules defined by the cre- ator of the compiler to determine exactly how the high-level language statements are converted. Many programmers just write their high-level language programs and assume the compiler is creating the proper executable code to implement the program. Introduction However, this is not always the case. When the compiler converts the high-level language code state- ments into assembly language code, quirks and oddities often pop up. In addition, the compiler is often written to follow the conversion rules so specifically that it does not recognize time-saving shortcuts that can be made in the final assembly language code, or it is unable to compensate for poorly written high- level routines. This is where knowledge of assembly language code can come in handy. This book shows that by examining the assembly language code generated by the compiler before link- ing it into an executable program, you can often find places where the code can be modified to increase performance or provide additional functionality. The book also helps you understand how your high- level language routines are affected by the compiler’s conversion process. How This Book Is Structured The book is divided into three sections. The first section covers the basics of the assembly language programming environment. Because assembly language programming differs among processors and assemblers, a common platform had to be chosen. This book uses the popular Linux operating system, running on the Intel family of processors. The Linux environment provides a wealth of program devel- oper tools, such as an optimizing compiler, an assembler, a linker, and a debugger, all at little or no charge. This wealth of development tools in the Linux environment makes it the perfect setting for dissecting C programs into assembly language code. The chapters in the first section are as follows: Chapter 1, “What Is Assembly Language?” starts the section off by ensuring that you understand exactly what assembly language is and how it fits into the programming model. It debunks some of the myths of assembly language, and provides a basis for understanding how to use assembly language with high- level languages. Chapter 2, “The IA-32 Platform,” provides a brief introduction to the Intel Pentium family of processors. When working with assembly language, it is important that you understand the underlying processor and how it handles programs. While this chapter is not intended to be an in-depth analysis of the opera- tion of the IA-32 platform, it does present the hardware and operations involved with programming for that platform. Chapter 3, “The Tools of the Trade,” presents the Linux open-source development tools that are used throughout the book. The GNU compiler, assembler, linker, and debugger are used in the book for com- piling, assembling, linking, and debugging the programs. Chapter 4, “A Sample Assembly Language Program,” demonstrates how to use the GNU tools on a Linux system to create, assemble, link, and debug a simple assembly language program. It also shows how to use C library functions within assembly language programs on Linux systems to add extra fea- tures to your assembly language applications. The second section of the book dives into the basics of assembly language programming. Before you can start to analyze the assembly language code generated by the compiler, you must understand the assem- bly language instructions. The chapters in this section are as follows: xxiv Introduction Chapter 5, “Moving Data,” shows how data elements are moved in assembly language programs. The concepts of registers, memory locations, and the stack are presented, and examples are shown for mov- ing data between them. Chapter 6, “Controlling Execution Flow,” describes the branching instructions used in assembly lan- guage programs. Possibly one of the most important features of programs, the ability to recognize branches and optimize branches is crucial to increasing the performance of an application. Chapter 7, “Using Numbers,” discusses how different number data types are used in assembly lan- guage. Being able to properly handle integers and floating-point values is important within the assembly language program. Chapter 8, “Basic Math Functions,” shows how assembly language instructions are used to perform the basic math functions such as addition, subtraction, multiplication, and division. While these are gener- ally straightforward functions, subtle tricks can often be used to increase performance in this area. Chapter 9, “Advanced Math Functions,” discusses the IA-32 Floating Point Unit (FPU), and how it is used to handle complex floating-point arithmetic. Floating-point arithmetic is often a crucial element to data processing programs, and knowing how it works greatly benefits high-level language programmers. Chapter 10, “Working with Strings,” presents the various assembly language string-handling instruc- tions. Character data is another important facet of high-level language programming. Understanding how the assembly language level handles strings can provide insights when working with strings in high-level languages. Chapter 11, “Using Functions,” begins the journey into the depths of assembly language programming. Creating assembly language functions to perform routines is at the core of assembly language optimiza- tion. It is good to know the basics of assembly language functions, as they are often used by the compiler when generating the assembly language code from high-level language code. Chapter 12, “Using Linux System Calls,” completes this section by showing how many high-level func- tions can be performed in assembly language using already created functions. The Linux system pro- vides many high-level functions, such as writing to the display. Often, you can utilize these functions within your assembly language program. The last section of the book presents more advanced assembly language topics. Because the main topic of this book is how to incorporate assembly language routines in your C or C++ code, the first few chapters show just how this is done. The remaining chapters present some more advanced topics to round out your education on assembly language programming. The chapters in this section include the following: Chapter 13, “Using Inline Assembly,” shows how to incorporate assembly language routines directly in your C or C++ language programs. Inline assembly language is often used for “hard-coding” quick rou- tines in the C program to ensure that the compiler generates the appropriate assembly language code for the routine. Chapter 14, “Calling Assembly Libraries,” demonstrates how assembly language functions can be com- bined into libraries that can be used in multiple applications (both assembly language and high-level language). It is a great time-saving feature to be able to combine frequently used functions into a single library that can be called by C or C++ programs. xxv Introduction Chapter 15, “Optimizing Routines,” discusses the heart of this book: modifying compiler-generated assembly language code to your taste. This chapter shows exactly how different types of C routines (such as if-then statements and for-next loops) are produced in assembly language code. Once you understand what the assembly language code is doing, you can add your own touches to it to customize the code for your specific environment. Chapter 16, “Using Files,” covers one of the most overlooked functions of assembly language program- ming. Almost every application requires some type of file access on the system. Assembly language pro- grams are no different. This chapter shows how to use the Linux file-handling system calls to read, write, and modify data in files on the system. Chapter 17, “Using Advanced IA-32 Features,” completes the book with a look at the advanced Intel Single Instruction Multiple Data (SIMD) technology. This technology provides a platform for program- mers to perform multiple arithmetic operations in a single instruction. This technology has become cru- cial in the world of audio and video data processing. What You Need to Use This Book All of the examples in this book are coded to be assembled and run on the Linux operating system, run- ning on an Intel processor platform. The Open Source GNU compiler (gcc), assembler (gas), linker (ld), and debugger (gdb) are used extensively throughout the book to demonstrate the assembly language features. Chapter 4, “A Sample Assembly Language Program,” discusses specifically how to use these tools on a Linux platform to create, assemble, link, and debug an assembly language program. If you do not have an installed Linux platform available, Chapter 4 demonstrates how to use a Linux distribution that can be booted directly from CD, without modifying the workstation hard drive. All of the GNU development tools used in this book are available without installing Linux on the workstation.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KiranWang

一起努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值