Introduction to Assembly Language
COS2621 - Computer Organisation · Machine Level Programming
Introduction to Assembly Language
Assembly language is a low-level programming language that is closely related to machine code. It provides a way to write instructions that a computer can execute directly. Each assembly language instruction corresponds to a specific machine code instruction for a given computer architecture.
What is Assembly Language?
Assembly language is a symbolic representation of the binary instructions that a computer's central processing unit (CPU) understands. Unlike high-level programming languages, assembly language is specific to a particular computer architecture. This means that code written in assembly language for one type of CPU may not work on another type.
Remember: Assembly language is architecture-specific, meaning it varies between different types of CPUs.
Components of Assembly Language
Assembly language consists of several components, including:
- Mnemonics: These are short, human-readable representations of machine instructions. For example, the mnemonic
MOVis used to move data from one location to another. - Operands: These specify the data to be manipulated. Operands can be registers, memory addresses, or constants.
- Labels: These are used to mark specific lines of code. Labels help in identifying where to jump or branch in the program.
- Directives: These are instructions to the assembler (the program that converts assembly code into machine code) that do not correspond to machine instructions. For example, the directive
.dataindicates the start of a data segment.
Basic Structure of an Assembly Language Program
A typical assembly language program includes the following sections:
- Data Segment: This section defines the variables and data used in the program.
- Code Segment: This section contains the actual instructions that the CPU will execute.
- Stack Segment: This section is used for managing function calls and local variables.
Example of an Assembly Language Program
Consider a simple assembly language program that adds two numbers and stores the result. The following example uses a hypothetical assembly language:
.data
num1 DB 5
num2 DB 10
result DB 0
.code
MOV AL, num1
ADD AL, num2
MOV result, ALIn this program:
.dataindicates the start of the data segment.num1andnum2are defined as bytes (DB) with values 5 and 10.resultis defined to store the result of the addition.MOV AL, num1moves the value ofnum1into the registerAL.ADD AL, num2adds the value ofnum2to the value inAL.MOV result, ALmoves the result fromALto the variableresult.
Advantages of Using Assembly Language
Assembly language has several advantages:
- Efficiency: Programs written in assembly language can be more efficient than those written in high-level languages. This is because assembly language allows for fine control over the hardware.
- Speed: Assembly language programs can execute faster since they are closer to machine code.
- Hardware Control: Assembly language provides direct access to hardware features, which can be important for system programming and embedded systems.
Disadvantages of Using Assembly Language
Despite its advantages, assembly language also has disadvantages:
- Complexity: Writing programs in assembly language is often more complex and time-consuming than using high-level languages.
- Portability: Assembly language programs are not portable across different hardware architectures.
- Readability: Assembly code can be harder to read and understand compared to high-level languages.
Watch out: When writing assembly code, ensure that you understand the specific architecture you are working with. Instructions may differ between architectures.
Common Assembly Language Instructions
Some common assembly language instructions include:
- MOV: Moves data from one location to another.
- ADD: Adds two values.
- SUB: Subtracts one value from another.
- MUL: Multiplies two values.
- DIV: Divides one value by another.
- JMP: Jumps to a specified label in the code.
Addressing Modes in Assembly Language
Addressing modes specify how the operand of an instruction is accessed. Common addressing modes include:
- Immediate Addressing: The operand is a constant value specified in the instruction. For example, in
MOV AL, 5, 5 is the immediate value. - Direct Addressing: The operand is a memory address. For example,
MOV AL, [num1]moves the value from the memory address associated withnum1. - Register Addressing: The operand is in a register. For example,
MOV AL, BLmoves the value from registerBLto registerAL. - Indirect Addressing: The operand's address is specified by a register. For example, if
BXcontains the address ofnum1,MOV AL, [BX]moves the value from that address intoAL.
Remember: Different addressing modes provide flexibility in how data is accessed and manipulated in assembly language.
Conclusion
Assembly language is a powerful tool for programming that provides direct control over computer hardware. While it has its advantages, such as efficiency and speed, it also comes with complexities and limitations. Understanding the basic structure, components, and common instructions of assembly language is essential for programming at the machine level.
Check your understanding
- What is the main purpose of assembly language?
- List three components of assembly language.
- Explain the difference between immediate addressing and direct addressing.
- What are the advantages and disadvantages of using assembly language?