Assembly Language (Copy)
Chapter 4.2: Assembly Language
Introduction to Assembly Language
- Machine Code Context:
- Machine code, composed of binary digits, is the only language directly understood by a CPU.
- Each instruction is tailored to a specific CPU type, forming the Instruction Set Architecture (ISA) for that processor.
- Instructions are often converted into hexadecimal format for easier human understanding.
- Assembly Language:
- Introduced to make programming more accessible by replacing binary instructions with readable mnemonics.
- Each assembly language instruction corresponds closely to its equivalent machine code, offering a low-level but human-readable way to interact with hardware.
Structure of Assembly Language
Basic Elements
- Mnemonic Codes:
- Represent operations (opcodes) such as
ADD(addition) orMOV(move data). - Mnemonics improve readability and reduce errors compared to binary instructions.
- Represent operations (opcodes) such as
- Operands:
- Specify the data or memory locations used by the operation.
- Can reference constants, registers, or memory addresses.
Opcode and Operand Relationship:
- Opcode: The operation to perform (e.g.,
LDA,SUB). - Operand: The data or address involved in the operation.
- Example:
LDA 500means “load data from memory address 500 into the accumulator.”
- Example:
Assembly Language Syntax:
- Composed of three components:
- Label: Optional and represents a memory location (e.g.,
LOOP:). - Instruction: The operation to perform (e.g.,
ADD). - Comment: Starts with a semicolon (
;) and explains the instruction for clarity. - Example:
LOOP: LDA 200 ; Load data from address 200 ADD 201 ; Add data from address 201 JMP LOOP ; Repeat the loop
- Label: Optional and represents a memory location (e.g.,
Translation: Role of the Assembler
Assembler Functions
- Converts assembly language into machine code so the CPU can execute it.
- Ensures that mnemonics are valid and match the CPU’s instruction set.
- Outputs machine code in two formats:
- Executable Machine Code: Ready for immediate execution.
- Object Code: Requires linking with additional modules before execution.
Two-Pass Assembly Process
- Pass 1:
- Scans the assembly program to identify labels and allocate memory addresses.
- Creates a symbol table linking labels to their respective memory locations.
- Pass 2:
- Translates mnemonics into machine code using the symbol table.
- Resolves forward references (e.g., labels used before they are defined).
Types of Instructions in Assembly Language
1. Data Movement Instructions
- Transfer data between memory, registers, and the accumulator (ACC).
- Examples:
LDA #5: Load the constant value 5 into the accumulator.MOV R1, R2: Move data from register R2 to R1.STO 300: Store the content of the accumulator at memory address 300.
2. Arithmetic Instructions
- Perform mathematical operations.
- Examples:
ADD 500: Add data from memory address 500 to the accumulator.SUB 400: Subtract data from memory address 400.
3. Logical and Bitwise Instructions
- Perform comparisons and manipulate individual bits.
- Examples:
AND 200: Perform a bitwise AND operation with data at address 200.OR 300: Perform a bitwise OR operation with data at address 300.
4. Branching and Control Instructions
- Control program flow, enabling decisions and loops.
- Examples:
JMP LABEL: Jump to the instruction labeledLABEL.JZ LABEL: Jump toLABELif the accumulator contains zero.
5. Input/Output Instructions
- Handle communication with external devices.
- Examples:
IN: Read input into the accumulator.OUT: Send accumulator data to an output device.
Addressing Modes
1. Immediate Addressing
- The operand is a constant value.
- Example:
LDA #5loads the constant value 5 into the accumulator.
- Example:
2. Direct Addressing
- The operand is a memory address directly.
- Example:
LDA 200loads data from memory address 200 into the accumulator.
- Example:
3. Indirect Addressing
- The operand specifies a memory location containing the actual address of the data.
- Example:
LDA (300)accesses data at the address stored in location 300.
- Example:
4. Indexed Addressing
- Combines a base address with an index register value.
- Example:
LDA 100(X)loads data from100 + X, whereXis the index register.
- Example:
5. Symbolic Addressing
- Labels replace explicit addresses, enhancing readability and maintainability.
- Example:
LDA TOTALloads data from the memory location labeledTOTAL.
- Example:
Using Labels for Clarity
- Purpose of Labels:
- Represent memory locations symbolically, improving program readability.
- Help manage jumps and loops without manually tracking memory addresses.
- Example Program with Labels:
START: LDA DATA ADD VALUE STA RESULT JMP START DATA: #10 VALUE: #5 RESULT: #0
Trace Tables
- A tool to simulate assembly language execution step-by-step.
- Tracks changes in the accumulator, memory, and registers at each instruction.
- Example:
Instruction ACC Memory[100] Comment LDA 10010 10 Load 10 into ACC ADD 20015 5 Add 5 to ACC
Example Assembly Language Programs
1. Arithmetic Program
- Adds two numbers stored in memory and stores the result:
START: LDA NUM1 ADD NUM2 STA RESULT HLT NUM1: #10 NUM2: #20 RESULT: #0
2. Array Summation
- Adds elements of an array using indexed addressing:
LDX #0 ; Initialize index register CLR ACC ; Clear accumulator LOOP: ADD ARR(X) INC X CMP X, #5 JNZ LOOP STA TOTAL HLT ARR: #1, #2, #3, #4, #5 TOTAL: #0
Applications of Assembly Language
- Efficiency and Low-Level Control:
- Used in performance-critical systems where direct hardware access is required.
- Embedded Systems:
- Central to programming microcontrollers in devices like washing machines, traffic lights, and robots.
- Operating Systems:
- Kernel functions often include assembly language for managing hardware resources.
Advantages and Disadvantages
Advantages
- Speed and Efficiency:
- Programs run faster due to their close alignment with machine code.
- Hardware Control:
- Provides direct interaction with CPU and memory.
- Compact Code:
- Efficient use of memory compared to high-level languages.
Disadvantages
- Complexity:
- Programs are harder to write and debug due to low-level syntax.
- Portability:
- Assembly language is processor-specific, limiting its use across different hardware.
Conclusion
Assembly language bridges the gap between machine code and high-level programming languages. While its low-level nature enables unparalleled control and efficiency, its complexity and lack of portability often restrict its use to specialized applications. Its relevance remains critical in embedded systems, device programming, and performance-optimized software.
