2 at&t assembly language knowledge
In Linux source code, files with the. s extension are "pure" assembly language files. Here, we combine
The specific example introduces some at&t assembly language knowledge.
1. GNU assembly program gas (GNU assembly and Connection Program)
After you write a program, you need to assemble and connect it. In Linux, there are two methods: one is to use the assembler gas and the Connection Program lD, and the other is to use GCC. Let's take a look at gas and LD:
Gas converts the source file (. O) of the assembly language to the target file (. O). The basic syntax is as follows:
As filename. S-O filename. o
Once a target file is created, it needs to be connected and executed. The basic syntax for connecting to a target file is:
LD filename. O-o filename
Here, filename. O is the target file name, while filename is the output (executable) file.
Gas uses at&t syntax rather than intel syntax, which once again demonstrates that at&t syntax is a standard in the Unix world and you must be familiar with it.
If you want to use the gnc c compiler GCC, you can complete the Assembly and connection step by step, for example:
Gcc-O example. s
Here, example. S is your assembler, and the output file (Executable File) is named example. Among them, the extension must be uppercase s, because the uppercase s can enable GCC to automatically identify C preprocessing commands in Assembler programs, for example, # include, # define, # ifdef, and # endif. That is to say, you can use the C pre-processing command in the assembler to compile with GCC.
2. Section in at&t)
In at&t's syntax, a segment is identified by the. Section keyword. When you compile an assembly language program, you must have at least three segments:
(1) Data Segment
. Section. Data: This section contains the data initialized by the program, that is, the variables with initial values, such:
Hello:. String "Hello world! /N"
Hello_len:. Long 13
(2) BSS segment
. Section. BSS: This section contains data that has not been initialized by the program, that is, variables without initial values. When the operating system loads this program, these variables are set to 0, for example:
Name:. fill 30 # used to request the user to enter a name
Name_len:. Long 0 # Name Length (not defined yet)
When this program is installed, both name and name_len are set to 0. If you accidentally assign an initial value to a variable in the. BSS section, this value will also be lost and the variable value is still 0.
The advantage of using. BSS over using. Data is that. BSS does not occupy disk space. On a disk, a long integer is enough to store. BSS segments. When the program is loaded into the memory, the operating system only allocates the memory size to this section of 4 bytes.
Note: Compile the program. data and. BSS align, for example ,. data has a total of 34 bytes, so the compiler puts it on 36 bytes, that is, actually giving it 36 bytes of space.
(3) code segment
. Section. Text: This section contains the program code. It is a read-only section, while. Data and. BSS are read/write sections.