Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Assembly Tutorial

Introduction to Assembly

Assembly language is a low-level programming language that is specific to a computer architecture. It provides a way to write instructions that the computer's processor can execute directly. Assembly language is often used for performance-critical applications and for interacting with hardware directly.

Basic Structure of an Assembly Program

An assembly program typically consists of three main sections: data, bss, and text.

  • Data section: This section is used for declaring initialized data or constants.
  • BSS section: This section is used for declaring variables.
  • Text section: This section contains the actual code that will be executed.
section .data
    msg db 'Hello, World!',0

section .bss
    res resb 1

section .text
    global _start

_start:
    ; Code goes here
                

Registers

Registers are small storage locations within the CPU that are used to perform operations and store intermediate results. Commonly used registers in x86 assembly include:

  • EAX: Accumulator register
  • EBX: Base register
  • ECX: Counter register
  • EDX: Data register
  • ESI: Source index for string operations
  • EDI: Destination index for string operations
  • EBP: Base pointer for stack frames
  • ESP: Stack pointer

Basic Instructions

Assembly language instructions are used to perform operations on data and control the flow of the program. Here are some basic instructions:

  • MOV: Move data from one location to another
  • ADD: Add two values
  • SUB: Subtract one value from another
  • MUL: Multiply two values
  • DIV: Divide one value by another
  • PUSH: Push a value onto the stack
  • POP: Pop a value off the stack
section .data
    num1 dd 10
    num2 dd 20
    result dd 0

section .text
    global _start

_start:
    mov eax, [num1]
    add eax, [num2]
    mov [result], eax
    ; Exit the program
    mov eax, 1
    int 0x80
                

Compilation and Execution

To compile and execute an assembly program, you need an assembler and a linker. Here is an example using NASM (Netwide Assembler) and GCC (GNU Compiler Collection) on a Unix-based system:

# Assemble the program
nasm -f elf64 program.asm

# Link the object file to create an executable
gcc -o program program.o

# Run the executable
./program
                
Hello, World!

Example Program: Hello World

Here is a complete example of a "Hello, World!" program in assembly language:

section .data
    msg db 'Hello, World!',0

section .text
    global _start

_start:
    ; Write the message to the screen
    mov eax, 4
    mov ebx, 1
    mov ecx, msg
    mov edx, 13
    int 0x80

    ; Exit the program
    mov eax, 1
    xor ebx, ebx
    int 0x80
                

To compile and run this program, use the following commands:

# Assemble the program
nasm -f elf32 hello.asm

# Link the object file to create an executable
ld -m elf_i386 -s -o hello hello.o

# Run the executable
./hello
                
Hello, World!