x86 MASM Chapter 1

1.1 Welcome to Assembly Language
  • Focus: Programming x86 processors compatible with Intel and AMD. These processors utilize a complex instruction set computer (CISC) architecture, which means a single instruction can perform multiple low-level operations like loading from memory, an arithmetic operation, and a memory store.

  • Software Requirement: Use the latest Microsoft Macro Assembler (MASM), included with Microsoft Visual Studio. MASM provides high-level features like macros, loops, and procedures while remaining a low-level tool.

  • Assembly Language Significance:

    • It is the oldest programming language and the one closest to machine language (00s and 11s).

    • It provides a symbolic representation of the machine instructions that a specific CPU executes.

    • Allows direct access to computer hardware, requiring a good understanding of the architecture, memory addressing, and how the operating system manages resources like processes and I/O.

1.1.1 Educational Value
  • Learning Outcomes:

    • Understand exactly how a CPU executes code at the hardware level.

    • Moving beyond "toy" simulators to use an industrial-grade tool like MASM helps bridge the gap between academic theory and real-world system programming.

1.1.2 Importance of Low-Level Programming
  • Need for Awareness of Hardware:

    • High-level languages like Java or Python abstract away memory management. In Assembly, you manage the stack and registers manually. This knowledge is crucial for finding efficiency bottlenecks or security vulnerabilities like buffer overflows.

1.1.3 Prerequisites and Beginning Steps
  • Definition of Assemblers and Linkers:

    • Assembler: A utility program that converts source code files (containing mnemonics like MOV or ADD) into object files. The object file consists of machine-readable binary code, but it is not yet executable.

    • Linker: This utility takes one or more object files created by the assembler and combines them with runtime libraries to produce a single executable program (.exe.exe file). It resolves external references to functions and variables.

  • Debugger: A tool used to pause program execution and inspect the current state of CPU registers (EAXEAX, EBXEBX, etc.) and system memory addresses. This is vital for understanding "invisible" logic errors.

1.1.5 Types of Programs with MASM
  • Program Types:

    • 32-Bit Protected Mode: Programs run in a segregated memory space provided by modern OSs, preventing one program from crashing another.

    • 64-Bit Mode: Utilizes the full range of 64-bit registers and a much larger memory address space (up to 2642^{64} bytes).

1.1.7 Learning Goals
  • Boolean Logic: Understanding how hardware gates (AND, OR, NOT, XOR) implement mathematical logic and flow control within the CPU.

  • Memory Management: Learning about segmented memory, paging, and how the CPU uses a stack for function calls.

  • Data Representation: Understanding how data is stored as signed/unsigned integers. For example, a 16-bit word can represent an unsigned range from 00 to 65,53565,535.

1.1.8 Relationships in Programming Languages
  • Machine Language vs. Assembly Language:

    • Machine language is purely numeric (e.g., B8 01 00B8 \ 01 \ 00). Assembly uses mnemonics (e.g., MOV EAX, 1) to make it human-readable. There is almost always a one-to-one correspondence between an assembly instruction and a machine code instruction.

1.1.9 Portability of Assembly Language
  • Portability: A language is portable if it can compile and run on different CPU architectures (like ARM vs. x86). Assembly is not portable because it is tied to the specific Instruction Set Architecture (ISA) of the processor it was written for.

1.2 Virtual Machine Concept
1.2.1 Overview
  • Computer systems are viewed as a series of layers. Each layer is a "Virtual Machine" (VM) that simplifies the layer below it.

  • Native Execution: The bottom layer consists of electrical circuits that directly perform logic. Each higher layer provides a level of abstraction, making programming easier but further removed from the actual hardware.

1.2.2 Interpretation vs. Translation:
  • Interpretation: As a program runs, a software tool reads each instruction and executes it immediately (e.g., JVM for Java). This is flexible but carries a performance penalty.

  • Translation: The entire program is converted into a lower-level language (like machine code) before it ever runs (e.g., C++ compiler). This leads to faster execution times because no decoding happens at runtime.

1.2.4 Levels of Virtual Machines
  • Level 1 (Digital Logic): The physical hardware consisting of transistors and gates.

  • Level 2 (Instruction Set Architecture - ISA): The set of raw machine instructions defined by the CPU manufacturer.

  • Level 3 (Operating System): Provides services like file management and memory allocation through system calls.

  • Level 4 (Assembly Language): The first human-readable level using mnemonics to represent Level 2 instructions.

  • Level 5 (High-Level Languages): Languages like C++ and Java that use algebraic notation and structured syntax.

1.3 Data Representation
1.3.1 Number Systems
  • Binary (Base-2): Consists of bits (00 or 11). Each position represents a power of 22.

  • Hexadecimal (Base-16): Used as a compact way to represent binary. One hex digit represents exactly 44 bits (a nibble).

1.3.3 Integer Storage and Sizes
  • Byte: 88 bits (28=2562^8 = 256 distinct values).

  • Word: 1616 bits (22 bytes).

  • Doubleword (Dword): 3232 bits (44 bytes).

  • Quadword (Qword): 6464 bits (88 bytes).

1.4 Boolean Expressions
1.4.1 Basics of Boolean Algebra
  • NOT (Inverter): Reverses the input (11 becomes 00).

  • AND: The output is true (11) only if all inputs are true.

  • OR: The output is true (11) if at least one input is true.

  • XOR (Exclusive OR): The output is true (11) only if the inputs are different (one is 00 and one is 11).