|
Most computer applications are distributed in a form that includes executable files, but not their source code. If the source code were included, it would be useful to a user, programmer, or system administrator, who may wish to modify the program or understand how it works.
Aside from its machine-readable forms, source code also appears in books and other media; often in the form of small code snippets, but occasionally complete code bases; a well-known case is the source code of PGP.
The notion of source code may also be taken more broadly, to include machine code and notations in graphical languages, neither of which are textual in nature. An example from an article presented on the annual IEEE conference on Source Code Analysis and Manipulation:[1]
OrganizationFor the purpose of clarity ‘source code’ is taken to mean any fully executable description of a software system. It is therefore so construed as to include machine code, very high level languages and executable graphical representations of systems.[2]
The source code which constitutes a program is usually held in one or more text files stored on a computer's hard disk; usually these files are carefully arranged into a directory tree, known as a source tree. Source code can also be stored in a database (as is common for stored procedures) or elsewhere.
The source code for a particular piece of software may be contained in a single file or many files. Though the practice is uncommon, a program's source code can be written in different programming languages.[3] For example, a program written primarily in the C programming language, might have portions written in assembly language for optimization purposes. It is also possible for some components of a piece of software to be written and compiled separately, in an arbitrary programming language, and later integrated into the software using a technique called library linking. This is the case in some languages, such as Java: each class is compiled separately into a file and linked by the interpreter at runtime.
Yet another method is to make the main program an interpreter for a programming language[citation needed], either designed specifically for the application in question or general-purpose, and then write the bulk of the actual user functionality as macros or other forms of add-ins in this language, an approach taken for example by the GNU Emacs text editor.
The code base of a computer programming project is the larger collection of all the source code of all the computer programs which make up the project. It has become common practice to maintain code bases in version control systems. Moderately complex software customarily requires the compilation or assembly of several, sometimes dozens or even hundreds, of different source code files. In these cases, instructions for compilations, such as a Makefile, are included with the source code. These describe the relationships among the source code files, and contain information about how they are to be compiled.
The revision control system is another tool frequently used by developers for source code maintenance.
Purposes[edit]Source code is primarily used as input to the process that produces an executable program (i.e., it is compiled or interpreted). It is also used as a method of communicating algorithms between people (e.g., code snippets in books).[4]
Programmers often find it helpful to review existing source code to learn about programming techniques.[4] The sharing of source code between developers is frequently cited as a contributing factor to the maturation of their programming skills.[4] Some people consider source code an expressive artistic medium.[5]
Porting software to other computer platforms is usually prohibitively difficult without source code. Without the source code for a particular piece of software, portability is generally computationally expensive.[citation needed]Possible porting options include binary translation and emulation of the original platform.
Decompilation of an executable program can be used to generate source code, either in assembly code or in a high-level language.
Programmers frequently adapt source code from one piece of software to use in other projects, a concept known as software reusability.
LicensingSoftware, and its accompanying source code, typically falls within one of two licensing paradigms: open source and proprietary software.
Generally speaking, software is open source if the source code is free to use, distribute, modify and study, and proprietary if the source code is kept secret, or is privately owned and restricted. The first software license to be published and to explicitly grant these freedoms was the GNU General Public License in 1989. The GNU GPL was originally intended to be used with the GNU operating system.
For proprietary software, the provisions of the various copyright laws, trade secrecy and patents are used to keep the source code closed. Additionally, many pieces of retail software come with an end-user license agreement (EULA) which typically prohibits decompilation, reverse engineering, analysis, modification, or circumventing of copy protection. Types of source code protection – beyond traditional compilation to object code – include code encryption,code obfuscation or code morphing.
Legal issues in the United States
One of the first court cases regarding the nature of source code as free speech involved University of California mathematics professor Dan Bernstein, who had published on the Internet the source code for an encryption program that he created. At the time, encryption algorithms were classified as munitions by the United States government; exporting encryption to other countries was considered an issue of national security, and had to be approved by the State Department. The Electronic Frontier Foundation sued the U.S. government on Bernstein's behalf; the court ruled that source code was free speech, protected by the First Amendment.In a 2003 court case in the United States, it was ruled that source code should be considered a constitutionally protected form of free speech. Proponents of free speech argued that because source code conveys information to programmers, is written in a language, and can be used to share humour and other artistic pursuits, it is a protected form of communication.
The way a program is written can have important consequences for its maintainers. Coding conventions, which stress readability and some language-specific conventions, are aimed at the maintenance of the software source code, which involves debugging and updating. Other priorities, such as the speed of the program's execution, or the ability to compile the program for multiple architectures, often make code readability a less important consideration, since codequality generally depends on its purpose.
See also[edit]![]() |
Look up code or source code in Wiktionary, the free dictionary. |
![]() |
Wikimedia Commons has media related to Source code. |
源代码(英语:Source code),也称源程序,是指一系列人类可读的计算机语言指令。
在现代程序语言中,源代码可以是以书籍或者磁带的形式出现,但最为常用的格式是文本文件,这种典型格式的目的是为了编译出计算机程序。计算机源代码的最终目的是将人类可读的文本翻译成为计算机可以执行的二进制指令,这种过程叫做编译,通过编译器完成。
源代码主要功用有如下2种作用:
需要指出的是,对于编译语言来说,例如C/C++/Java,源代码的修改不能改变已经生成的目标代码。如果需要目标代码做出相应的修改,必须重新编译。但是目前有许多流行的脚本语言,例如Perl/Python都不需要重新编译,修改完代码可以直接执行看到修改的结果。
源代码作为软件的特殊部分,可能被包含在一个或多个文件中。一个程序不必用同一种格式的源代码书写。例如,一个程序如果有C语言库的支持,那么就可以用C语言;而另一部分为了达到比较高的运行效率,则可以用汇编语言编写。就目前的情况而言,很少有需要直接用汇编语言来编写的软件了,因为很多时候编译器生成的优化程序的运行效率已经很好了,更多的时候是用C/C++/Java这样的编译语言来写核心需要速度的部分,用Perl/Python/Lua等这样的动态语言来做核心的扩展,例如界面,管理配置等等。这样既不会损失效率,也增加了程序的灵活性。
较为复杂的软件,一般需要数十种甚至上百种的源代码的参与。为了降低种复杂度,必须引入一种可以描述各个源代码之间联系,并且如何正确编译的系统。在这样的背景下,版本控制系统(VCS)诞生了,并成为研发者对代码修订的必备工具之一。
还有另外一种组合:将为一种平台编写的软件移植到另外一种平台上,例如将Windows下的软件移植到Linux或者MacOS下,专业术语叫做软件移植。一般可以运行在多个平台下的软件叫做跨平台软件。
版权如果按照源代码类型区分软件,通常被分为两类:自由软件和专有软件。通常,自由软件不仅可以免费得到,而且公开源代码;相对应地,非自由软件则不公开源代码。通过非正常手段获得非自由软件源代码的各种行为都将被视为非法。
质量/质量对于计算机而言,并不存在真正意义上的“好”的源代码;好的源程序,首先要是正确的代码。然后是源程序的可维护性,好的程序风格将可以增强代码的可维护性。源代码是否具有可读性,成为代码质量/质量的标准之一。也有人将程序的效率放在可维护性之前。根据程序所要实现的功能和应用领域很多人对源代码质量/质量有着不同的看法。但是普遍达成一致的是质量/质量高的源程序就是正确的程序。
效率虽然我们可以通过不同的语言来实现计算机的同一功能,但在执行效率上则存在不同。普遍规律是:越高级的语言,其执行效率越低。这也是为什么汇编语言生成的文件比用VB语言生成文件普遍要小的原因。然而,使用低级语言虽可提高运行效率,却会大大降低程序的开发效率,可能导致开发工作变得非常困难,因此一些程序员并不在意高级语言带来的运行效率损失,
目前,许多程序主要使用高级语言来开发,对于要求执行效率的部分使用低级语言编写,以达到开发效率与运行效率的折中。
源代码(也称源程序),是指一系列人类可读的计算机语言指令。
在现代程序语言中,源代码可以是以书籍或者磁带的形式出现,但最为常用的格式是文本文件,这种典型格式的目的是为了编译出计算机程序。计算机源代码的最终目的是将人类可读的文本翻译成为计算机可以执行的二进制指令,这种过程叫做编译,通过编译器完成。
汇编语言(英语:Assembly language)是一种用于电子计算机、微处理器、单片机或其他可编程器件的低级语言,在不同的设备中,汇编语言对应着不同的机器语言指令集。一种汇编语言专用于某种计算机系统结构,而不像许多高级语言,可以在不同系统平台之间移植。
使用汇编语言编写的源代码,需要通过使用相应的汇编程序将它们转换成可执行的机器代码。这一过程被称为汇编过程。
汇编语言采用了助记符(mnemonics)来代表特定低级机器语言的操作。特定的汇编目标指令集可能会包括特定的操作数。许多汇编程序可以识别代表地址和常量的标签(label)和符号(symbols),这样就可以用字符来代表操作数而无需采取写死的方式。普遍地说,特定的汇编语言和特定的机器语言指令集是一一对应的。
许多汇编程序为程序开发、汇编控制、辅助调试提供了额外的支持机制。有的汇编语言编程工具经常会提供宏,它们也被称为宏汇编器。
汇编语言不像其他大多数的程序设计语言一样被广泛用于程序设计;在今天的实际应用中,它通常被应用在底层硬件操作和高要求的程序优化的场合。驱动程序、嵌入式操作系统和实时运行程序都需要汇编语言。
典型的现代汇编器(Assembler)建造目标代码,由解译组语指令集的易记码(mnemonics)到操作码(OpCode),并解析符号名称(symbolic names)成为存储器地址以及其它的实体。使用符号参考是汇编器的一个重要特征,它可以节省修改程序后人工转址的乏味耗时计算。基本就是把机器码变成一些字母而已,编译的时候再把输入的指令字母替换成为晦涩难懂机器码。
现状随着现代软件系统越来越庞大复杂,大量经过了封装的高级语言如C/C++,Pascal/Object Pascal也应运而生。这些新的语言使得程序员在开发过程中能够更简单,更有效率,使软件开发人员得以应付快速的软件开发的要求。而汇编语言由于其复杂性使得其适用领域逐步减小。但这并不意味着汇编已无用武之地。由于汇编更接近机器语言,能够直接对硬件进行操作,生成的程序与其他的语言相比具有更高的运行速度,占用更小的内存,因此在一些对于时效性要求很高的程序、许多大型程序的核心模块以及工业控制方面大量应用。
此外,虽然有众多编程语言可供选择,但汇编依然是各大学计算机科学类专业学生的必修课,以让学生深入了解计算机的运行原理。
http://www.baike.com/wiki/汇编语言
汇编语言(AssemblyLanguage)是面向机器的程序设计语言。汇编语言是一种功能很强的程序设计语言,也是利用计算机所有硬件特性并能直接控制硬件的语言。汇编语言,作为一门语言,对应于高级语言的编译器,需要一个“汇编器”来把汇编语言原文件汇编成机器可执行的代码。高级的汇编器如MASM,TASM等等为我们写汇编程序提供了很多类似于高级语言的特征,比如结构化、抽象等。在这样的环境中编写的汇编程序,有很大一部分是面向汇编器的伪指令,已经类同于高级语言。现在的汇编环境已经如此高级,即使全部用汇编语言来编写windows的应用程序也是可行的,但这不是汇编语言的长处。汇编语言的长处在于编写高效且需要对机器硬件精确控制的程序。[1]
在汇编语言中,用助记符(Memoni)代替操作码,用地址符号(Symbol)或标号(Label)代替地址码。这样用符号代替机器语言的二进制码,就把机器语言变成了汇编语言。因此汇编语言亦称为符号语言。
汇编使用汇编语言编写的程序,机器不能直接识别,要由一种程序将汇编语言翻译成机器语言,这种起翻译作用的程序叫汇编程序,汇编程序是系统软件中语言处理系统软件。汇编语言编译器把汇编程序翻译成机器语言的过程称为汇编。
不能通用汇编语言比机器语言易于读写、调试和修改,同时具有机器语言全部优点。但在编写复杂程序时,相对高级语言代码量较大,而且汇编语言依赖于具体的处理器体系结构,不能通用,因此不能直接在不同处理器体系结构之间移植。
1.面向机器的低级语言,通常是为特定的计算机或系列计算机专门设计的。
2.保持了机器语言的优点,具有直接和简捷的特点。
3.可有效地访问、控制计算机的各种硬件设备,如磁盘、存储器、CPU、I/O端口等。
4.目标代码简短,占用内存少,执行速度快,是高效的程序设计语言。
5.经常与高级语言配合使用,应用十分广泛。
6.对于不同型号的计算机,有着不同的结构的汇编语言。
7.汇编语言由于采用了助记符号来编写程序,比用机器语言的二进制代码编程要方便些,在一定程度上简化了编程过程。
8.汇编语言的特点是用符号代替了机器指令代码,而且助记符与指令代码一一对应,基本保留了机器语言的灵活性。使用汇编语言能面向机器并较好地发挥机器的特性,得到质量较高的程序。
9.汇编语言是面向具体机型的,它离不开具体计算机的指令系统,因此,对于不同型号的计算机,有着不同的结构的汇编语言,而且,对于同一问题所编制的汇编语言程序在不同种类的计算机间是互不相通的。
10.汇编语言中由于使用了助记符号,用汇编语言编制的程序输入计算机,计算机不能象用机器语言编写的程序一样直接识别和执行,必须通过预先放入计算机的"汇编程序"中进行加工和翻译,才能变成能够被计算机直接识别和处理的二进制代码程序。
11.用汇编语言等非机器语言书写好的符号程序称为源程序,运行时汇编程序要将源程序翻译成目标程序。目标程序是机器语言程序,当它被安置在内存的预定位置上,就能被计算机的CPU处理和执行。[2]
汇编语言直接同计算机的底层软件甚至硬件进行交互,它具有如下一些优点:
(1)能够直接访问与硬件相关的存储器或I/O端口;
(2)能够不受编译器的限制,对生成的二进制代码进行完全的控制;
(3)能够对关键代码进行更准确的控制,避免因线程共同访问或者硬件设备共享引起的死锁;
(4)能够根据特定的应用对代码做最佳的优化,提高运行速度;
(5)能够最大限度地发挥硬件的功能。
(6)汇编语言用来编制系统软件和过程控制软件,其目标程序占用内存空间少,运行速度快,有着高级语言不可替代的用途。
缺点同时还应该认识到,汇编语言是一种层次非常低的语言,它仅仅高于直接手工编写二进制的机器指令码,因此不可避免地存在一些缺点:
(1)编写的代码非常难懂,不好维护;
(2)很容易产生bug,难于调试;
(3)只能针对特定的体系结构和处理器进行优化;
(4)开发效率很低,时间长且单调。
(5)汇编语言像机器指令一样,是硬件操作的控制信息,因而仍然是面向机器的语言,使用起来还是比较繁琐费时,通用性也差。
70%以上的系统软件是用汇编语言编写的。某些快速处理、位处理、访问硬件设备等高效程序是用汇编语言编写的。某些高级绘图程序、视频游戏程序是用汇编语言编写的。
最有效途径汇编语言是我们理解整个计算机系统的最佳起点和最有效途径。人们经常认为汇编语言的应用范围很小,而忽视它的重要性。其实汇编语言对每一个希望学习计算机科学与技术的人来说都是非常重要的,是不能不学习的语言。所有可编程计算机都向人们提供机器指令,通过机器指令人们能够使用机器的逻辑功能。所有程序,不论用何种语言编制,都必须转成机器指令,运用机器的逻辑功能,其功能才能得以实现。机器的逻辑功能,软件系统功能构筑其上,硬件系统功能运行于下。汇编语言直接描述机器指令,比机器指令容易记忆和理解。通过学习和使用汇编语言,能够感知、体会、理解机器的逻辑功能,向上为理解各种软件系统的原理,打下技术理论基础;向下为掌握硬件系统的原理,打下实践应用基础。学习汇编语言,向上可以理解软件,向下能够感知硬件,是我们理解整个计算机系统的最佳起点和最有效途径。
作为最基本的编程语言之一,汇编语言虽然应用的范围不算很广,但重要性却勿庸置疑,因为它能够完成许多其它语言所无法完成的功能。就拿 Linux 内核来讲,虽然绝大部分代码是用 C 语言编写的,但仍然不可避免地在某些关键地方使用了汇编代码,其中主要是在 Linux 的启动部分。由于这部分代码与硬件的关系非常密切,即使是 C 语言也会有些力不从心,而汇编语言则能够很好扬长避短,最大限度地发挥硬件的性能。
Linux 程序员大多数情况下 Linux 程序员不需要使用汇编语言,因为即便是硬件驱动这样的底层程序在 Linux 操作系统中也可以用完全用 C 语言来实现,再加上 GCC 这一优秀的编译器目前已经能够对最终生成的代码进行很好的优化,的确有足够的理由让我们可以暂时将汇编语言抛在一边了。但实现情况是 Linux 程序员有时还是需要使用汇编,或者不得不使用汇编,理由很简单:精简、高效和 libc 无关性。假设要移植 Linux 到某一特定的嵌入式硬件环境下,首先必然面临如何减少系统大小、提高执行效率等问题,此时或许只有汇编语言能帮上忙了。
优点汇编语言直接同计算机的底层软件甚至硬件进行交互,它具有如下一些优点:
•能够直接访问与硬件相关的存储器或 I/O 端口;
•能够不受编译器的限制,对生成的二进制代码进行完全的控制;
•能够对关键代码进行更准确的控制,避免因线程共同访问或者硬件设备共享引起的死锁;
•能够根据特定的应用对代码做最佳的优化,提高运行速度;
•能够最大限度地发挥硬件的功能。
同时还应该认识到,汇编语言是一种层次非常低的语言,它仅仅高于直接手工编写二进制的机器指令码,因此不可避免地存在一些缺点:
•编写的代码非常难懂,不好维护;
•很容易产生 bug,难于调试;
•只能针对特定的体系结构和处理器进行优化;
•开发效率很低,时间长且单调。
Linux 下用汇编语言编写的代码具有两种不同的形式。第一种是完全的汇编代码,指的是整个程序全部用汇编语言编写。尽管是完全的汇编代码,Linux 平台下的汇编工具也吸收了 C 语言的长处,使得程序员可以使用 #include、#ifdef 等预处理指令,并能够通过宏定义来简化代码。第二种是内嵌的汇编代码,指的是可以嵌入到C语言程序中的汇编代码片段。虽然 ANSI 的 C 语言标准中没有关于内嵌汇编代码的相应规定,但各种实际使用的 C 编译器都做了这方面的扩充,这其中当然就包括 Linux 平台下的 GCC。[3]
http://www.baike.com/wiki/反汇编
Archiver|干细胞之家 ( 吉ICP备2021004615号-3 )
GMT+8, 2025-5-2 00:14
Powered by Discuz! X1.5
© 2001-2010 Comsenz Inc.