ASSEMBLY LANGUAGE 01
Assembly Language
Overview
- Definition: Assembly language is a specific set of instructions tailored for a particular computer system.
- Characteristics: It is classified as a Low Level Programming Language.
- Importance of Learning Assembly Language:
- Enhances algorithmic/coding skills.
- Provides an introduction to low-level programming concepts.
Industrial Applications of Assembly Language
- Uses:
- Device Drivers
- Real-Time Systems
- Embedded Computing Systems
- Companies: Utilized in products from IBM, Dell, HP, etc.
- Advantages:
- Programs are lighter and faster.
- Requires less memory.
- Examples of Assembly language applications include: Printers, Mice, USB devices.
Machine Instructions
- Definition: A machine instruction is a binary code that instructs a computer’s CPU to perform specific tasks.
- Representation: Machine instructions are often represented using hexadecimal numbers to minimize the amount of writing space needed.
Central Processing Unit (CPU) Architecture
- Registers:
- The following are the registers found inside the Intel 8086 CPU:
- General Purpose Registers: AX, AH, AL, BX, BH, BL, CX, CH, CL, DX, DH, DL
- Special Purpose Registers: CS, LP, SS, SP, BP, SI, DI, ES, DS
What are Registers?
- Definition: Registers are memory devices used for storing data.
- Specification: In the 8086 architecture, all registers are 16 bits in size.
General Purpose Registers in the 8086 CPU
- The 8086 CPU contains 8 General Purpose Registers:
- AX - Accumulator Register (divided into AH and AL)
- BX - Base Address Register (divided into BH and BL)
- CX - Count Register (divided into CH and CL)
- DX - Data Register (divided into DH and DL)
- SI - Source Index Register
- DI - Destination Index Register
- BP - Base Pointer
- SP - Stack Pointer
Segment Registers
- Definition: Segments are specific memory areas defined in a program.
- Types of Segment Registers:
- CS (Code Segment): Points to the segment containing the current program.
- DS (Data Segment): Generally points to the segment where variables are defined.
- ES (Extra Segment): An additional segment defined by the coder based on need.
- SS (Stack Segment): Points to the segment containing the stack.
Understanding the First Four Registers (AX, BX, CX, DX)
- These registers comprise two separate 8-bit registers.
- Example:
- If AX=0011000000111001b then:
- AH=00110000b
- AL=00111001b
- This means AX=AH+AL
Variable Declaration in 8086 Assembly Language
- Comparison with C/C++:
- C/C++ Example:
int x = 10;double y = 20.0;- 8086 Assembler Data Types:
- DB (Define Byte) – 1 byte
- DW (Define Word) – 2 bytes
- DD (Define Double Word) – 4 bytes
- DQ (Define Quad Word) – 8 bytes
Examples of Variable Declarations
- Example for DB (Define Byte):
- For smaller values:
Var_Name Type ValueA DB 9Message DB 'HelloWorld'
- Example for DW (Define Word):
- For larger values:
Var_Name Type ValueVar DW 1122H
String Variables in Assembly Language
- Declaration:
STR1 DB 'Hello World'STR2 DB 'Hello World', '$' // '$' indicates end of stringSTR3 DB 10,13, 'Hello World', '$' // 10, 13 is for new line
Array Declarations in 8086 Assembly Language
- Example of Array Declaration:
Name Type Valuea DB 1h,2h,3Fh,7Fhb DB 1111h,2222h,3333h
Accessing Array Values
- Accessing Elements in an Array:
- Given array example:
a DB 22h,23h,24h,25h- Locations:
a[0] -> 22 a[1] -> 23 a[2] -> 24 a[3] -> 25
- Example Instructions:
MOV AL, a[2]- Using Index Registers:
Declaration using DUP in 8086 Assembly Language
- Usage:
DUP or dup stands for duplicate.
- Examples:
X DB 3 DUP (7)- Similar to:
X DB 7,7,7 Y DB 3 DUP (5,6)- Similar to:
MOV Instruction in 8086 Assembly Language
- Definition: The MOV instruction copies the second operand (the source) to the first operand (the destination).
- Types of Operand Supported:
MOV Reg, MemoryMOV Memory, RegMOV Reg, RegMOV Memory, ImmediateMOV Reg, Immediate
- Note:
MOV Mem, Mem is not supported.
Operand Types
- Registers Used:
- For example: AX, BX, CX, DX, AL, DI, AH, etc.
- Immediate Values:
- Examples:
7, -11, 4FH (Hexadecimal)
- Memory Access:
- Examples:
[BX] or [BX+SI] + Displacement BX, SI, DI, BP can be used to access memory.
Examples of MOV Instructions
MOV AX, BX // Register to RegisterMOV [BX], AX // Memory to RegisterMOV AX, [BX] // Register to MemoryMOV [BX+SI], 5 // Memory to ImmediateMOV AX, 5 // Register to Immediate
Arithmetic Operations
- Arithmetic Operation Examples in C/C++/Java/C#:
- Addition, Subtraction, Multiplication
- Example code:
int a = 11;
int b = 14;
int sum = 0, sub = 0, mul = 0;
sum = a + b; // sum = 25
sub = a - b; // sub = -3
mul = a * b; // mul = 154
Arithmetic Instructions in 8086
- Includes Multiple Operations:
- Addition
- Subtraction
- Multiplication
- Division
Addition Syntax in 8086
- Syntax:
ADD Destination, Source - Result: The outcome gets stored in the destination.
- Example:
- Case Study:
- If AX=11H and BX=14H, then:
ADD AX, BX results in AX = 25H
Supported Operand Types for Addition
- Types of Operands:
- Operands:
- Register to Register:
ADD AX, BX - Memory to Register:
ADD [BX], AX - Register to Memory:
ADD AX, [BX] - Memory to Immediate:
ADD [BX], 7 - Register to Immediate:
ADD CL, 40H
Example of Addition in Assembly Language
- Instructions:
MOV AX, 11H // Set AX = 11HMOV BX, 14H // Set BX = 14HADD AX, BX // AX now contains 25H
- Binary Representation:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 - Representations for
11H, 14H, and 25H respectively.