assembly_basics

Assembly Language Guide for Windows 64-bit

Introduction

This guide is designed to help you learn assembly language from the ground up, focusing on the 64-bit Windows architecture. We’ll cover everything from setting up your environment to understanding registers and writing basic programs. High-level programming language comparisons are included to make concepts more understandable.

Table of Contents

  1. Setting Up the Environment
  2. Basic Syntax and Structure
  3. Registers
  4. Simple Input and Output
  5. Arithmetic Operations
  6. Control Flow
  7. Functions and Calling Conventions
  8. Learning Strategies

Setting Up the Environment

  1. Install NASM: Download and install NASM from the official site.
  2. Install MinGW or GCC: Download and install MinGW or GCC for Windows.
  3. Set up IntelliJ IDEA: Use IntelliJ IDEA with the necessary plugins to support assembly language.

Basic Syntax and Structure

An assembly program is divided into sections:

Example

section .data
    hello db 'Hello, world!', 0

section .text
    global _start

_start:
    ; Your code here

Registers

Registers are limited storage locations in the CPU, each with specific purposes.

General-Purpose Registers

Segment Registers

Example

C Code:

int main() {
    int a = 5;
    int b = 10;
    int sum = a + b;
    return sum;
}

Assembly Code:

section .text
global _start

_start:
    mov rax, 5       ; a = 5
    mov rbx, 10      ; b = 10
    add rax, rbx     ; sum = a + b
    mov rdi, rax     ; return sum
    call ExitProcess

Simple Input and Output

Printing a String

C Code:

#include <stdio.h>
int main() {
    printf("Hello, World!");
    return 0;
}

Assembly Code:

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

section .text
global _start
extern GetStdHandle
extern WriteConsoleA
extern ExitProcess

_start:
    mov rcx, -11           ; STD_OUTPUT_HANDLE
    call GetStdHandle

    mov rcx, rax           ; handle to stdout
    mov rdx, hello         ; pointer to string
    mov r8, 13             ; string length
    lea r9, [rsp+8]        ; dummy variable
    call WriteConsoleA

    xor rcx, rcx           ; exit code 0
    call ExitProcess

Arithmetic Operations

Addition Example

C Code:

int add(int a, int b) {
    return a + b;
}

Assembly Code:

section .text
global _start

_start:
    mov rax, 5       ; a = 5
    mov rbx, 10      ; b = 10
    add rax, rbx     ; result = a + b
    call ExitProcess

Control Flow

Loop Example

C Code:

int sum_n(int n) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    return sum;
}

Assembly Code:

section .text
global _start

_start:
    mov rcx, 10      ; n = 10
    mov rax, 0       ; sum = 0
    mov rbx, 1       ; i = 1

loop_start:
    add rax, rbx     ; sum += i
    inc rbx          ; i++
    cmp rbx, rcx
    jle loop_start   ; if i <= n, repeat

    call ExitProcess

Functions and Calling Conventions

Function Example

C Code:

int multiply(int a, int b) {
    return a * b;
}
int main() {
    int result = multiply(5, 10);
    return result;
}

Assembly Code:

section .text
global _start
extern multiply

_start:
    mov rcx, 5       ; a = 5
    mov rdx, 10      ; b = 10
    call multiply
    ; result is in rax
    call ExitProcess

multiply:
    imul rax, rcx, rdx
    ret

Learning Strategies