Linux Assembly Language Development Guide 2-General Linux technology-Linux programming and kernel information. For details, see the following section. Linux Assembly Language Development Guide
Edit: Ice Angel Author: Source: ChinaUnix 2005-6-3
I. Introduction
As one of the most basic programming languages, assembly language, although not widely used, is of no doubt important because it can accomplish functions that many other languages cannot do. Taking the Linux kernel as an example, although most of the Code is written in C, it is inevitable to use assembly code in some key areas, mainly in the Linux Startup part. Due to the close relationship between this part of code and hardware, even the C language can be a little powerless, while the assembly language can be very good to foster strengths and circumvent weaknesses, to maximize the performance of the hardware.
In most cases, Linux programmers do not need to use the assembly language, because even underlying programs such as hardware drivers can be fully implemented in the Linux operating system using the C language, in addition, the GCC excellent compiler has been able to optimize the final generated code. There are indeed enough reasons for us to temporarily leave the Assembly Language aside. But the implementation is that Linux Programmers sometimes still need to use assembly, or have to use assembly. The reason is simple: simplification, efficiency, and libc independence. If we want to port Linux to a specific embedded hardware environment, we must first face problems such as how to reduce the system size and improve the execution efficiency. At this time, only the assembly language may be able to help.
The Assembly Language Directly Interacts with the underlying software or even hardware of the computer. It has the following advantages:
Direct access to hardware-related memory or I/O ports;
It can completely control the generated binary code without being limited by the compiler;
More accurate control of key code to avoid deadlocks caused by concurrent thread access or hardware device sharing;
Optimize the code based on specific applications to improve the running speed;
The ability to maximize the functionality of the hardware.
At the same time, we should also realize that the Assembly Language is a very low-level language, which is only higher than directly writing binary machine instruction codes. Therefore, it inevitably has some disadvantages:
The written code is difficult to understand and difficult to maintain;
It is easy to generate bugs and difficult to debug;
Only specific architecture and processors can be optimized;
Low development efficiency, long time and monotonous.
The code written in assembly language in Linux has two different forms. The first is complete assembly code, which means that the entire program is all written in assembly language. Despite the complete compilation code, the compilation tools on the Linux platform also absorb the advantages of the C language, so that programmers can use the # include, # ifdef, and other pre-processing commands, and can simplify the Code through macro definition. The second is embedded assembly code, which refers to the assembly code snippets that can be embedded into C language programs. Although ansi c language standards do not provide relevant provisions on Embedded Assembly Code, all the actually used C compilers have been expanded in this regard, of course, this includes GCC on the Linux platform.
Ii. Linux Assembly syntax format
The vast majority of Linux programmers have previously only been familiar with DOS/Windows assembly languages. These Assembly codes are Intel-style. However, in Unix and Linux systems, the AT&T format is mostly used. The two are quite different in syntax format:
In AT&T Assembly format, Register names must be prefixed with '%'. In Intel assembly format, Register names do not need to be prefixed. For example:
AT&T format pushl % eax
Push eax in Intel format
In AT&T Assembly format, the prefix '$' is used to represent an immediate operand. In Intel assembly format, the representation of immediate numbers does not contain any prefix. For example:
AT&T format pushl $1
Intel format push 1
The source and target operands in AT&T and Intel are in the opposite position. In Intel assembly format, the destination operand is on the left of the source operand, while in AT&T Assembly format, the destination operand is on the right of the source operand. For example:
AT&T format addl $1, % eax
Intel format add eax, 1
In AT&T Assembly format, the length of an operand is determined by the last letter of the operator. The suffixes 'B', 'w', and 'l' indicate that the operands are bytes (byte, 8 bits) characters (word, 16 bits) and long characters (long, 32 bits). In Intel assembly format, the length of an operand is expressed by prefix such as "byte ptr" and "word ptr. For example:
AT&T format
Movb val, % al
Intel format mov al, byte ptr val
In AT&T Assembly format, the prefix '*' must be added before the operands of absolute transfer and call commands (jump/call), but not in Intel format.
The operation code of the remote Transfer Instruction and remote sub-call instruction is "ljump" and "lcall" in AT&T Assembly format, while "jmp far" and "call far" in Intel assembly format ", that is:
AT&T format ljump $ section, $ offset lcall $ section, $ offset
Intel jmp far section: offset call far section: offset
The corresponding remote return command is:
AT&T format lret $ stack_adjust
Intel format ret far stack_adjust
In AT&T Assembly format, the addressing method of memory operands is
Section: disp (base, index, scale)
In Intel assembly format, the addressing mode of memory operands is:
Section: [base + index * scale + disp]
Because Linux uses 32-bit linear addresses in protection mode, the following address calculation method is used instead of considering the segment base address and offset when calculating the address:
Disp + base + index * scale
The following is an example of some memory operations:
I really don't know what will happen if I break this tradition, but since the first example of all programming languages is to print a string "Hello World!" on the screen! ", Then we will introduce the Assembly Language Programming in Linux in this way.
In Linux, there are many ways to display a string on the screen, but the simplest way is to use the system call provided by the Linux kernel. The biggest advantage of using this method is that it can communicate directly with the kernel of the operating system. You do not need to link a function library such as libc or use the ELF interpreter, therefore, the code size is small and the execution speed is fast.
Linux is a 32-bit operating system running in protected mode. It adopts the flat memory mode. Currently, binary code in ELF format is the most commonly used. An executable program in the ELF format is generally divided into the following parts :. text ,. data and. bss, where. text is a read-only code area ,. data is a readable and writable data area, while. bss is a readable and writable data zone without initialization. Code and data zones are collectively called sections in ELF. You can use other standard sections or add custom sections as needed, but at least one ELF executable program should have one. text section. The following is our first assembler, In the AT&T assembly language format:
Example 1. AT&T format
# Hello. s
. Data # data Segment Declaration
Msg:. string "Hello, world! \ N "# string to be output
Len =.-msg # String Length
. Text # code snippet Declaration
. Global _ start # specify the entry function
_ Start: # display a string on the screen
Movl $ len, % edx # parameter 3: String Length
Movl $ msg, % ecx # parameter 2: string to be displayed
Movl $1, % ebx # parameter 1: file descriptor (stdout)
Movl $4, % eax # system call number (sys_write)
Int $0x80 # Call the kernel function
# Exit the program
Movl $0, % ebx # parameter 1: Exit code
Movl $1, % eax # system call number (sys_exit)
Int $0x80 # Call the kernel function
When I first came into contact with AT&T-formatted assembly code, many programmers thought it was too obscure. It doesn't matter. On the Linux platform, you can also use the Intel format to compile the assembly program:
Example 2. Intel format
; Hello. asm
Section. data; data segment Declaration
Msg db "Hello, world! ", 0xA; string to be output
Len equ $-msg; String Length
Section. text; code segment Declaration
Global _ start; specifies the entry function
_ Start:; display a string on the screen
Mov edx, len; parameter 3: String Length
Mov ecx, msg; parameter 2: string to be displayed
Mov ebx, 1; parameter 1: file descriptor (stdout)
Mov eax, 4; system call number (sys_write)
Int 0x80; call the kernel function
; Exit the program
Mov ebx, 0; parameter 1: Exit code
Mov eax, 1; system call number (sys_exit)
Int 0x80; call the kernel function
Although the syntax used by the above two assembler programs is completely different, the function is to call the sys_write provided by the Linux kernel to display a string, and then call sys_exit to exit the program. In the Linux kernel source File include/asm-i386/unistd. h, you can find the definitions of all system calls.
Iv. Linux assembly tools
There are many types of assembler tools on Linux, but like DOS/Windows, the most basic tools are assembler, connector, and debugger.
1. Assembler
The assembler is used to convert source programs written in assembly languages into binary-format target codes. The standard assembler on the Linux platform is GAS, which is the background compilation tool on which GCC depends. It is usually included in the binutils software package. GAS uses the standard AT&T Assembly syntax and can be used to compile programs written in AT&T format:
[Xiaowp @ gary code] $ as-o hello. o hello. s
Another commonly used assembler on Linux is NASM. It provides good macro commands and supports a considerable number of target code formats, including bin and. out, coff, elf, and rdf. NASM uses a manually compiled syntax analyzer, so the execution speed is much faster than that of GAS. More importantly, it uses Intel assembly syntax, it can be used to compile assembler programs written in Intel syntax format:
[Xiaowp @ gary code] $ nasm-f elf hello. asm
2. linker
The target code generated by the assembler cannot run directly on the computer. It must be processed by the linker to generate executable code. The linker is usually used to connect multiple target codes into one executable code. In this way, the entire program can be divided into several modules for separate development before they can be combined into an application. Linux uses ld as a standard linking program, which is also included in the binutils package. After compilation and generation of the target code through GAS or NASM, the assembler can use ld to link it to an executable program:
[Xiaowp @ gary code] $ ld-s-o hello. o
3. Debugger
Some people say that the program is not compiled but called out. It shows that debugging plays an important role in software development, especially when programming in assembly languages. In Linux, You can debug Assembly code by using a general Debugger such as GDB and DDD, or by using an ALD (Assembly Language Debugger) Specially Used to debug Assembly code ).
From the perspective of debugging, the advantage of using GAS is that you can include the symbol table in the generated target code ), in this way, you can use GDB and DDD for source code-level debugging. To include the symbol table in the generated executable program, you can compile and link the table in the following way:
[Xiaowp @ gary code] $ as -- maid-o hello. o hello. s
[Xiaowp @ gary code] $ ld-o hello. o
When running the as command, the parameter-ststabs can tell the assembler to add a symbol table to the generated target code. Note that the-s parameter is not added when the ld command is used for link, otherwise, the symbol table in the target code will be deleted during the link.
Debugging assembly code in GDB and DDD is the same as debugging C-language code. You can set breakpoints to interrupt program running and view the current values of variables and registers, you can also track the code in one step.
Debug assembler in DDD
Assembly programmers usually face some harsh software and hardware environments. The short and concise ALD may better meet the actual needs. Therefore, the following describes how to use the ALD to debug the assembly program. First, run the ald command in the command line mode to start the debugger. The parameter of this command is the executable program to be debugged:
[Xiaowp @ gary doc] $ ald hello
Assembly Language Debugger 0.1.3
Copyright (C) 2000-2002 Patrick Alken
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.