- 论坛徽章:
- 0
|
An assembly language program consists of three components that are used to define the program operations:
- Opcode mnemonics
- Data sections
- Directives
Opcode mnemonics
The core of an assembly language program is the instruction codes used to create the program. To help facilitate writing the instruction codes, assemblers equate mnemonic words with instruction code functions, such as moving or adding data elements.
Defining data
Assembly language also enables the programmer to define data items that will be stored in memory. One of the advantages of programming in assembly language is that it provides you with greater control over where and how your data is stored in memory. The following sections describe two methods used to store and retrieve data in assembly language.
Using memory locations
Assembly language enables you to declare a variable that points to a specific location in memory. Defining variables in assembly language consists of two parts:
1. A label that points to a memory location
2. A data type and default value for the memory bytes
The data type determines how many bytes are reserved for the variable. In an assembly language program, this would look like the following:
testvalue:
.long 150
message:
.ascii “This is a test message”
pi:
.float 3.14159
As you can see from the data types, assembly language allows you to declare the type of data stored in the memory location, along with the default values placed in the memory location, similar to most HLL methods. Each data type occupies a specific number of bytes, starting at the memory location reserved for the label.
Using the stack
Another method used to store and retrieve data in assembly language is called the stack. The stack is a special memory area usually reserved for passing data elements between functions in the program. It can also be used for temporarily storing and retrieving data elements.
The stack is a region of memory reserved at the end of the memory range that the computer reserves for the application. A pointer (called the stack pointer) is used to point to the next memory location in the stack to put or take data. Much like a stack of papers, when a data element is placed in the stack, it becomes the first item that can be removed from the stack (assuming you can only take papers off of the top of the paper stack).
Directives
Instructions and data are not the only elements that make up an assembly language program. Assemblers reserve special keywords for instructing the assembler how to perform special functions as the mnemonics are converted to instruction codes.
One of the most important directives used in the assembly language program is the .section directive. This directive defines the section of memory in which the assembly language program is defining elements. All assembly language programs have at least three sections that must be declared:
- A data section
- A bss section
- A text section
The data section is used to declare the memory region where data elements are stored for the program. This section cannot be expanded after the data elements are declared, and it remains static throughout the program.
The bss section is also a static memory section. It contains buffers for data to be declared later in the program. What makes this section special is that the buffer memory area is zero-filled.
The text section is the area in memory where the instruction code is stored. Again, this area is fixed, in that it contains only the instruction codes that are declared in the assembly language program.
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/48729/showart_403603.html |
|