Assembly language is a low-level programming language that provides a symbolic representation of a computer's machine code instructions. It allows programmers to write programs that are very close to the hardware level, giving them direct control over the computer’s operations.
In an assembly language program, there are several key elements that define how the program is written and executed. Below is a breakdown of the main elements of assembly language:
Mnemonics are symbolic representations of machine-level instructions. These are the core of an assembly language program and correspond to specific machine instructions that the CPU can execute.
MOV: Move data between registers or between a register and memory.ADD: Add two operands.SUB: Subtract two operands.MUL: Multiply two operands.JMP: Jump to a specific instruction (used for loops or branching).CMP: Compare two values.Example:
MOV AX, 5 ; Move the value 5 into the AX register
ADD AX, 10 ; Add 10 to the value in the AX register
Registers are small, fast storage locations within the CPU. They are used to store data, addresses, or control information temporarily during the execution of a program. Each processor has its own set of registers, but there are some common types of registers used in most assembly languages.
Common Register Types:
AX, BX, CX, DX): Used for storing temporary data during execution.SI, DI): Used for addressing data in memory.SP, BP): Used for stack management.FLAGS): Store status flags (such as carry, zero, sign, etc.) that indicate the result of an operation.Example:
MOV AX, 100 ; Load 100 into the AX register
MOV BX, 50 ; Load 50 into the BX register
ADD AX, BX ; Add BX to AX, result is stored in AX
Labels are used as markers to indicate specific locations in your program. They are often used for branching (e.g., loops and jumps). Labels are not instructions, but they provide a way for the program to "jump" to different sections based on conditions.
Example:
start: ; This is a label
MOV AX, 5
ADD AX, 3
JMP start ; Jump back to the 'start' label (infinite loop)
Directives are special commands that tell the assembler how to process the assembly code. They are not executed by the CPU but are essential for organizing and managing your program’s memory and execution.
Common Directives:
.data: Marks the beginning of the data segment, where data is defined..text: Marks the beginning of the code segment, where executable instructions are written..bss: Defines a section for uninitialized variables..org: Defines the starting address for code or data in memory..equ: Defines a constant or symbol..db, .dw, .dd: Reserve space for bytes, words, or double words of data..end: Marks the end of the source code.Example:
.data
message DB 'Hello, World!', 0 ; Declare a string
num1 DW 1234 ; Declare a word-sized variable
.text
; Code goes here...
Constants are values that are predefined and do not change during the execution of the program. In assembly language, constants are often defined using EQU (equate) directive or can be hardcoded into the program.
Example:
MAX_VALUE EQU 100 ; Define a constant named MAX_VALUE with a value of 100
Constants are typically used in situations where a specific value must be reused throughout the program.
Memory addressing refers to how data is accessed from memory. In assembly language, there are several addressing modes that dictate how operands are accessed.
Common Addressing Modes:
MOV AX, 5 ; 5 is the immediate value
MOV AX, BX ; Copy the value of BX into AX
MOV AX, [1000h] ; Load the value at memory address 1000h into AX
MOV AX, [BX] ; Load the value at the memory address in BX into AX
MOV AX, [SI + 4] ; Load the value at the memory address (SI + 4) into AX
Comments are used to add descriptive notes within the code. These do not affect the execution of the program, but they help explain what the code is doing. In assembly language, comments are typically denoted by a semicolon (;).
Example:
MOV AX, 5 ; Load the value 5 into register AX
ADD AX, 10 ; Add 10 to AX
Everything after the semicolon on that line is treated as a comment.
Assembly language programs often include control flow instructions that alter the normal sequence of execution. These include conditional and unconditional jumps (branches), which allow loops and conditional operations.
Common Jump Instructions:
JMP: Unconditional jump to a specific label.JE / JZ: Jump if equal or zero (based on a flag).JNE / JNZ: Jump if not equal or not zero.JG / JNLE: Jump if greater.JL / JNGE: Jump if less.Example:
CMP AX, BX ; Compare AX with BX
JE equalLabel ; Jump to 'equalLabel' if AX == BX
JMP notEqual ; Jump to 'notEqual' otherwise
In assembly language, subroutines (or functions) are defined as a series of instructions that can be called from different parts of the program. A subroutine is typically entered via a CALL instruction, and the return is done using the RET instruction.
Example:
; Subroutine definition
myFunction:
MOV AX, 100 ; Perform some task
RET ; Return from the subroutine
; Calling the subroutine
CALL myFunction ; Call 'myFunction' subroutine
Input and output operations are handled by interacting with the operating system or hardware through specific instructions or system calls. In assembly language, this is usually done using interrupts.
Example (for DOS):
MOV AH, 09h ; DOS interrupt to print string
LEA DX, message ; Load address of the string into DX
INT 21h ; Call DOS interrupt to print the string
The elements of assembly language—such as mnemonics, registers, labels, directives, constants, memory addressing modes, comments, control flow, subroutines, and I/O operations—form the building blocks for writing programs that can directly control hardware. While assembly language is low-level and machine-specific, it offers maximum control over system resources, making it useful in systems programming, embedded systems, and performance-critical applications. Understanding these elements helps programmers to write efficient, optimized, and hardware-near code.
Open this section to load past papers