Basic Knowledge of Assembly Language

1. Statement Format of Assembly Language

The source program written in assembly language consists of many statements (also known as assembly instructions). Each statement consists of 1 to 4 parts, and its format is:

[Label] Instruction Mnemonic [Operands] [; Comment]

The parts in square brackets can be present or absent. Each part is separated by at least one space, and a line can have a maximum of 132 characters.

1. Identifier

The name given to an instruction or an address of a storage unit. It can consist of the following characters:

Letters: A ~ Z; Numbers: 0 ~ 9; Special characters: ?, ·, @, -, $.

Numbers cannot be the first character of an identifier, and a dot can only be used as the first character. The maximum length of an identifier is 31 characters. When an identifier is followed by a colon, it indicates a label. It represents the starting address of that line of instruction; when an identifier is not followed by a colon, it indicates a variable; identifiers before pseudo-instructions do not have a colon.

2. Instruction Mnemonic

Represents different operations of instructions, which can be 8086 instruction mnemonics or pseudo-instructions.

3. Operands

The objects on which the instruction operates. Depending on the instruction’s requirements, there may be one, two, or none,

for example: RET; no operands

COUNT: INC CX; one operand

If it is a pseudo-instruction, there may be multiple operands, for example:

COST DB 3,4,5,6,7; 5 operands

MOV AX, [BP+4]; the second operand is an expression

4. Comments

This item can be present or absent, and is used to add notes to the source program to improve the program’s readability.

2. Operators in Assembly Language

1. Arithmetic Operators, Logical Operators, and Relational Operators

Arithmetic operators can be applied to numeric operands, and the result is also numeric. When applied to memory operands, only + and – operators are meaningful.

2. Value Operators SEG, OFFSET, TYPE, SIZE, and LENGTH

· SEG and OFFSET give the segment address and offset of a variable or label, respectively.

For example, define: SLOT DW 25

Then: MOV AX, SLOT; fetch a word from the SLOT address into AX

MOV AX, SEG SLOT; move the segment address of SLOT into AX

MOV AX, OFFSET SLOT; move the offset address within the segment of SLOT into AX

· The TYPE operator returns a value representing the type of the memory operand. The values of various memory address operand types are shown in Table 4-1.

Basic Knowledge of Assembly Language

· LENGTH and SIZE operators apply only to data memory operands (defined with DB/DW/DD, etc.).

LENGTH returns the number of units associated with the memory address operand,

SIZE returns the number of bytes allocated for the memory operand.

For example: if MULT-WORD DW 50DUP(0)

Then LENGTH(MULT-WORD) = 50

SIZE(MULT-WORD) = 100

Note: SIZE(X) = (LENGTH X) * (TYPE X)

3. Attribute Operators

Attribute operators are used to specify a temporary attribute for the operands in the instruction while temporarily ignoring the current attributes. Commonly used ones include:

(1) Composite Operator PTR

When it acts on an operand, it ignores the current type (byte or word) and attributes (NEAR or FAR) of the operand, and gives a temporary type or attribute,

General format: Type PTR Expression

Function: Establish a memory address operand that has the same segment address offset as the subsequent memory address operand but has a different type.

For example: SLOT DW 25

At this point, SLOT is defined as a word unit. If we want to fetch its first byte content, we can use PTR to temporarily change it to a byte unit,

i.e., MOV AL, BYTE PTR SLOT

3. Expressions

Composed of a sequence of operators and operands, producing a definite value during assembly. This value can represent a constant or the offset address of a storage unit; the corresponding expressions are called constant expressions and address expressions.

1. Constants

Constants appearing in assembly language statements can be of 7 types:

① Binary numbers followed by the letter B, such as 01000001B.

② Octal numbers followed by the letter Q or O, such as 202Q or 202O.

③ Decimal numbers followed by D or not followed by a letter, such as 85D or 85.

④ Hexadecimal numbers followed by H, such as 56H, 0FFH. Note that when the first character of the number is A~F, a digit 0 should be added before the character to distinguish it from variables.

Additionally, decimal floating-point numbers, hexadecimal real numbers, characters, and strings.

2. Constant Operands

Constant operands are numeric operands that are generally constants or identifiers representing constants. They can be numeric constant operands or string constant operands. The former can use binary, octal, decimal, or hexadecimal counting forms; the latter corresponds to the ASCII code of the respective characters.

3. Memory Operands

Memory operands are address operands that represent the address of a storage unit, usually appearing in the form of identifiers.

Memory operands can be divided into two types: variables and labels. If the address represented by the memory operand is the address of some data in the data segment, extra segment, or stack segment, then this memory operand is called a variable; if the address represented by the memory operand is the address of a certain instruction code in the code segment, then this memory operand is called a label. (http://www.diangon.com/ All rights reserved) The content of the memory unit corresponding to the variable can be changed during the program’s execution, while labels are usually the target operands of transfer instructions or call instructions and cannot be changed during program execution.

Memory operands have three aspects of attributes.

(1) Segment Address: The segment address of the memory unit corresponding to the memory operand;

(2) Offset Address: The offset address of the memory unit corresponding to the memory operand within its segment;

(3) Type: The type of the variable is the byte count of the data item stored in the corresponding memory unit; the type of the label reflects the addressing mode of the corresponding memory unit address when it is used as the target operand of transfer or call instructions, which can be NEAR or FAR. Specific values can be seen in Table 4-1.

4. Constant Expressions

Composed of constant operands and operators, producing a constant during assembly.

For example: PORT, VAL + 1, OFFSET SUM, SEG SUM, TYPE CYCLE, etc.

5. Address Expressions

Composed of memory operands and operators, must have clear physical meanings.

For example: SUM + 2, CYCLE – 5

The value of the expression SUM + 2, CYCLE – 5 is still a memory operand, and the segment address and type attributes of this memory operand are the same as those of the memory operands SUM and CYCLE, but the offset address is larger than SUM and CYCLE by 2 or smaller by 5, respectively. Expressions are computed during assembly, while the content of variable units can change during program execution.

4. Steps to Assemble an Assembly Language Program

To run an assembly language program on a machine, it must first be assembled into an executable program. To accomplish this, the following steps must be completed.

1. Edit the source program

2. Call the macro assembler to assemble the source program

3. Link the object program

4. Run the executable program and debug

Electronics enthusiasts, follow electronic DIY

Basic Knowledge of Assembly Language

Leave a Comment