Basic Assembly Language Instructions
COS2621 - Computer Organisation · Machine Level Programming
Basic Assembly Language Instructions
Assembly language is a low-level programming language that is closely related to machine code. It uses mnemonics to represent machine-level instructions, making it easier for humans to understand and write programs for computers. This topic covers basic assembly language instructions, which are the building blocks of assembly programming.
What is Assembly Language?
Assembly language is a symbolic representation of machine code. Each assembly language instruction corresponds to a machine code instruction that the CPU can execute. Unlike high-level languages, assembly language provides direct control over hardware, allowing programmers to manage system resources efficiently.
Basic Components of Assembly Language
Assembly language instructions typically consist of three main components:
- Opcode: This is the operation code that specifies the operation to be performed (e.g., ADD, SUB, MOV).
- Operands: These are the values or addresses on which the operation will be performed. Operands can be registers, memory addresses, or immediate values.
- Labels: Labels are used to identify locations in the code, often for jumps or loops.
Common Assembly Language Instructions
Here are some of the most common assembly language instructions:
1. Data Movement Instructions
Data movement instructions transfer data between registers, memory, and I/O ports. The most common instruction is MOV.
MOV destination, sourceThis instruction copies the value from the source to the destination.
Example:
MOV AX, 5This instruction moves the value 5 into the register AX.
Remember: The destination is always on the left side of the comma.
2. Arithmetic Instructions
Arithmetic instructions perform mathematical operations. Common arithmetic instructions include:
- ADD: Adds two values.
- SUB: Subtracts one value from another.
- MUL: Multiplies two values.
- DIV: Divides one value by another.
Example of ADD:
ADD AX, BXThis instruction adds the value in the register BX to the value in AX and stores the result in AX.
Example of SUB:
SUB AX, 3This instruction subtracts 3 from the value in AX and stores the result back in AX.
Watch out: Ensure that the registers used in arithmetic operations can hold the result. For example, if AX is a 16-bit register, the result of an addition must also fit within 16 bits.
3. Logical Instructions
Logical instructions perform bitwise operations. Common logical instructions include:
- AND: Performs a bitwise AND operation.
- OR: Performs a bitwise OR operation.
- XOR: Performs a bitwise exclusive OR operation.
Example of AND:
AND AX, BXThis instruction performs a bitwise AND operation between AX and BX, storing the result in AX.
4. Control Flow Instructions
Control flow instructions alter the sequence of execution of the program. Common control flow instructions include:
- JMP: Unconditional jump to another instruction.
- JE: Jump if equal.
- JNE: Jump if not equal.
Example of JMP:
JMP LABELThis instruction jumps to the instruction at the location marked by LABEL.
Example of JE:
JE LABELThis instruction jumps to LABEL if the zero flag is set, indicating that the last operation resulted in zero.
Watch out: Ensure that the labels are defined in the code before they are referenced in jump instructions.
5. Comparison Instructions
Comparison instructions compare two values and set flags based on the result. The most common comparison instruction is CMP.
CMP operand1, operand2This instruction compares operand1 with operand2 but does not store the result. Instead, it sets the CPU flags based on the comparison.
Example:
CMP AX, BXThis instruction compares the values in AX and BX.
Remember: After a CMP instruction, you can use conditional jumps based on the flags set by the comparison.
Addressing Modes
Addressing modes determine how the operands of an instruction are accessed. While we will cover addressing modes in detail in the next topic, it is important to note that different addressing modes can affect how you write assembly instructions. Common addressing modes include:
- Immediate Addressing: The operand is specified explicitly in the instruction.
- Direct Addressing: The operand's memory address is given directly.
- Indirect Addressing: The operand's address is stored in a register or memory location.
Summary
- Assembly language provides a symbolic representation of machine code.
- Basic instructions include data movement, arithmetic, logical, control flow, and comparison instructions.
- Addressing modes determine how operands are accessed in assembly language instructions.
Check your understanding
- What is the function of the MOV instruction in assembly language?
- Explain the difference between the ADD and SUB instructions.
- What does the CMP instruction do in assembly language?
- Why is it important to define labels before using them in jump instructions?