Assembly program design in Linux

Source: Internet
Author: User
Article title: assembly program design in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: Zhou Tianyang
  
Introduction:
  
Assembly language is a low-level language that is closely related to hardware and operating systems. My PC used DOS before, and now it has developed into WINDOWS 98, while another operating system Linux is also on the rise. The following compares the three operating systems:
  
  
  
Operation System
Optimization
Missing points
Price
  
DOS
Relatively stable and fast
Unable to make full use of computer performance, no graphical interface
Relatively low
  
WINDOWS 98
Easy to operate,
  
Many applications and good hardware compatibility
Unstable, frequent crashes, slow speed
High
  
Linux
Excellent performance, very stable, beautiful interface, easy to operate
Lack of support from software vendors and less application software
Free
  
  
Table 1 comparison of operating systems
  
  
  
From the comparison above, we can see that the Linux operating system has a great advantage, and its popularity should be just a matter of time, therefore, how to develop software in Linux is a subject that students in the computer department must learn and study.
  
The main programming language in Linux is C. At the same time, Linux also supports many other programming languages. assembly language is also included as one of the most important programming languages. It can complete functions that cannot be completed by many other languages. To learn Linux programming, you must learn assembly programming in Linux. Next I will introduce the assembly program design in Linux.
  
  
  
Linux Assembly introduction:
  
I. Advantages and disadvantages of assembly language:
  
Since Linux is written in C, C naturally becomes the standard programming language of Linux. Most people ignore the compilation, and it is very difficult to find information on the internet. many problems need to be tried by yourself. In my opinion, it is unfair to treat assembly languages like this. we cannot only see its shortcomings, but also its advantages. the following compares its advantages and disadvantages:
  
Advantage: Assembly languages can express very underlying things.
  
L can directly access registers and I/O
  
L The code can be executed very accurately.
  
L code compilation is more efficient than general compilation systems
  
L can be used as interfaces of different languages or standards
  
Disadvantage: assembly language is a very low-level language.
  
L It is very lengthy and monotonous, and can be realized during programming in DOS.
  
L prone to bugs and difficult debugging
  
L code is not easy to maintain
  
L poor compatibility and close relationship with hardware
  
In general, the assembly language should be used wherever necessary, as far as possible to write large programs with less compilation, and the inline mode should be adopted.
  
II. assembly language tools:
  
Commonly used tools under DOS, such as MASM and TASM, cannot be used in Linux. Linux has its own assembly tools and many types of tools. Among them, Gas can be regarded as a standard configuration. each type of Linux includes Gas, but GAS does not use the assembly syntax we usually use in DOS. it uses the AT&T syntax format, the syntax format is quite different from that of intel.
  
To use a syntax similar to DOS, you must use another compilation tool, NASM, which is basically the same as MASM, but there are also many differences, especially when it comes to operating system principles, it is totally different from DOS.
  
  
  
Linux assembler program design:
  
1. Hello, world!
  
Almost all languages are started with "Hello, world !" For example, I also use Hello, world! As an example.
  
  
  
; ------------- NASM's standalone Hello-World.asm for Linux --------
Section. text
Extern puts
Global main
  
Main:
Push dword msg; stash the location of msg on the stack.
Call puts; call the "puts" routine (libc ?)
Add esp, byte 4; clean the stack?
Ret; exit.
  
Msg:
Db "Hello World! ", 0
  
Compile:
Nasm? F elf hello. asm
Gcc? O hello. o
  
Note: This program is actually called. the Linux system's puts function is the same as calling the C language function under DOS. use Extern to declare puts as an external function, press the parameter (msg address) into the stack, and the Call function outputs the result.
Let's look at a program:
  
  
  
Section. text
Global main
  
Main:
Mov eax, 4; called on 4
Mov ebx, 1; ebx returns 1 to indicate stdout
Mov ecx, msg; the first address of the string is sent to ecx
Mov edx, 14; the length of the string is sent to edx
Int 80 h; output string
Mov eax, 1; 1 call
Int 80 h; end
Msg:
Db "Hello World! ", 0ah, 0dh
(Compile the same program)
  
This program is very similar to the DOS program. it uses 80 h interrupt in linux, which is equivalent to 21h interrupt in DOS, because Linux is a 32-bit operating system, therefore, registers such as EAX and EBX are used. However, Linux, as a multi-user operating system, is very different from DOS. It is impossible to write special programs without understanding the operating system and hardware. Next I will introduce the Linux operating system.
  
  
  
II. Introduction to Linux:
  
The operating system is actually an interface between abstract resource operations and specific hardware operation details. For a multi-user operating system such as Linux, it must avoid direct access to hardware and mutual interference between users. Therefore, Linux takes over BIOS calls and Port input and output. For more information about Port input and output, see Linux IO-Port-Programming HOWTO. To access hardware through Linux, System Call is required. In fact, many C functions can be called in assembler programs. The Calling method is the same as that in DOS, in addition, you do not need to link additional library functions when using ASM for assembly.
  
The main differences between Linux and DOS are memory management, processes (no process concept under DOS), and file systems. memory management and process are closely related to assembly programming:
  
1. memory management:
  
For any computer, its memory and other resources are limited. Linux uses a memory management method called "virtual memory" to ensure that the limited physical memory meets the large memory demand of applications. Linux divides memory into easy-to-process "memory pages". when the memory requirements of applications during system operation are greater than the physical memory, in Linux, memory pages that are not currently used can be switched to the hard disk. in this way, idle memory pages can meet the memory requirements of applications, but applications will not notice the occurrence of memory switching.
  
2. process
  
A process is actually a running entity of a specific application. In Linux, multiple processes can be run at the same time. in Linux, multiple tasks are implemented by running these processes in turn at a short interval ". This short interval is called a "time slice". the method that allows the process to run in turn is called "scheduling", and the program that completes the scheduling is called a scheduling program. Through a multi-task mechanism, each process can be considered to only have its own computer, thus simplifying programming. each process has its own address space and can only be accessed by this process, the operating system avoids mutual interference between processes and the potential harm to the system caused by "bad" programs.
  
To complete a specific task, you sometimes need to combine the functions of two programs, for example, one program outputs the text, and the other program sorts the text. To this end, the operating system also provides inter-process communication mechanisms to help complete such tasks. Common inter-process communication mechanisms in Linux include signals, pipelines, shared memory, semaphores, and sockets.
  
III. compilation tools in Linux:
  
In Linux, the compilation tools can be described as a hundred schools of contention. unlike in DOS, they all need to be controlled by MASM and TASM. However, every compilation tool in Linux is very different. it is almost impossible to master it all. next I will introduce several common compilation tools, focusing on NASM and its usage and syntax.
  
1. GCC
  
GCC is actually a gnu c language product, but it supports Inline Assemble. in GCC, inline assemble is used like a macro, but it is clearer and more accurate than a macro to express the working status of machines.
  
C is a high generalization of assembly programming, which can reduce the trouble in many compilation, especially in the C compiler of GCC, assemble does not seem to play much role.
  
2. GAS
  
GAS is a basic assembly tool in various Linux versions, but it uses AT&T's syntax standards and Intel's syntax standards. for DOS programming, it is very difficult to learn. Of course, to be proficient in assembly programming in Linux, it is also necessary to learn GAS. for specific syntax standards, see Using GNU compiler er.
  
3. GASP
  
GASP is an extension of GAS, which enhances GAS's support for macros.
  
4. NASM
  
NASM is a compilation tool with the most similar syntax as DOS in linux. Even so, it is quite different from MASM.
  
L The NASM format is as follows:
  
Nasm? F -O
  
For example:
  
Nasm-f elf hello. asm
  
Will compile hello. asm into the ELF object File, and
  
Nasm-f bin hello. asm-o hello.com
  
Will compile hello. asm into a binary executable file hello.com
  
Nasm? H
  
The complete description of the NASM command line is listed.
  
NASM does not have any output unless an error occurs.
  
-F is mainly used for aout and ELF in Linux. if you are not sure whether your Linux system should use AOUT or ELF, you can enter File NASM in the nasm directory if nasm is output: ELF 32-bit LSB executable i386 (386 and up) Version 1 indicates ELF. If nasm: Linux/i386 demand-paged executable (QMAGIC) is output, it indicates aout.
  
  
  
L major differences between NASM and MASM:
  
First, like linux systems, nasm is case-sensitive. Hello and hello are different identifiers. to assemble them into DOS or OS/2, add the UPPERCASE parameter.
  
Second, the memory operands in nasm are expressed in.
  
In MASM
  
Foo equ 1
Bar dw 2
Mov ax, foo
Mov ax, bar
It will be compiled into completely different commands, although their expression in MASM is completely one
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.