CodeToLive

Introduction to Assembly Language

Assembly language is a low-level programming language that is specific to a particular computer architecture. It provides a human-readable representation of machine code, making it easier to write programs that directly interact with hardware.

What is Assembly Language?

Assembly language is a symbolic representation of machine code. Each assembly language instruction corresponds to one machine language instruction. Unlike high-level languages, assembly is not portable between different processor architectures.

Why Learn Assembly?

Basic Concepts

Assembly language programming involves working with:

Simple x86 Assembly Example

Here's a simple "Hello, World!" program in x86 assembly for Linux:


section .data
    hello db 'Hello, World!', 0xA  ; String with newline
    len equ $ - hello              ; Length of the string

section .text
    global _start

_start:
    ; Write the string to stdout
    mov eax, 4        ; sys_write system call
    mov ebx, 1        ; file descriptor (stdout)
    mov ecx, hello    ; pointer to string
    mov edx, len      ; string length
    int 0x80          ; call kernel

    ; Exit the program
    mov eax, 1        ; sys_exit system call
    mov ebx, 0        ; exit status
    int 0x80          ; call kernel
      

Assembling and Linking

To assemble and link the program using NASM on Linux:


nasm -f elf hello.asm    # Assemble to object file
ld -m elf_i386 -s -o hello hello.o  # Link to create executable
./hello                  # Run the program
      

Common x86 Registers

Register Purpose
EAX Accumulator (used for arithmetic operations)
EBX Base (used as pointer to data)
ECX Counter (used in loops)
EDX Data (used in I/O operations)
ESI Source index (string operations)
EDI Destination index (string operations)
ESP Stack pointer
EBP Base pointer (stack frames)

Basic Instructions

Instruction Description
MOV Move data between registers/memory
ADD/SUB Add/Subtract values
INC/DEC Increment/Decrement a value
JMP Unconditional jump
CMP Compare two values
CALL/RET Call and return from subroutines
PUSH/POP Stack operations

Advantages of Assembly

Disadvantages of Assembly

Next Steps

Now that you understand the basics of assembly language, you can explore more advanced topics: