Assembly program design in Linux

Source: Internet
Author: User
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.

. You can directly access registers and I/O
. The code can be executed very accurately.
. You can compile code that is more efficient than the general compilation system.
. Can be used as different languages or different standard interfaces

Disadvantage: assembly language is a very low-level language.

. Very lengthy and monotonous, which can be realized during DOS programming.

. Easy to generate bugs and difficult to debug
. Code is not easy to maintain
. Poor compatibility, very close to 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. Where
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
"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
Call eax, 4; 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, you need to use System
Call is actually a lot of C functions that can be called in the assembler. The Calling method is identical to that in the DOS assembly, and there is no need to link additional library functions in the ASM 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. In order to make the limited physical memory meet the application's high memory
Demand, Linux uses a memory management method called "Virtual Memory. Linux divides the memory into a "Memory Page" that is easy to process.
When the system runs and the application needs more memory than the physical memory, Linux can switch memory pages that are not currently used to the hard disk.
The idle Memory Page can meet the memory requirements of the application, but the application does 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 simultaneously.
Multiple tasks are implemented by running these processes in turn during the interval ". This short interval is called a "time slice" that allows the process to run in turn.
The method is called "scheduling", and the program that completes scheduling is called a scheduling program. With the multi-task mechanism, each burst can be considered to only have its own exclusive computing
To simplify 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 performs
Sort. 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, in Linux
There is a big difference, it is almost impossible to grasp all of them. Next I will introduce several common assembly 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 like a macro, but it is clearer and more accurate than a macro to express the working status of the machine.

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 Linux versions. However, it uses AT&T's syntax standards, which are quite different from Intel's syntax standards.
In terms of DOS programming, it is very difficult to learn. Of course, to be proficient in assembly programming in Linux, it is also essential to learn GAS. For specific syntax standards, see Using GNU
Aggreger.

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.

The format of. NASM 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
In Linux, there are two main types: aout and ELF. If you are not sure whether your Linux system should use AOUT or ELF, you can enter file NASM In the nasm directory, if the output is nasm: ELF 32-bit LSB executable i386 (386 and
Up) Version 1 indicates ELF. If nasm: Linux/i386 demand-paged executable is output
(QMAGIC) indicates aout.

The main differences between. NASM and MASM are as follows:

First, like linux systems, nasm is case-sensitive. Hello And hello are different identifiers.
To 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 they are expressed in the same way in MASM. NASM completely avoids such confusion. It uses the rule that all memory operations must be implemented through. For example, in the preceding example, the bar operation must be written as follows:
Mov
Ax, [bar]. Therefore, the use of offset in nasm is unnecessary (there is no offset in nasm ). The use of [] In nasm is different from that in masm. All expressions must be written in [
], Two examples are given below:

Masm nasm
Mov
Ax, table [di] mov ax, [table + di]
Mov
Ax, es: [di] mov ax, [es: di]
Mov
Ax, [di] + 1 mov ax, [di + 1]

Nasm does not store variable types. The reason is that masm uses [
] The addressing type variable must also be specified. LODS, MOVS, STOS, SCAS, CMPS, INS,
OUTS only supports specified types of operations such as lodsb and lodsw. There is no assume operation in nasm, and the segment address is entirely dependent on the value of the stored segment register.

For more information about NASM usage and syntax, see the NASM user manual.

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.