chapter 8 - Assembly Language Language

Assembly Language for x86 Processors

X86 Processor Architecture


Basic IA Computer Organization

  • Historical Context: Since the 1940s, the Von Neumann architecture has formed the backbone of computer organization.

  • Key Components:

    • Processor (CPU): The Central Processing Unit executes instructions and processes data.

    • Memory and Storage Devices: Holds data and instructions for execution.

    • I/O Devices: Facilitates input and output operations with the external environment.

  • System Interconnection:

    • The components are interconnected through one or more buses:

    • Data Bus: Carries data between processor and other components.

    • Address Bus: Transmits addresses to memory to specify where data is being read from or written to.

    • Control Bus: Carries control signals to coordinate operations.

  • Intel Architecture (IA): Refers to 32-bit (or i386) processor architectures, critical for compatibility and standardization.


IA-32 Registers

Processor Composition
  • Elements of the Processor:

    • Datapath: The pathway for data within the processor.

    • ALU (Arithmetic Logic Unit): Performs arithmetic and logic operations.

    • Registers: High-speed storage locations within the CPU.

    • Control Unit (CU): Generates control signals necessary for executing instructions.


Instruction Execution Cycle

  • The execution of instructions involves several steps to ensure correct processing and execution by the CPU.


Memory Address Space

  • Address Space Definition: The address space represents the set of memory locations (bytes) that are addressable within the architecture.

  • Addressing (in decimal and hex):

    • Maximum Address: 232−12^{32} - 1 corresponding to 0xFFFFFFFF0xFFFFFFFF in hexadecimal.

    • Minimum Address: 00 corresponding to 0x000000000x00000000 in hexadecimal.

    • Address Range: From 00 to 232−12^{32}-1.


Registers Overview

  • Types of Registers:

    • General-Purpose Registers:

    • Total: Eight 32-bit registers: EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP.

    • Functionality: Primarily used for arithmetic operations and data movements.

    • Special-Purpose Registers:

    • EIP (Extended Instruction Pointer): Contains the address of the next instruction to be executed.

    • EFLAGS (Extended Flags Register): Contains status and control flags pertaining to the execution state of the CPU.

    • Segment Registers: Six 16-bit registers to support segmented memory organization (Code, Data, and Stack segments).


General-Purpose Registers
  • Primary Use: Arithmetic calculations and data movement operations.

    • Example Operation:

    • Command: mov eax, 10 which moves the constant integer 10 into the register EAX.

  • Specific Uses for Registers:

    • EAX: Accumulator register, automatically used in multiplication and division instructions.

    • ECX: Counter register, automatically used by loop instructions.

    • ESP: Stack Pointer register, points to the top of the stack, utilized in push and pop operations.

    • ESI and EDI: Source Index and Destination Index registers, commonly used in string manipulation instructions.

    • EBP: Base Pointer register, references parameters and local variables in the stack.


Special-Purpose & Segment Registers
  • EIP: Points to the address of the next instruction to be executed in the instruction stream.

  • EFLAGS:

    • Contains multiple status and control flags; each represented as a single binary bit.

    • Significance: Allows detection of CPU operations success or failures through status flags.

  • Segment Registers:

    • Facilitate segmented memory architecture, dividing memory into distinct segments:

    • Code Segment: Contains executable instructions.

    • Data Segment: Holds variables and constants.

    • Stack Segment: Manages function calls and local storage.


Address Space Overview
  • Concept of Linear Address Space:

    • Each segment (Code, Data, Stack) is mapped to a common linear address space, allowing a programmer's view of memory as flat.

    • Base Address for Segments: All segments have a base address of 00.

  • Registers Overview:

    • EIP: Directing flow to the next instruction.

    • ESI and EDI: Assist in indexing into arrays and accessing data.

    • ESP and EBP: ESP leads to the stack's top, while EBP handles local variable accesses.


EFLAGS Register Details
  • Categories of Flags:

    1. Status Flags: Indicate outcomes of arithmetic or logical operations.

    2. Control and System Flags: Alter CPU operation behavior.

    • Programmers can manipulate individual bits within the EFLAGS register to control flow and logic in their programs.


Status Flags
  • Flags and their purposes:

    • Carry Flag: Activated when an unsigned arithmetic result exceeds the allowable range.

    • Overflow Flag: Set when a signed arithmetic result exceeds its bounds.

    • Sign Flag: Mirrors the sign bit of results; set when the output is negative.

    • Zero Flag: Indicates when a computed result equates to zero.

    • Auxiliary Carry Flag: Engaged if there is a carry from bit 3 to bit 4.

    • Parity Flag: Activated when the least significant byte has an even number of bits set to 1.


64-Bit Processors

  • Operation Modes:

    • Compatibility Mode: Runs existing 16-bit and 32-bit applications; Windows offers limited support for 32-bit applications in this mode.

    • 64-bit Mode: Exclusive to Windows operating in the 64-bit realm.

  • Environmental Setup:

    • Memory addresses can be 64 bits (actual usage is typically 48 bits).

    • Contains 16 general-purpose registers that can handle 64-bit operations, enhancing processing capacity.

    • Introduces an instruction pointer named RIP for referencing the next instruction location.


64-Bit General Purpose Registers

  • General Purpose Registers (32-bit): EAX, EBX, ECX, EDX, EDI, ESI, EBP, ESP.

  • 64-bit General Purpose Registers: RAX, RBX, RCX, RDX, RDI, RSI, RBP, RSP, and R8 through R15 characteristics.


Fetch-Execute Cycle: The Heartbeat of the CPU

  • Instruction Workflow:

    • Each instruction is fetched from memory and stored in an Instruction Register (IR).

    • The Instruction Pointer (EIP) or Program Counter (PC) signifies which instruction to fetch next.

    • After fetching, EIP/PC increments to prepare for the next instruction fetching.

    • Instruction Decoding happens to determine execution requirements and parameters.

    • The instruction is executed by the CPU based on the decoded information.


Instruction Execute Cycle Overview

  • Steps involved in the instruction execution cycle:

    • Obtain Instruction: Fetch instruction from storage (program memory).

    • Determine Actions: Analyze required operations and instruction size.

    • Locate Operand Data: Find and retrieve operand data needed for execution.

    • Compute:

    • Calculate result values and necessary flags.

    • Deposit Results: Store computation results for future use.

  • Cycle Continuity: This process is repeated infinitely for continuous instruction processing.