- 论坛徽章:
- 0
|
The GNU assembler provides many different ways to define and handle data elements in your assembly language program. It’s up to you to choose the best way to deal with the data your application requires. The data and bss sections both provide methods for defining data elements. The following sections describe the methods available to define data in assembly language applications.
The data section
The data section of the program is the most common place to define data elements. The data section defines specific memory locations where items are stored. These items can be referenced from the instruction codes in the program, and read and modified at will.
The data section is declared using the .data directive. Any data elements declared in this section are reserved in memory and can be read or written to by instructions in the assembly language program.
There is another type of data section called .rodata. Any data elements defined in this section can only be accessed in read-only mode (thus the ro prefix).
Two statements are required to define a data element in the data section: a label and a directive.
The label is used as a tag to reference the data element, much like a variable name in a C program. The label has no meaning to the processor; it is just a place for the assembler to use as a reference point when trying to access the memory location.
Besides the label, you must define how many bytes will be reserved for the data element. This is done using an assembler directive. The directive instructs the assembler to reserve a specified amount of memory for the data element to be referenced by the label.
The amount of memory reserved depends on the type of data that is defined, and the number of items of that type that will be declared. The following table shows the different directives that can be used to reserve memory for specific types of data elements.
Directive
Data Type
.ascii
Text string
.asciz
Null-terminated text string
.byte
Byte value
.double
Double-precision floating-point number
.float
Single-precision floating-point number
.int
32-bit integer number
.long
32-bit integer number (same as .int)
.octa
16-byte integer number
.quad
8-byte integer number
.short
16-bit integer number
.single
Single-precision floating-point number (same as .float)
After the directive is declared, a default value (or values) must be defined. This sets the data in the reserved memory location to the specific values.
Define Array
You are not limited to defining just one value on the directive statement line. You can define multiple values on the line, with each value being placed in memory in the order it appears in the directive. For example, the code
sizes:.long 100,150,200,250,300
places the long integer (4 bytes) value of 100 in the memory location starting at reference sizes, then places the 4 bytes for the value 150 after that in memory, and so on. This acts as an array of values. Each individual value can be referenced by its relative location in the list. Knowing that each long integer value is 4 bytes, you can reference the 200 value by accessing the memory location sizes+8 (and reading 4 bytes).
The .fill directive enables the assembler to automatically create the specified .byte data elements for you.
.fill ele_num
Defining static symbols
Although the data section is intended primarily for defining variable data, you can also declare static data symbols here as well. The .equ directive is used to set a constant value to a symbol that can be used in the text section, as shown in the following examples:
.equ factor, 3.equ LINUX_SYS_CALL, 0x80
Once set, the data symbol value cannot be changed within the program. The .equ directive can appear anywhere in the data section, although to make life easier for anyone else who may need to read your program, it’s best to define them all at once either before or after the other data that is defined.
To reference the static data element, you must use a dollar sign before the label name. For example, the instruction
movl $LINUX_SYS_CALL, %eax
The bss section
Defining data elements in the bss section is somewhat different from defining them in the data section. Instead of declaring specific data types, you just declare raw segments of memory that are reserved for whatever purpose you need them for.
The GNU assembler uses two directives to declare buffers, as shown in the following table.
Directive
Description
.comm
Declares a common memory area for data that is not initialized
.lcomm
Declares a local common memory area for data that is not initialized
While the two sections work similarly, the local common memory area is reserved for data that will not be accessed outside of the local assembly code. The format for both of these directives is
.comm symbol, length
where symbol is a label assigned to the memory area, and length is the number of bytes contained in the memory area.
One benefit to declaring data in the bss section is that the data is not included in the executable program. When data is defined in the data section, it must be included in the executable program, since it must be initialized with a specific value. Because the data areas declared in the bss section are not initialized with program data, the memory areas are reserved at runtime, and do not have to be included in the final program.
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/48729/showart_413949.html |
|