10 Examples of Assembly Language

Assembly language is a low-level programming language used to write machine-level code in human-readable form. It communicates directly with a computer’s hardware and performs specific tasks by writing instructions that are interpreted by the CPU.

Examples of Assembly Language

The following are the common examples of assembly language:

1. Adding Two Numbers

Adding two numbers in assembly involves using basic arithmetic operations:

Steps:

  • Load the first number into a register (e.g., AX).
  • Load the second number into another register (e.g., BX).
  • Add them using the ADD instruction.
  • Store the result in a register or memory.

Example Use:

Calculating the sum of values in mathematical or financial applications.

2. Subtracting Two Numbers

Subtraction is performed by loading two numbers and using the SUB instruction:

Steps:

  • Load a number into AX.
  • Load another number into BX.
  • Subtract BX from AX.
  • Save the result back in a register or memory.

Example Use:

Deducting costs in billing software or implementing decrement operations in loops.

3. Multiplication

Assembly supports multiplication through commands like MUL:

Steps:

  • Load a number into a register.
  • Multiply it with another register or memory value.
  • Store the product in a result register.

Example Use:

Used in scaling graphics or performing proportional adjustments in calculations.

4. Data Movement

The MOV instruction transfers data between registers or between a register and memory:

Steps:

  • Load a value from memory into a register.
  • Copy it to another register.
  • Move it back to memory if needed.

Example Use:

Loading configuration values into the processor or swapping variables in code.

5. Creating Loops

Loops in an assembly are created using conditional or unconditional jumps:

Steps:

  • Set up a counter.
  • Use the LOOP instruction to iterate until the counter reaches zero.

Example Use:

Repeatedly adding numbers, processing arrays, or applying algorithms.

6. Input and Output Operations

Assembly language can read inputs and display outputs via system calls or hardware instructions:

Steps:

  • Use the IN instruction to read data from input devices.
  • Use the OUT instruction to send data to output devices.

Example Use:

Reading a key pressed on the keyboard or displaying a number on the screen.

7. String Manipulation

String operations include copying, comparing, or searching within strings:

Steps:

  • Use MOV to copy string data.
  • Use CMP to compare characters.
  • Iterate over the string for other operations.

Example Use:

Managing user input in text-based software or formatting output for reports.

8. Conditional Jumps

Conditional jumps are used for decision-making in programs:

Steps:

  • Compare two values using CMP.
  • Use JE (Jump if Equal), JG (Jump if Greater), or similar instructions based on the result.

Example Use:

Creating branching logic like “if-else” statements in algorithms.

9. Register Manipulation

Registers are directly controlled to optimize computations:

Steps:

  • Clear a register using XOR with itself.
  • Initialize or modify values as needed.

Example Use:

Preparing parameters for a subroutine or managing program state.

10. Interrupt Handling

Interrupts interact with system-level processes like I/O handling:

Steps:

  • Use the INT instruction with an interrupt number.
  • Execute system calls (e.g., displaying text).

Example Use:

Read a file or handle user commands in operating systems.

Leave a Comment