Assembly Language Study Notes
4.2 Assembly Language
Syllabus Overview
- Candidates should be able to:
- Show understanding of the relationship between assembly language and machine code.
- Describe the different stages of the assembly process for a two-pass assembler.
- Trace a given simple assembly language program.
- Show understanding that a set of instructions are grouped.
- Apply the two-pass assembler process to a given simple assembly language program, including:
- Data movement
- Input and output of data
- Arithmetic operations
- Unconditional and conditional instructions
- Compare instructions
- Including Immediate, direct, indirect, indexed, relative addressing.
Key Concepts
Machine Code
- Definition: Simple instructions that are executed directly by the CPU.
- Characteristics:
- The programming language that the CPU uses.
- Machine code is in binary form.
Assembly Language
- Definition: A low-level chip/machine-specific programming language that uses mnemonics.
- Characteristics:
- Assembly code is an easy-to-read interpretation of machine code.
- One-to-one correspondence: One line of assembly equals one line of machine code.
- Example:
- Machine code:
000000110101 - Assembly code:
Store 53
Relationship Between Assembly Language and Low-Level Language
- Example:
- Assembly Code:
LOAD 253 - Machine Code:
0000 11111101
- Note: For every assembly language command, there is an equal machine language command (1:1 relationship).
Assembler
- Definition: An assembler converts an assembly language program into a machine language program.
Source Code
- Definition: The original computer program before translation into machine code.
Object Code
- Definition: The computer program after translation into machine code.
Instruction Set
- Definition: The complete set of machine code instructions used by a CPU.
Structure of Instructions
- Components:
- Op-code: Short for operation code; identifies the action the CPU will perform.
- Operand: Address or data on which the operation is to be performed.
Word Size Impact
- The word size determines the number of bits available for the opcode and the operand.
Accumulator
- Usage: Stores intermediate results of calculations.
- Important Instructions for all processors:
- LDD: Loads contents of the memory address or integer into the accumulator.
- ADD: Adds contents of the memory address or integer to the accumulator.
- STO: Stores contents of the accumulator into the addressed location.
Assembly Language Examples
- Example Assembly Code: Support Calculations
LDA #23 ; loads the number 23 into the accumulatorADD #42 ; adds the number 42 to the contents of the accumulator → Result: 65STO 34 ; saves the accumulator result to the memory address 34.
Modes of Addressing
- Definition: Methods of locating data and instructions in memory.
- Significance: These methods determine how data is accessed, controlling program flow.
- Types of Addressing Modes:
- Direct Addressing: Go to the specified address and load the data into the accumulator.
- Example:
- Instruction:
LDD 41 - Addressing Mapping:
- Address: 40, Data: 2
- Address: 41, Data: 3
- Address: 42, Data: 20
- Indirect Addressing: Access an address indirectly if it is out of range.
- Example:
LDI 65 - Immediate Addressing: Operate directly on a constant value specified in the instruction.
- Example:
LDM #500 → immediately loads 500 into the accumulator. - Indexed Addressing: Uses an index register to access memory addresses in loops or array processing.
- Example:
- If
IX = 3, then LDX 50 → Accesses the address 50 + 3 = 53.
- Relative Addressing: Computes the memory address as the current memory address plus the operand (useful for jumps).
- Example:
JMR #5 → jumps to the instruction 5 locations after the current one. - Symbolic Addressing: Utilizes labels that replace actual memory addresses to make programs more understandable.
Advantages of Symbolic Addressing
- Programs become relocatable; they can operate regardless of their absolute memory location.
- Increases code readability for human programmers by breaking code into sections using symbols.
Assembly Instructions Types
Data Movement Instructions
- Instructions that involve loading data into registers or storing them in memory.
- Examples include:
LDM, LDD, LDI, STO, MOV.
- Allow data reading from input devices or outputting data to display devices.
Arithmetic Operation Instructions
- Perform calculations using data stored in the accumulator and store results back into the accumulator.
- Examples include:
ADD, SUB, MUL, DIV.
Compare Instructions
- Used to compare the contents of the accumulator with addresses or constants.
- Examples include:
CMP, CMI.
Unconditional and Conditional Instructions
- Unconditional: e.g.,
JMP <address>. - Conditional: e.g.,
JPE <address>, JPN <address> depending on previous comparisons.
Assemblers
- Definition: Programs that translate assembly language code into machine code, categorized into:
- Single-Pass Assemblers: Execute in one pass, directly putting instructions into memory.
- Two-Pass Assemblers: Require two passes over the program code to resolve labels and generate object code that can be stored and executed later.
Two-Pass Assembler Process
- Pass 1: Analysis
- Reads the code line by line, ignoring irrelevant information (comments).
- Allocates memory addresses, checks opcodes against the instruction set, and builds a symbol table for labels.
- Keeps track of memory counters and processes any directives.
- Pass 2: Synthesis
- Revisits the assembly program using the symbol table to generate object code with complete addresses.
- Resolves any remaining forward references encountered in Pass 1.
Example of Using Two-Pass Assemblers
- Instruction sample with forward referencing:
- In pass one, it sees
JMP x, but does not know x’s address yet. - It registers the label
x without a value and resolves it in the second pass when x is defined.
Stages of Assembly
- Translation Process: An assembler translates assembly language (source code) to machine code, checking for syntactical correctness and associating opcodes with the correct instructions and addresses.
- Pass 1: Analyzes the program, ignoring unnecessary comments and building a symbol table.
- Pass 2: Generates object code, associates symbolic addresses with actual memory locations.
Error Checking during Assembly
- The assembler performs error checking, which helps in identifying issues before code execution—speeding up development.
Use of Directives and Macros in Assembly Programming
- Directives: Instructions that guide the assembler on how to process its code (e.g., setting up memory space).
- Macros: Subroutines that can be defined once and called multiple times to simplify code development and enhance modularity.
Conclusion
- Understanding assembly language, its relationship with machine code, and the assembly process itself is crucial for interpreting and writing low-level programs. This includes clearly delineating and applying various addressing modes and instruction types as a foundation for building efficient software.
Example Problems and Tracing
- Tracing provides an opportunity to visualize how each instruction processes data in memory, ultimately showing the contents of registers and memory addresses at various stages in processing a simple assembly language program, reinforcing the learning of these concepts effectively.