← Back to Articles

Getting Started with Assembly

What is Assembly Language?

Assembly language is a low-level programming language that provides a direct correspondence between the language instructions and the processor's machine code instructions. Unlike high-level languages like Python or JavaScript, assembly language gives you direct control over the CPU.

Each line of assembly code typically corresponds to a single machine instruction that the CPU can execute. This makes assembly language:

  • Very fast - no interpretation or compilation overhead
  • Memory efficient - complete control over resource usage
  • Hardware specific - written for a particular processor architecture

Why Learn Assembly?

Even though most modern software is written in high-level languages, learning assembly is valuable because:

  • Understanding: You'll understand how computers work at a fundamental level
  • Performance: Critical code sections can be optimized in assembly
  • Debugging: Helps debug and reverse engineer compiled code
  • Embedded Systems: Essential for programming microcontrollers and embedded devices
  • Security: Important for understanding exploits and vulnerabilities

The Basic Components

Before diving into assembly programming, you need to understand these key components:

1. Registers

Registers are small, fast storage locations inside the CPU. In our compiler, we have registers R0 through R12. Think of them as variables that the CPU can access instantly.

2. Memory

Memory is where your program and data are stored. Unlike registers, memory is slower to access but much larger. Our compiler simulates 1024 memory locations.

3. Instructions

Instructions are the commands that tell the CPU what to do. Examples include MOV (move data), ADD (addition), and SUB (subtraction).

4. Program Counter (PC)

The program counter keeps track of which instruction should execute next. It automatically increments after each instruction.

Your First Assembly Program

Let's write a simple program that adds two numbers:

MOV R0, #5      // Put 5 in register R0
MOV R1, #10     // Put 10 in register R1
ADD R2, R0, R1  // Add R0 and R1, store result in R2
HALT            // Stop the program

When you run this program:

  1. R0 becomes 5
  2. R1 becomes 10
  3. R2 becomes 15 (the sum of R0 and R1)
  4. The program halts

Comments in Assembly

Always use comments to explain what your code does. In our compiler, comments start with //:

// This is a comment
MOV R0, #42     // This is also a comment

Comments are essential because assembly code can be difficult to read. A well-commented program is much easier to understand and maintain.

Number Formats

In assembly, you can use immediate values (constants) by prefixing them with #:

MOV R0, #100    // Decimal 100
MOV R1, #0      // Zero
MOV R2, #-5     // Negative numbers work too

Next Steps

Now that you understand the basics, you're ready to explore:

  • Different types of instructions
  • How to work with memory
  • Control flow and branching
  • More complex programs

Continue to the next article to learn about registers in detail!