Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

UNIT II

Introduction to Assembly Language Programming

Introduction to Assembly Language

• Assembly Language is a low-level programming language that is specific to a computer's


architecture.

• It is a human-readable representation of machine code instructions.

• Assembly language allows programmers to have precise control over the computer's
hardware.

Why Assembly Language is Important

1. Efficiency: Assembly language programs can be highly optimized for speed and memory
usage since they operate directly on the hardware.

2. Embedded Systems: Many embedded systems and device drivers are written in assembly
language to maximize performance and minimize resource usage.

3. Operating System Development: Kernel components of operating systems are often written
in assembly language for control and performance reasons.

4. Reverse Engineering: Understanding assembly language is crucial for reverse engineering


software or analyzing malware.

5. Debugging: Assembly language is sometimes used in debugging and low-level system


programming to diagnose issues.

Basic Structure of Assembly Language Programs

• Assembly programs typically consist of sections like data, text, and sometimes a stack.

• Instructions are written using mnemonics (e.g., MOV for move) and operands (e.g., registers,
memory addresses).

• Programs start at a specific address (entry point) and execute sequentially unless directed
otherwise.

Difference between Assembly Language vs. High-Level Language

• Abstraction Level: Assembly language is closer to the hardware, while high-level languages
are more abstract and easier to read/write.

• Portability: High-level languages are more portable across different platforms, while
assembly is specific to a particular architecture.

• Development Speed: High-level languages are faster for application development, while
assembly requires more effort and is error-prone.

• Control: Assembly language offers fine-grained control over hardware, whereas high-level
languages abstract many hardware details.

Examples of Assembly Language Instructions

1. MOV (Move): Copies data from one location to another.


MOV AX, BX ; Copies the value of BX into AX

2. ADD (Addition): Adds two operands and stores the result.

ADD AX, BX ; Adds the values of AX and BX, storing the result in AX

3. SUB (Subtraction): Subtracts one operand from another and stores the result.

SUB AX, BX ; Subtracts the value of BX from AX, storing the result in AX

4. JMP (Jump): Unconditional branch to a specified location.

JMP Label ; Jumps to the instruction labeled "Label"

5. CMP (Compare): Compares two operands and sets flags based on the result.

CMP AX, BX ; Compares AX and BX and sets flags

6. JZ (Jump if Zero): Conditional jump if the Zero Flag is set.

JZ Label ; Jumps to "Label" if the Zero Flag is set.

MACROS, LABELS and DIRECTIVES

Understanding Macros and their Usage:

• Macros are sequences of instructions that are defined and named to perform specific tasks.

• They allow for code reuse and simplification by encapsulating a series of instructions into a
single, named entity.

• Macros are typically defined using macro assemblers and are invoked using their names.

• Common usage scenarios include repetitive code, complex calculations, or custom


instructions.

Creating and Using Macros in Assembly Programs:

• To create a macro, you define it using a macro assembler directive, typically starting with
something like MACRO and ending with MEND.

• Parameters can be passed to macros, enabling customization for different situations.

• Macros can be invoked using their names followed by parameter values, if any.

• Macros can greatly improve code readability, maintainability, and reduce redundancy.

Labels in Assembly Language and Their Significance:

• Labels are identifiers used to mark locations or points in your assembly code.

• They are crucial for control flow instructions (e.g., jumps and branches) to reference specific
memory addresses.

• Labels can also be used for referencing data or variables.

• Labels are typically followed by a colon, like loop:.


Directives in Assembly Language (e.g., ORG, DB, DW) and How to Use Them:

• Directives are instructions to the assembler, providing information about the program
layout, data allocation, and other assembly-specific details.

• Common directives include:

• ORG (Origin): Specifies the memory location where the program should be loaded.

• DB (Define Byte): Reserves memory for one or more bytes and initializes them.

• DW (Define Word): Reserves memory for one or more words (usually 16 bits) and
initializes them.

• There are many other directives like DS for reserving space, EQU for equating values,
and more.

Practical Exercises and Examples of Macros, Labels, and Directives:

1. Macro Example:

SQUARE MACRO X MOV AX, X MUL AX, X ENDM

Usage: SQUARE 5

2. Label Example:

START: MOV AX, 1 ADD AX, 2 JMP END LOOP: ; Loop instructions here END: ; End of program

3. Directives Example:

ORG 1000H ; Set program origin to 1000H MOV AX, 5 ; Load 5 into AX DB 10, 20, 30 ; Define three
bytes with values DW 1001H, 2002H ; Define two words with values

• Practical exercises can include creating macros for common arithmetic operations, labeling
code for branching, and using directives to allocate memory and initialize data.

Arithmetic Operations (Addition, Subtraction):

• Arithmetic instructions like ADD and SUB are used for basic calculations.

• Example:

MOV EAX, 5 ; Load 5 into EAX ADD EAX, 3 ; Add 3 to EAX

Input and Output Operations:

• Input and output operations are crucial for communication with the user.

• Use INT 21h (DOS interrupt) for simple console input and output.

• Example (for DOS):

MOV AH, 1 ; Function code for input INT 21h ; DOS interrupt

Conditional Jumps and Loops:


• Conditional jumps are used for decision-making.

• Conditional instructions like JZ (jump if zero) and JNZ (jump if not zero) are used.

• Loops can be created using instructions like LOOP and JCXZ.

• Example:

CMP EAX, 0 ; Compare EAX to 0 JZ label ; Jump to 'label' if EAX is zero

MICROPROCESSOR TIMINGS OVERVIEW

Introduction to Microprocessor Timings:

• Microprocessor timings refer to the precise timing and synchronization of various operations
performed by a microprocessor during the execution of instructions.

• Timings are crucial to ensure that instructions are executed accurately and efficiently.

Why Understanding Timings is Crucial:

• Understanding timings is essential for optimizing the performance of microprocessor-based


systems.

• It helps in minimizing execution time, improving efficiency, and ensuring correct


functionality.

• Timings are critical for designing real-time systems, where precise timing is crucial.

Overview of the Instruction Cycle:

• The instruction cycle is the fundamental process through which a microprocessor executes
instructions.

• It consists of fetching, decoding, executing, and storing results.

• Understanding the timing of each stage is necessary for proper execution.

Different Machine Cycles:

1. Fetch Cycle:
• Involves retrieving the next instruction from memory.

• Timing accuracy is crucial to fetch the correct instruction.

2. Decode Cycle:

• Involves decoding the fetched instruction to determine the operation to be


performed.

• Timings affect how quickly the instruction can be decoded.

3. Execute Cycle:

• The actual execution of the instruction.

• Timing impacts the speed and accuracy of execution.

4. Memory Cycle:

• Relates to read/write operations in memory.

• Timings are essential for data transfer between the CPU and memory.

T States (Time States) and Their Significance:


• T states represent the smallest unit of time in a microprocessor's operation.

• They are used to measure and control the timing of each operation.

• Significance of T states:

1. Synchronization: Ensure that all components of the microprocessor work in


harmony.

2. Control: Allow precise control over the execution of instructions.

3. Performance: Optimization of execution time for better overall performance.

4. Error Avoidance: Help in preventing timing-related errors.

Key Takeaways:

• Microprocessor timings are essential for precise and efficient execution of instructions.

• The instruction cycle consists of fetch, decode, execute, and memory stages.

• Different machine cycles require accurate timing.

• T states provide a granular measurement of time for synchronization and control.

INSTRUCTION CYCLE AND MACHINE CYCLES

Detailed Explanation of the Instruction Cycle:

The instruction cycle, also known as the fetch-decode-execute cycle, is the fundamental process that
a computer's central processing unit (CPU) follows to execute instructions from a program. It
consists of several machine cycles, each of which plays a crucial role in the execution of instructions.
The instruction cycle can be summarized in the following steps:

1. Fetch: In this phase, the CPU fetches the next instruction from memory. The program
counter (PC) is used to keep track of the address of the next instruction. The CPU reads the
instruction from memory and loads it into the instruction register (IR).

2. Decode: Once the instruction is in the IR, the CPU decodes it to determine what operation
needs to be performed and which operands are involved. This phase typically involves
breaking down the instruction into its constituent parts, such as the opcode (operation
code) and operand(s).

3. Execute: In this phase, the CPU performs the actual operation specified by the instruction.
This could involve arithmetic calculations, data transfers, logical operations, and more,
depending on the instruction's type.

4. Memory Access: Some instructions may require accessing memory to read or write data.
This phase, known as the memory cycle, is responsible for interacting with the computer's
memory subsystem. It includes memory read and write operations as necessary.

5. Write Back: The final phase of the instruction cycle involves storing the result of the
execution back into registers or memory locations as needed. This phase ensures that any
changes made by the instruction are saved for future use.
In-Depth Discussion of Each Machine Cycle:

Now, let's delve deeper into each machine cycle:

1. Fetch Cycle:

• Purpose: To fetch the next instruction from memory.

• Steps:

• The program counter (PC) is incremented to point to the address of the next
instruction.

• The CPU reads the instruction from the memory location pointed to by the
PC.

• The instruction is loaded into the instruction register (IR).

2. Decode Cycle:

• Purpose: To decode the fetched instruction.

• Steps:

• The CPU decodes the opcode to determine the operation to be performed.

• Operand(s) are identified and decoded to determine their location and


values.

3. Execute Cycle:

• Purpose: To execute the operation specified by the instruction.

• Steps:

• The CPU performs the actual computation or operation indicated by the


instruction.

• Results are generated and prepared for further processing.

4. Memory Cycle:

• Purpose: To access memory when needed for data operations.

• Steps:

• If the instruction requires accessing memory (e.g., load/store operations),


memory addresses are calculated.

• Data is read from or written to memory as necessary.

5. Write Back Cycle:

• Purpose: To store the results of the instruction.

• Steps:

• The final results of the operation are written back to registers or memory
locations, ensuring that any changes are saved.
Practical Examples Illustrating Machine Cycles:

1. Example: Addition Operation

• Fetch Cycle: Fetch the "add" instruction.

• Decode Cycle: Decode the instruction, identifying the operands.

• Execute Cycle: Perform the addition operation.

• Memory Cycle: Not required for simple addition.

• Write Back Cycle: Store the result in the destination register.

2. Example: Load from Memory

• Fetch Cycle: Fetch the "load" instruction.

• Decode Cycle: Decode the instruction, determining the memory address.

• Execute Cycle: Calculate the memory address.

• Memory Cycle: Read data from the specified memory location.

• Write Back Cycle: Store the loaded data in a register.

You might also like