0% found this document useful (0 votes)
10 views

Assembly Language and Machine Code

Machine code is the only language understood by CPUs, consisting of binary instructions specific to each computer type. Assembly language simplifies programming by using mnemonics and requires translation into machine code via an assembler, which can be single-pass or two-pass. The two-pass assembler processes the code in two stages to handle labels and generate the final machine code efficiently.

Uploaded by

levi makokha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Assembly Language and Machine Code

Machine code is the only language understood by CPUs, consisting of binary instructions specific to each computer type. Assembly language simplifies programming by using mnemonics and requires translation into machine code via an assembler, which can be single-pass or two-pass. The two-pass assembler processes the code in two stages to handle labels and generate the final machine code efficiently.

Uploaded by

levi makokha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assembly Language and Machine Code

• Machine code is the only programming language a CPU can


understand.
• Every computer/chip type has its own set of machine code instructions.
• A program in machine code consists of binary instructions, which the CPU
can carry out automatically during the fetch-execute cycle.
• Each instruction performs a simple task, such as storing a value in
memory.
• Machine code can be displayed in hexadecimal format to make it easier
for human programmers to understand.

Challenges with Machine Code:


• Writing in machine code is time-consuming and error-prone.
• It's difficult to test or modify machine code programs, leading to a need
for easier languages.

Assembly Language:
• Assembly language was developed to simplify the process.
• It uses mnemonics (e.g., `LDD`, `ADD`, `STO`) instead of binary, but it
closely mirrors machine code.
• Each assembly language instruction consists of:
• Opcode: Identifies the operation to be carried out by the CPU.
• Operand: Contains the data or address for the operation.
Stages of Assembly

Programs written in assembly language need to be translated into machine


code by an assembler before the CPU can execute them.

Assembler Functionality
• The assembler translates each mnemonic into its corresponding machine
code instruction.
• It also checks the syntax and ensures only valid instructions are used,
speeding up development by catching errors early.

Types of Assemblers
1. Single-pass assembler:
• Translates code in a single pass.
• Produces a machine code program ready to be executed, but doesn't
handle labels used before their memory addresses are known.

2. Two-pass assembler
- Passes through the code twice.
- In the first pass, it stores labels and memory addresses.
- In the second pass, it generates the final machine code, including handling
labels that refer to forward instructions.

Two-Pass Assembly Process

Pass 1:
- Reads the assembly program line by line.
- Ignores unneeded elements like comments.
- Allocates memory addresses to each line.
- Verifies the opcode.
- Adds labels to a symbol table with memory addresses.

Pass 2:
- Re-reads the program.
- Generates the object code with opcodes and operands based on the symbol
table.
- Saves and executes the program.
Example:
If a label is used before it’s defined (a forward reference), it is handled in the
second pass. For example, `Found` might refer to an instruction yet to be
defined.

A symbol table stores labels and their memory addresses. In the example
provided:
- `Notfound` is at memory address 100.
- `Found` is at memory address 104.

You might also like