8086 Execution Unit

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

Relative Addressing Mode

relative addressing mode

Relative addressing mode is a method used in assembly language programming for specifying the
operand's address. It involves providing an offset value relative to a base address, which is typically
the address of the current instruction or a register. This addressing mode is particularly useful for
branch instructions and memory access patterns that depend on the current program counter (PC) or
another reference point.

Key Characteristics of Relative Addressing Mode:

1. Offset-Based: The operand's address is determined by adding a constant offset to a base


address (usually the program counter).
2. Program Counter (PC) Relative: For branch instructions, the offset is added to the current value
of the PC to determine the target address.
3. Compact Instruction Encoding: Since the offset is typically smaller than absolute addresses,
instructions can be more compact.
4. Flexibility: Enables easy implementation of control flow structures like loops, conditional
branches, and function calls.

Example of Relative Addressing in Branch Instructions:

Consider an instruction set where a branch instruction has the format:

sql

BR offset

If the current PC is at address `1000` and the instruction is `BR 4`, the processor will add `4` to
the current PC, resulting in the new PC value `1004`.

Example in Assembly Language:

Here’s a simple example in an assembly language syntax (like that used in ARM or MIPS processors):

assembly

LOOP: NOP ; No operation, placeholder instruction


ADD R1, R2, R3 ; Some arithmetic operation
BNE R1, R2, LOOP ; Branch to LOOP if R1 is not equal to R2

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/12
In this example:

`LOOP` is a label at the start of the loop.


`BNE R1, R2, LOOP` will cause the program to branch back to the label `LOOP` if the condition is
met. The actual address for `LOOP` is calculated as an offset relative to the PC.

Advantages:

Efficient Looping and Branching: Ideal for implementing loops and conditional jumps.
Code Portability: Makes the code more position-independent, as the actual addresses are not
hard-coded.
Memory Efficient: Saves memory as the offset is often smaller than absolute addresses.

Disadvantages:

Limited Range: The offset typically has a limited range, which may restrict the distance between
the instruction and the target address.
Complexity in Address Calculation: May add complexity to the address calculation logic within
the CPU.

Practical Application:

Relative addressing is commonly used in modern CPUs for implementing various control flow
instructions, such as:

Conditional and unconditional branch instructions.


Loop constructs.
Function calls and returns.

Understanding relative addressing mode is crucial for low-level programming and for developers
working on compiler design, embedded systems, and performance-critical applications.

give me simple defination

Relative addressing mode is a way of specifying the location of an operand in assembly language by
providing an offset relative to a base address, typically the current program counter (PC). This mode is
commonly used for branching instructions, where the offset is added to the PC to determine the
target address for the jump.

i can't understand

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/12
Relative addressing mode specifies the location of data by giving an offset (difference) from the
current instruction's address. Instead of using an absolute memory address, it tells the processor to
go a certain distance forward or backward from where it is now.

Simple Definition:

Relative addressing mode uses an offset to determine the address of the data relative to the current
position in the program. This is often used in branch instructions to jump to a new instruction a
certain number of steps away.

Example:

Imagine you are on step 5 of a list, and you have an instruction that says, "Go forward 3 steps." In
relative addressing mode, you would go from step 5 to step 8.
Similarly, if the current instruction is at memory address 100, and the offset is 4, the new address will
be 100 + 4 = 104.

Subroutine instructions in 8086

In the 8086 microprocessor, subroutines (also known as procedures or functions) are blocks of code
that perform a specific task and can be called from different places in the program. The main
instructions associated with subroutines in the 8086 microprocessor are `CALL` and `RET`.

CALL Instruction

The `CALL` instruction is used to call a subroutine. It does the following:

1. Saves the address of the next instruction (the return address) onto the stack.
2. Transfers control to the subroutine by jumping to its starting address.

The syntax for the `CALL` instruction is:

objectivec

CALL subroutine_address

or

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/12
objectivec

CALL subroutine_label

RET Instruction

The `RET` instruction is used to return from a subroutine. It does the following:

1. Pops the return address from the stack.


2. Transfers control back to the address popped from the stack, continuing execution from there.

The syntax for the `RET` instruction is simply:

RET

Example

Here is an example demonstrating how to use `CALL` and `RET` instructions in an assembly language
program for the 8086 microprocessor:

assembly

segment .data
message db 'Hello, World!', 0

segment .text
global _start

_start:
; Call the subroutine
CALL print_message

; Exit the program (using an interrupt for simplicity)


MOV AX, 4C00h
INT 21h

print_message:
; Display the message
MOV DX, offset message ; Load address of message into DX
MOV AH, 09h ; DOS print string function
INT 21h ; Call DOS interrupt to print string

; Return from the subroutine


RET

Explanation

1. Main Program:
The `CALL print_message` instruction calls the `print_message` subroutine.
The return address (next instruction after `CALL`) is pushed onto the stack.
The control is transferred to the `print_message` subroutine.
2. Subroutine (print_message):

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/12
The subroutine prints a message by using DOS interrupt 21h with function 09h.
After printing the message, the `RET` instruction is executed.
The return address is popped from the stack, and control is transferred back to the main
program, right after the `CALL` instruction.

Stack Operations

When `CALL` is executed:


The address of the instruction immediately following the `CALL` is pushed onto the stack.
When `RET` is executed:
The address is popped from the stack and execution continues from that address.

Nested Subroutines

Subroutines can call other subroutines. In such cases, each `CALL` pushes a return address onto the
stack, and each `RET` pops an address from the stack. The stack ensures that each subroutine returns
to the correct location.

Summary

`CALL`: Used to call a subroutine, pushing the return address onto the stack.
`RET`: Used to return from a subroutine, popping the return address from the stack.

Understanding how to use `CALL` and `RET` effectively allows for modular and reusable code in
assembly language programming.

give me the detaile and simple explaination of 8086 instruction set

The 8086 microprocessor instruction set includes a variety of instructions for performing different
operations. These instructions can be broadly classified into several categories, including data
transfer, arithmetic, logical, control transfer, string manipulation, and processor control instructions.
Below is a detailed and simple explanation of each category along with examples.

1. Data Transfer Instructions

These instructions move data between registers, between memory and registers, or between I/O
ports and registers.

MOV: Transfer data from source to destination.

css

MOV AX, BX ; AX = BX
MOV [SI], AX ; Memory at address SI = AX

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/12
PUSH: Push data onto the stack.

mathematica

PUSH AX ; Stack = AX

POP: Pop data from the stack.

mathematica

POP AX ; AX = Stack

IN: Input data from an I/O port to a register.

csharp

IN AL, DX ; AL = data from port DX

OUT: Output data from a register to an I/O port.

java

OUT DX, AL ; Port DX = AL

2. Arithmetic Instructions

These instructions perform arithmetic operations like addition, subtraction, multiplication, and
division.

ADD: Add source to destination.

sql

ADD AX, BX ; AX = AX + BX

SUB: Subtract source from destination.

vbnet

SUB AX, BX ; AX = AX - BX

MUL: Unsigned multiplication.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/12
arduino

MUL BX ; AX = AX * BX (result in AX for 8-bit, AX:DX for 16-bit)

DIV: Unsigned division.

css

DIV BX ; AX = AX / BX (quotient in AX, remainder in DX)

3. Logical Instructions

These instructions perform bitwise logical operations like AND, OR, XOR, and NOT.

AND: Logical AND operation.

AND AX, BX ; AX = AX AND BX

OR: Logical OR operation.

OR AX, BX ; AX = AX OR BX

XOR: Logical XOR operation.

vbnet

XOR AX, BX ; AX = AX XOR BX

NOT: Logical NOT operation (one's complement).

NOT AX ; AX = NOT AX

4. Control Transfer Instructions

These instructions change the sequence of execution of the program.

JMP: Unconditional jump.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/12
css

JMP LABEL ; Jump to LABEL

CALL: Call a subroutine.

sql

CALL SUBR ; Call subroutine at SUBR

RET: Return from a subroutine.

vbnet

RET ; Return to caller

JZ (or JE): Jump if zero (equal).

css

JZ LABEL ; Jump to LABEL if zero flag is set

JNZ (or JNE): Jump if not zero (not equal).

css

JNZ LABEL ; Jump to LABEL if zero flag is not set

5. String Manipulation Instructions

These instructions operate on strings of data in memory.

MOVS: Move string data.

css

MOVSB ; Move byte from [SI] to [DI]


MOVSW ; Move word from [SI] to [DI]

CMPS: Compare string data.

vbnet

CMPSB ; Compare byte at [SI] with byte at [DI]

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/12
CMPSW ; Compare word at [SI] with word at [DI]

SCAS: Scan string data.

vbnet

SCASB ; Compare byte at [DI] with AL


SCASW ; Compare word at [DI] with AX

LODS: Load string data.

csharp

LODSB ; Load byte at [SI] into AL


LODSW ; Load word at [SI] into AX

STOS: Store string data.

arduino

STOSB ; Store byte in AL at [DI]


STOSW ; Store word in AX at [DI]

6. Processor Control Instructions

These instructions control the operation of the processor.

HLT: Halt the processor.

HLT ; Halt execution

NOP: No operation (do nothing).

yaml

NOP ; No operation

STC: Set carry flag.

STC ; CF = 1

CLC: Clear carry flag.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/12
objectivec

CLC ; CF = 0

CMC: Complement carry flag.

objectivec

CMC ; CF = NOT CF

Summary

The 8086 instruction set includes a diverse range of instructions to perform various operations,
including data transfer, arithmetic, logical, control transfer, string manipulation, and processor
control. Understanding these instructions allows for efficient and effective programming of the 8086
microprocessor.

Execution cycle in 8086 in detaile

The execution cycle of the 8086 microprocessor involves several stages, during which the processor
fetches, decodes, and executes instructions. Understanding the execution cycle is crucial for
comprehending how the 8086 operates. Here’s a detailed explanation of each stage in the execution
cycle of the 8086 microprocessor:

1. Instruction Fetch

The instruction fetch stage involves retrieving the instruction from memory. The steps are as follows:

1. Program Counter (PC): The PC holds the address of the next instruction to be executed.
2. Address Calculation: The address from the PC is placed on the address bus.
3. Memory Read: The memory read operation is initiated to fetch the instruction.
4. Instruction Queue: The fetched instruction is placed into the instruction queue (a six-byte
prefetch queue to speed up execution).

2. Instruction Decode

The instruction decode stage involves interpreting the fetched instruction to determine what actions
are required. The steps are as follows:

1. Queue Extraction: The instruction is extracted from the queue.


2. Opcode Decode: The operation code (opcode) is decoded to determine the type of instruction.
3. Operand Fetch: The operands required for the instruction are identified and fetched if
necessary.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/12
4. Address Calculation: Effective addresses for memory operands are calculated using segment
and offset registers.

3. Execution

The execution stage involves carrying out the actions specified by the decoded instruction. The steps
are as follows:

1. ALU Operations: Arithmetic and logical unit (ALU) performs operations such as addition,
subtraction, AND, OR, etc.
2. Data Transfer: Data is transferred between registers, or between memory and registers.
3. Control Flow Changes: Control flow instructions (like jumps and calls) alter the sequence of
execution.
4. Status Flag Updates: Processor status flags (carry, zero, sign, etc.) are updated based on the
result of the operation.

4. Write Back

The write back stage involves writing the results of the execution back to the destination. The steps
are as follows:

1. Register Update: Results of operations are written back to the appropriate registers.
2. Memory Update: If the operation involves memory, the results are written back to the specified
memory location.

Detailed Breakdown of Each Stage

Fetch Stage

The 8086 uses a prefetch queue to fetch instructions ahead of time. This queue can hold up to
six bytes of instructions.
The Bus Interface Unit (BIU) handles fetching the next instruction from memory and placing it
into the queue.
Instruction Pointer (IP) register, combined with the Code Segment (CS) register, forms the
address of the next instruction.
Memory Read Cycle: The BIU sends out the address and reads the instruction byte(s) from
memory.

Decode Stage

The Execution Unit (EU) takes instructions from the prefetch queue.
The instruction is broken down into opcode and operand(s).
The opcode is used to identify the type of operation, while operand(s) provide data or addresses.
ModR/M Byte: For many instructions, an additional byte (ModR/M byte) specifies the addressing
mode and register operands.
If additional operands are required, the EU fetches them from the queue or memory.

Execute Stage

The EU performs the specified operation using the ALU.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 11/12
Depending on the instruction, the ALU may perform arithmetic operations (e.g., ADD, SUB),
logical operations (e.g., AND, OR), or data movement (e.g., MOV).
The ALU interacts with registers to perform these operations.
Flags Register: The results of operations may affect the status flags (e.g., zero flag, carry flag).

Write Back Stage

Results are written back to the destination operand, which could be a register or memory
location.
If the instruction modifies memory, the BIU handles writing data to the correct memory address.
The PC is updated to point to the next instruction, which may involve computing a new address
for jumps or calls.

Additional Concepts

Pipelining

The 8086 employs a simple form of pipelining where instruction fetching and execution overlap. While
one instruction is being executed, the next instruction is being fetched.

Segmentation

The 8086 uses a segmented memory model with four segment registers: CS (Code Segment), DS (Data
Segment), SS (Stack Segment), and ES (Extra Segment). Each segment register holds the starting
address of a segment, which is combined with an offset to form the full address.

Execution Cycle Example

Consider the following instruction:

sql

ADD AX, BX

1. Fetch: The instruction `ADD AX, BX` is fetched from memory into the prefetch queue.
2. Decode: The instruction is decoded; the opcode identifies it as an ADD operation, and the
operands are AX and BX.
3. Execute: The ALU adds the contents of BX to AX.
4. Write Back: The result is stored in the AX register, and the status flags are updated.

Summary

The 8086 execution cycle involves a series of stages where the processor fetches, decodes, executes,
and writes back instructions. By using a prefetch queue and simple pipelining, the 8086 can overlap
instruction fetch and execution to improve efficiency. Understanding these stages is essential for
grasping how the 8086 microprocessor processes instructions and manages program flow.

ChatGPT can make mistakes. Check important info.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 12/12

You might also like