0% found this document useful (0 votes)
29 views

7 Introduction To Assembly Language

Assembly Language introduction

Uploaded by

rkeditors47
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

7 Introduction To Assembly Language

Assembly Language introduction

Uploaded by

rkeditors47
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to Assembly Language

Assembly language is a low-level programming language that is closely related to machine code,
the native language of a computer's central processing unit (CPU). Unlike high-level languages
(such as Python or Java), which abstract away the hardware details, assembly language provides a
direct interface to the hardware, offering fine-grained control over the CPU's operations. It is used
primarily in systems programming, embedded systems, and performance-critical applications
where understanding and manipulating the hardware is essential.

History and Evolution


The evolution of assembly language is intertwined with the history of computing. Early computers
were programmed directly in machine code, which consists of binary instructions specific to a
CPU's architecture. As computing systems grew in complexity, the need for a more human-
readable form of programming led to the development of assembly language.
Key milestones in the development of assembly language:
1. 1940s-1950s: Early computers like the ENIAC were programmed using machine code.
2. 1950s: The advent of the first symbolic assembly languages, such as the Manchester Mark
I’s symbolic assembler, allowed programmers to use mnemonic codes instead of binary.
3. 1960s-1970s: Assembly languages became more modern, with the development of
assemblers and macro assemblers, which automated some aspects of coding.
4. 1980s-present: Despite the rise of high-level languages, assembly language remains
crucial in areas requiring direct hardware manipulation, such as embedded systems and
operating systems.

Structure of Assembly Language


Assembly language consists of a series of mnemonic codes and symbols that represent instructions
and data. Each instruction corresponds directly to a machine code instruction, allowing for precise
control over the hardware.
Key components:
• Instructions: Represent the operations to be performed (e.g., MOV, ADD, SUB).
• Operands: Specify the data or the addresses of the data on which the instructions operate.
• Labels: Mark locations in code, useful for jumps and loops.
• Directives: Provide instructions to the assembler on how to process the code (e.g., DB,
DW).
• Comments: Allow programmers to annotate code for readability (preceded by ; in many
assembly languages).
Assembler and Linker
To convert an assembly language program into an executable, two main tools are used:
1. Assembler: Translates assembly language code into machine code. Common assemblers
include NASM (Netwide Assembler), MASM (Microsoft Assembler), and GAS (GNU
Assembler).
2. Linker: Combines object files generated by the assembler into a single executable. It
resolves references between different modules and addresses.

Advantages and Disadvantages


Advantages:
• Performance: Provides the highest possible performance by allowing direct control over
hardware.
• Size: Generates very compact code, which is essential for embedded systems with limited
memory.
• Control: Offers complete control over the system's resources and operations.
Disadvantages:
• Complexity: Requires detailed knowledge of the hardware and is more complex to write
and debug.
• Portability: Assembly language is not portable across different CPU architectures.
• Development Time: Takes longer to develop and maintain compared to high-level
languages.

Applications of Assembly Language


Despite the dominance of high-level languages, assembly language is still used in specific
scenarios:
• Embedded Systems: For programming microcontrollers and other embedded devices.
• Operating Systems: Critical parts of operating systems are often written in assembly for
efficiency.
• Performance-Critical Applications: Applications requiring maximum performance, such
as game engines and high-frequency trading systems.
• Device Drivers: For interacting directly with hardware.

Basic Concepts and Terminology


1. Instruction: A command for the CPU to perform a specific operation. Examples include
MOV, ADD, and SUB.
2. Opcode: The part of the instruction that specifies the operation to be performed. For
example, in the instruction MOV AX, BX, MOV is the opcode.
3. Operands: The data or the addresses of data that the instruction operates on. In MOV AX,
BX, AX and BX are operands.
4. Registers: Small, fast storage locations within the CPU used to hold data and instructions.
Common registers include AX, BX, CX, and DX in x86 assembly.
5. Memory Addressing: Refers to how data is accessed in memory. Common modes include
immediate, direct, indirect, and indexed addressing.
6. Assembler: A tool that converts assembly language code into machine code.
7. Labels: Names assigned to specific lines or sections of code, used for jump and branch
instructions.

Basic Syntax
Assembly language syntax can vary between different processors and assemblers, but the basic
structure includes the following elements:
• Labels: Used to identify a location in code.
• Instructions: The operation to be performed.
• Operands: The data or addresses the instruction will operate on.
• Directives: Commands to the assembler that are not translated into machine code but affect
the assembly process.

Simple Assembly Instructions


1. MOV: Move data from one place to another.
• Syntax: MOV destination, source
• Example: MOV AX, BX (Moves the contents of register BX into register AX).
2. ADD: Add two operands.
• Syntax: ADD destination, source
• Example: ADD AX, 1 (Adds 1 to the contents of register AX).
3. SUB: Subtract one operand from another.
• Syntax: SUB destination, source
• Example: SUB AX, BX (Subtracts the contents of register BX from register AX).
4. INC: Increment the operand by 1.
• Syntax: INC operand
• Example: INC AX (Increments the contents of register AX by 1).
5. DEC: Decrement the operand by 1.
• Syntax: DEC operand
• Example: DEC BX (Decrements the contents of register BX by 1).
6. JMP: Jump to a specified label or address.
• Syntax: JMP label
• Example: JMP START (Jumps to the label START).
7. CMP: Compare two operands.
• Syntax: CMP operand1, operand2
• Example: CMP AX, BX (Compares the contents of AX and BX).
8. JE/JNE: Conditional jumps based on the result of a comparison.
• Syntax: JE label / JNE label
• Example: JE EQUAL (Jumps to EQUAL if the previous comparison resulted in
equality).

Example Program
Here's a simple assembly program that demonstrates some of these instructions. This program adds
two numbers and stores the result.
Understanding the Example
1. Data Section:
• section .data is where we define variables. num1, num2, and result are defined
here.
2. Text Section:
• section .text contains the code.
• global _start makes _start the entry point.
• _start: is a label marking the beginning of the program.
3. Instructions:
• mov al, [num1] loads the value of num1 into register AL.
• add al, [num2] adds the value of num2 to AL.
• mov [result], al stores the result in result.
• The final three instructions (mov eax, 60, xor edi, edi, syscall) are used to exit the
program.

You might also like