Unit-3 DLD&CO
Unit-3 DLD&CO
The input data travels from input unit to ALU. Similarly, the computed data travels
from ALU to output unit. The data constantly moves from storage unit to ALU and
back again. This is because stored data is computed on before being stored again. The
control unit controls all the other units as well as their data.
Details about all the computer units are −
Input Unit
The input unit provides data to the computer system from the outside. So,
basically it links the external environment with the computer. It takes data
from the input devices, converts it into machine language and then loads it into
the computer system. Keyboard, mouse etc. are the most commonly used input
devices.
Output Unit
The output unit provides the results of computer process to the users i.e it links
the computer with the external environment. Most of the output data is the
form of audio or video. The different output devices are monitors, printers,
speakers, headphones etc.
Storage Unit
Storage unit contains many computer components that are used to store data. It
is traditionally divided into primary storage and secondary storage.Primary
storage is also known as the main memory and is the memory directly
accessible by the CPU. Secondary or external storage is not directly accessible
by the CPU. The data from secondary storage needs to be brought into the
primary storage before the CPU can use it. Secondary storage contains a large
amount of data permanently.
Control Unit
This unit controls all the other units of the computer system and so is known
as its central nervous system. It transfers data throughout the computer as
required including from storage unit to central processing unit and vice versa.
The control unit also dictates how the memory, input output devices,
arithmetic logic unit etc. should behave.
Instruction codes
A computer instruction is a binary code that determines the micro-operations in a
sequence for a computer. They are saved in the memory along with the information.
Each computer has its specific group of instructions.
They can be categorized into two elements as Operation codes (Opcodes) and
Address. Opcodes specify the operation for specific instructions. An address
determines the registers or the areas that can be used for that operation. Operands are
definite elements of computer instruction that show what information is to be operated
on.
It consists of 12 bits of memory that are required to define the address as the memory
includes 4096 words. The 15th bit of the instruction determines the addressing mode
(where direct addressing corresponds to 0, indirect addressing corresponds to 1).
Therefore, the instruction format includes 12 bits of address and 1 bit for the
addressing mode, 3 bits are left for Opcodes.
The following block diagram shows the instruction format for a basic computer.
There are three parts of the Instruction Format which are as follows −
Addressing Modes
Instructions that define the address of a definite memory location are known as
memory reference instructions. The method in which a target address or effective
address is recognized within the instruction is known as addressing mode.
The address field for instruction can be represented in two different ways are as
follows −
A processor register may hold an instruction, a storage address, or any data (such as
bit sequence or individual characters).
The computer needs processor registers for manipulating data and a register for
holding a memory address. The register holding the memory location is used to
calculate the address of the next instruction after the execution of the current
instruction is completed.
Following is the list of some of the most common registers used in a basic computer:
Skip Ad
o The Memory unit has a capacity of 4096 words, and each word contains 16
bits.
o The Data Register (DR) contains 16 bits which hold the operand read from the
memory location.
o The Memory Address Register (MAR) contains 12 bits which hold the address
for the memory location.
o The Program Counter (PC) also contains 12 bits which hold the address of the
next instruction to be read from memory after the current instruction is
executed.
o The Accumulator (AC) register is a general purpose processing register.
o The instruction read from memory is placed in the Instruction register (IR).
o The Temporary Register (TR) is used for holding the temporary data during
the processing.
o The Input Registers (IR) holds the input characters given by the user.
o The Output Registers (OR) holds the output after processing the input data.
Computer Instructions
Computer instructions are a set of machine language instructions that a particular
processor understands and executes. A computer performs tasks on the basis of the
instruction provided.
The Register-reference instructions are represented by the Opcode 111 with a 0 in the
leftmost bit (bit 15) of the instruction.
A Register-reference instruction specifies an operation on or a test of the AC
(Accumulator) register.
Input-Output instruction
Just like the Register-reference instruction, an Input-Output instruction does not need a reference to
memory and is recognized by the operation code 111 with a 1 in the leftmost bit of the instruction. The
remaining 12 bits are used to specify the type of the input-output operation or test performed.
A control unit whose binary control values are saved as words in memory is called a microprogrammed
control unit.
A controller results in the instructions to be implemented by constructing a definite collection of
signals at each system clock beat. Each of these output signals generates one micro-operation including
register transfer. Thus, the sets of control signals are generated definite micro-operations that can be
saved in the memory.
Each bit that forms the microinstruction is linked to one control signal. When the bit is set, the control
signal is active. When it is cleared the control signal turns inactive. These microinstructions in a
sequence can be saved in the internal ’control’ memory. The control unit of a microprogram-controlled
computer is a computer inside a computer.
The following image shows the block diagram of a Microprogrammed Control organization.
Instruction cycle
A program residing in the memory unit of a computer consists of a sequence of
instructions. These instructions are executed by the processor by going through a
cycle for each instruction.
Fetch Cycle
The address instruction to be implemented is held at the program counter. The
processor fetches the instruction from the memory that is pointed by the PC.
Next, the PC is incremented to display the address of the next instruction. This
instruction is loaded onto the instruction register. The processor reads the instruction
and executes the important procedures.
Execute Cycle
The data transfers for implementation takes place in two methods are as follows −
Processor-memory − The data sent from the processor to memory or from
memory to processor.
Processor-Input/Output − The data can be transferred to or from a peripheral
device by the transfer between a processor and an I/O device.
In the execute cycle, the processor implements the important operations on the
information, and consistently the control calls for the modification in the sequence of
data implementation. These two methods associate and complete the execute cycle.
8086 architecture
The BIU:
o Data Segment Register (DS): The data, variables and constants given
in the program are held in the data segment of the memory.
o Stack Segment Register (SS): Stack segment holds addresses and data
of subroutines. It also holds the contents of registers and memory
locations given in PUSH instruction.
i. AX = [AH:AL]
ii. BX = [BH:BL]
iii. CX = [CH:CL]
iv. DX = [DH:DL]
AX AH AL
BX BH BL
CX CH CL
DX DH DL
o Index Register: The following four registers are in the group of pointer
and index registers:
The pointers will always store some address or memory location. In 8086
Microprocessor, they usually store the offset through which the actual address is
calculated.
o Carry Flag
o Auxiliary Flag
o Parity Flag
o Zero Flag
o Sign Flag
o Overflow Flag
o Trap Flag
o Interrupt Flag
o Direction Flag
Register organization
Registers are the smaller and the fastest accessible memory units in the central processing unit (CPU).
According to memory hierarchy, the registers in the processor, function a level above the main
memory and cache memory. The registers used by the central unit are also called as processor
registers.
A register can hold the instruction, address location, or operands. Sometimes, the instruction has
register as a part of itself.
Types of Registers
As we have discussed above, registers can be organized into two main categories i.e. the User-Visible
Registers and the Control and Status Registers. Although we can’t separate the registers in the
processors clearly among these two categories.
This is because in some processors, a register may be user-visible and in some, the same may not be
user-visible. But for our rest of discussion regarding register organization, we will consider these two
categories of register.
Program Counter
Instruction Register
Memory Address Register
Memory Buffer Register
User-Visible Registers
These registers are visible to the assembly or machine language programmers and they use them
effectively to minimize the memory references in the instructions. Well, these registers can only
be referenced using the machine or assembly language.
Well, the programmers can restrict some of the general-purpose registers to specific functions. Like,
some registers are specifically used for stack operations or for floating-point operations. The general-
purpose register can also be employed for the addressing functions.
2. Data Register
The term itself describes that these registers are employed to hold the data. But the
programmers can’t use these registers for calculating operand address.
3. Address Register
Now, the address registers contain the address of an operand or it can also act as a general-purpose
register. An address register may be dedicated to a certain addressing mode. Let us understand this
with the examples.
4. Condition Code
Condition codes are the flag bits which are the part of the control register. The condition codes are set
by the processor as a result of an operation and they are implicitly read through the machine
instruction.
The programmers are not allowed to alter the conditional codes. Generally, the condition codes are
tested during conditional branch operation.
The control and status register holds the address or data that is important to control the processor’s
operation. The most important thing is that these registers are not visible to the users. Below we will
discuss all the control and status registers are essential for the execution of an instruction.
1. Program Counter
The program counter is a processor register that holds the address of the instruction that has to be
executed next. It is a processor which updates the program counter with the address of the next
instruction to be fetched for execution.
2. Instruction Register
Instruction register has the instruction that is currently fetched. It helps in analysing the opcode and
operand present in the instruction.
The memory buffer register holds the data that has to be written to a memory location or it holds the
data that is recently been read.
The memory address registers (MAR) and memory buffer registers (MBR) are used to move the
data between processor and memory.
Apart from the above registers, several processors have a register termed as Program Status Word
(PSW). As the word suggests it contains the status information.
Sign: This field has the resultant sign bit of the last arithmetic operation performed.
Zero: This field is set when the result of the operation is zero.
Carry: This field is set when an arithmetic operation results in a carry into or borrow out.
Equal: If a logical operation results in, equality the Equal bit is set.
Supervisor: This bit indicates whether the processor is executing in the supervisor mode or the user mode.
So, these are the types of registers a processor has. The processor designer organizes the registers according
to the requirement of the processor.
Register organization
Registers are the smaller and the fastest accessible memory units in the central
processing unit (CPU). According to memory hierarchy, the registers in the processor,
function a level above the main memory and cache memory. The registers used by
the central unit are also called as processor registers.
A register can hold the instruction, address location, or operands. Sometimes, the
instruction has register as a part of itself.
Types of Registers
As we have discussed above, registers can be organized into two main categories i.e.
the User-Visible Registers and the Control and Status Registers. Although we
can’t separate the registers in the processors clearly among these two categories.
This is because in some processors, a register may be user-visible and in some, the
same may not be user-visible. But for our rest of discussion regarding register
organization, we will consider these two categories of register.
Program Counter
Instruction Register
Memory Address Register
Memory Buffer Register
User-Visible Registers
These registers are visible to the assembly or machine language programmers and
they use them effectively to minimize the memory references in the instructions.
Well, these registers can only be referenced using the machine or assembly
language.
The general-purpose registers detain both the addresses or the data. Although we
have separate data registers and address registers. The general purpose register also
accepts the intermediate results in the course of program execution.
Well, the programmers can restrict some of the general-purpose registers to specific
functions. Like, some registers are specifically used for stack operations or for
floating-point operations. The general-purpose register can also be employed for
the addressing functions.
2. Data Register
The term itself describes that these registers are employed to hold the data. But the
programmers can’t use these registers for calculating operand address.
3. Address Register
Now, the address registers contain the address of an operand or it can also act as
a general-purpose register. An address register may be dedicated to a
certain addressing mode. Let us understand this with the examples.
(a) Segment Pointer Register
A memory divided in segments, requires a segment register to hold the base address
of the segment. There can be multiple segment registers. As one segment register can
be employed to hold the base address of the segment occupied by the operating
system. The other segment register can hold the base address of the segment allotted
to the processor.
4. Condition Code
Condition codes are the flag bits which are the part of the control register. The
condition codes are set by the processor as a result of an operation and they are
implicitly read through the machine instruction.
The programmers are not allowed to alter the conditional codes. Generally, the
condition codes are tested during conditional branch operation.
1. Program Counter
The program counter is a processor register that holds the address of the instruction
that has to be executed next. It is a processor which updates the program counter
with the address of the next instruction to be fetched for execution.
2. Instruction Register
The memory buffer register holds the data that has to be written to a memory
location or it holds the data that is recently been read.
The memory address registers (MAR) and memory buffer registers (MBR) are used
to move the data between processor and memory.
Apart from the above registers, several processors have a register termed as Program
Status Word (PSW). As the word suggests it contains the status information.
Sign: This field has the resultant sign bit of the last arithmetic operation performed.
Zero: This field is set when the result of the operation is zero.
Carry: This field is set when an arithmetic operation results in a carry into or borrow out.
Equal: If a logical operation results in, equality the Equal bit is set.
Supervisor: This bit indicates whether the processor is executing in the supervisor
mode or the user mode.
So, these are the types of registers a processor has. The processor designer organizes the
registers according to the requirement of the processor.
The least Significant bit of address (A0 is not used for byte selection)it is reserved for bank
selection. Therefore A0=0 will select Even bank. The BHE signal is used for selection of odd
bank.the processor will used combination of this two signals decide type of data transfer.
1 1 None(Idle)
Maximum mode
In the maximum mode, the 8086 is operated by strapping the MN/MX pin
to ground.
In this mode, the processor derives the status signal S2, S1, S0. Another
chip called bus controller derives the control signal using this status
information .
In the maximum mode, there may be more than one microprocessor in the
system configuration.
Minimum mode
In a minimum mode 8086 system, the microprocessor 8086 is operated in
minimum mode by strapping its MN/MX pin to logic 1.
In this mode, all the control signals are given out by the microprocessor
chip itself.
There is a single microprocessor in the minimum mode system.
For every instruction that is executed in the 8086 microprocessor, an instruction format is
available that is the binary representation of that instruction.
This instruction format can be coded from 1 to 6 bytes depending upon the addressing modes used for
instructions.
The general Instruction format that most of the instructions of the 8086 microprocessor follow is:
The Opcode stands for Operation Code. Every Instruction has a unique 6-bit opcode. For
example, the opcode for MOV is 100010.
D stands for direction
If D=0, then the direction is from the register
If D=1, then the direction is to the register
W stands for word
If W=0, then only a byte is being transferred, i.e. 8 bits
If W=1, them a whole word is being transferred, i.e. 16 bits
The MOD and R/M together is calculated based upon the addressing mode and register being
used in it. This is calculated as follows:
R/M 00 01 10 11
(Memory Mode with no (Memory mode with 8 bit (Memory Mode with 16 bit (Register
displacement) displacement) displacement) Mode)
REG stands for register selected. It is a 3-bit code which is calculated as follows:
000 AL AX
001 CL CX
010 DL DX
011 BL BX
100 AH SP
101 CH BP
110 DH SI
111 BH DI
The low order displacement and high order displacement are optional and the instruction
format contains them only if there exists any displacement in the instruction. If the
displacement is of 8 bits, then only the cell of low order displacement infilled and if the
displacement is of 16 bits, then both the cells od low order and high order are filled, with the
exact bits that the displacement number represents.
The 8086 microprocessors have 8 addressing modes. Two addressing modes have been provided for
instructions which operate on register or immediate data.
Register Addressing: In register addressing, the operand is placed in one of the 16-bit or 8-bit general
purpose registers.
Example
o MOV AX, CX
o ADD AL, BL
o ADD CX, DX
Immediate Addressing: In immediate addressing, the operand is specified in the instruction itself.
Example
The remaining 6 addressing modes specify the location of an operand which is placed in a memory.
Direct Addressing: In direct addressing mode, the operand?s offset is given in the instruction as an 8-
bit or 16-bit displacement element.
Example
The instruction adds the content of the offset address 0301 to AL. the operand is placed at the given
offset (0301) within the data segment DS.
Register Indirect Addressing: The operand's offset is placed in any one of the registers BX, BP, SI or
DI as specified in the instruction.
Example
It moves the contents of memory locations addressed by the register BX to the register AX.
Based Addressing: The operand's offset is the sum of an 8-bit or 16-bit displacement and the contents
of the base register BX or BP. BX is used as base register for data segment, and the BP is used as a
base register for stack segment.
Example
Indexed Addressing: The offset of an operand is the sum of the content of an index register SI or DI
and an 8-bit or 16-bit displacement.
Example
Based Indexed Addressing: The offset of operand is the sum of the content of a base register BX or
BP and an index register SI or DI.
Here, BX is used for a base register for data segment, and BP is used as a base register for stack
segment.
Example
Effective Address (Offset) = [BX or BP] + [SI or DI] + 8-bit or 16-bit displacement
Example
o Instructions are classified on the basis of functions they perform. They are categorized into the
following main types:
Instruction set
Data Transfer instruction
o All the instructions which perform data movement come under this category. The source data
may be a register, memory location, port etc. the destination may be a register, memory
location or port. The following instructions come under this category:
Instruction Description
MOV Moves data from register to register, register to memory, memory to register, memory to accumulator, acc
to memory, etc.
LDS Loads a word from the specified memory locations into specified register. It also loads a word from the
memory locations into DS register.
LES Loads a word from the specified memory locations into the specified register. It also loads a word from
memory locations into ES register.
LAHF Loads low order 8-bits of the flag register into AH register.
SAHF Stores the content of AH register into low order bits of the flags register.
XCHG Exchanges the contents of the 16-bit or 8-bit specified register with the contents of AX register, specified r
memory locations.
PUSH Pushes (sends, writes or moves) the content of a specified register or memory location(s) onto the top of th
POP Pops (reads) two bytes from the top of the stack and keeps them in a specified register, or memory location
POPF Pops (reads) two bytes from the top of the stack and keeps them in the flag register.
OUT Transfers data from accumulator or AL or AX register to an I/O port identified by the second by
instruction.
Arithmetic Instructions
instructio Description
n
ADC Adds specified operands and the carry status (i.e. carry of the previous stage).
SBB Subtract immediate data with borrow from accumulator, memory or register.
DAA Decimal Adjust after BCD Addition: When two BCD numbers are added, the DAA is used after ADD
instruction to get correct answer in BCD.
DAS Decimal Adjust after BCD Subtraction: When two BCD numbers are added, the DAS is used after SUB
instruction to get correct answer in BCD.
AAA ASCII Adjust for Addition: When ASCII codes of two decimal digits are added, the AAA is used after a
get correct answer in unpacked BCD.
AAD Adjust AX Register for Division: It converts two unpacked BCD digits in AX to the equivalent binary num
adjustment is done before dividing two unpacked BCD digits in AX by an unpacked BCD byte.
AAM Adjust result of BCD Multiplication: This instruction is used after the multiplication of two unpacked BCD
AAS ASCII Adjust for Subtraction: This instruction is used to get the correct result in unpacked BCD
subtraction of the ASCII code of a number from ASCII code another number.
NEG Obtains 2's complement (i.e. negative) of the content of an 8-bit or 16-bit specified register or memory locati
CMP Compare Immediate data, register or memory with accumulator, register or memory location(s).
Logical Instructions
Instruction of this group perform logical AND, OR, XOR, NOT and TEST operations. The
following instructions come under this category:
Instruction Description
AND Performs bit by bit logical AND operation of two operands and places the result in the specified destination.
OR Performs bit by bit logical OR operation of two operands and places the result in the specified destination.
XOR Performs bit by bit logical XOR operation of two operands and places the result in the specified destination.
NOT Takes one's complement of the content of a specified register or memory location(s).
TEST Perform logical AND operation of a specified operand with another specified operand.
Rotate Instructions
Instruction Description
RCL Rotate all bits of the operand left by specified number of bits through carry flag.
RCR Rotate all bits of the operand right by specified number of bits through carry flag.
ROL Rotate all bits of the operand left by specified number of bits.
ROR Rotate all bits of the operand right by specified number of bits.
Shift Instructions
Instruction Description
SAL or SHL Shifts each bit of operand left by specified number of bits and put zero in LSB position.
SAR Shift each bit of any operand right by specified number of bits. Copy old MSB into new MSB.
SHR Shift each bit of operand right by specified number of bits and put zero in MSB position.
Branch Instructions
It is also called program execution transfer instruction. Instructions of this group transfer program
execution from the normal sequence of instructions to the specified destination or target. The following
instructions come under this category:
Instruction Description
JMP Causes the program execution to jump unconditionally to the memory address or label given in the instruction.
CALL Calls a procedure whose address is given in the instruction and saves their return address to the stack.
RET Returns program execution from a procedure (subroutine) to the next instruction or main program.
IRET Returns program execution from an interrupt service procedure (subroutine) to the main program.
ZF = Zero Flag
OF = Overflow Flag SF = Sign Flag
CX = Register
CLC Clear Carry Flag: This instruction resets the carry flag CF to 0.
CLD Clear Direction Flag: This instruction resets the direction flag DF to 0.
CLI Clear Interrupt Flag: This instruction resets the interrupt flag IF to 0.
ESC Escape: makes bus free for external master like a coprocessor or peripheral device.
WAIT When WAIT instruction is executed, the processor enters an idle state in which the processor does no proces
LOCK It is a prefix instruction. It makes the LOCK pin low till the execution of the next instruction.
String Instructions
o String is series of bytes or series of words stored in sequential memory locations. The 8086
provides some instructions which handle string operations such as string movement,
comparison, scan, load and store.
Instruction Description
MOVS/MOVSB/MOVSW Moves 8-bit or 16-bit data from the memory location(s) addressed by SI register to the memo
location addressed by DI register.
CMPS/CMPSB/CMPSW Compares the content of memory location addressed by DI register with the content of memo
location addressed by SI register.
SCAS/SCASB/SCASW Compares the content of accumulator with the content of memory location addressed by D
register in the extra segment ES.
LODS/LODSB/LODSW Loads 8-bit or 16-bit data from memory location addressed by SI register into AL or A
register.
STOS/STOSB/STOSW Stores 8-bit or 16-bit data from AL or AX register in the memory location addressed by D
register.
Assembler directives
Introduction:
Assembler directives are the directions to the assembler which indicate how an operand or section of
the program is to be processed. These are also called pseudo operations which are not executable by the
microprocessor. The following section explains the basic assembler directives for 8086.
ASSEMBLER DIRECTIVES:
1. ASSUME : The ASSUME directive is used to inform the assembler the name of the logical segment
it should use for a specified segment.
Ex: ASSUME DS: DATA tells the assembler that for any program instruction which refers to the
data segment ,it should use the logical segment called DATA.
2.DB -Define byte. It is used to declare a byte variable or set aside one or more storage locations of
type byte in memory.
For example, CURRENT_VALUE DB 36H tells the assembler to reserve 1 byte of memory for a
variable named CURRENT_ VALUE and to put the value 36 H in that memory location when the
program is loaded into RAM .
3. DW -Define word. It tells the assembler to define a variable of type word or to reserve storage
locations of type word in memory.
4. DD(define double word) :This directive is used to declare a variable of type double word or restore
memory locations which can be accessed as type double word.
5.DQ (define quadword) :This directive is used to tell the assembler to declare a variable 4 words in
length or to reserve 4 words of storage in memory .
6.DT (define ten bytes):It is used to inform the assembler to define a variable which is 10 bytes in
length or to reserve 10 bytes of storage in memory.
7. EQU –Equate It is used to give a name to some value or symbol. Every time the assembler finds
the given name in the program, it will replace the name with the value or symbol we have equated with
that name
8.ORG -Originate : The ORG statement changes the starting offset address of the data.
It allows to set the location counter to a desired value at any point in the program.For example the
statement ORG 3000H tells the assembler to set the location counter to 3000H.
10. END- End program .This directive indicates the assembler that this is the end of the program
module.The assembler ignores any statements after an END directive.
11. ENDP- End procedure: It indicates the end of the procedure (subroutine) to the assembler.
12.ENDS-End Segment: This directive is used with the name of the segment to indicate the end of that
logical segment.
Introduction to MASM
MASM: (Microsoft assembler)
MASM COMMANDS:
Structure of Program:
.model tiny/small/medium/large
.Stack <some number>
.data
; Initialize data
; which is used in program.
.code
; Program logic goes here.
;
end
i) a = b + c – d * e
ii) z = x * y + w – v +u / k
Take the input in consecutive memory locations and also Display the results by using “int xx” of
PROGRAM:
i) a=b+c-d*e
data segment
b db 02
c db 04
d db 01
e db 03
a db 01 dup()
data ends
code segment
start:mov ax,data
mov ds,ax
mov al,b
mov bl,c
add al,bl
mov cl,d
sub al,cl
mov dl,e
mul dl
mov a,al
int 03
code ends
end start
RESULT:AX=000F
ii) Z=X*Y+W-V+U/K
assume cs:code,ds:data
data segment
x db 02
y db 04
w db 09
v db 03
u dw 0006
k db 03
z db 01 dup()
data ends
code segment
start:mov ax,data
mov ds,ax
mov al,x
mov bl,y
mul bl
mov cl,w
add al,cl
mov dl,v
sub al,dl
mov cl,al
mov ax,u
mov bl,k
div bl
add cl,al
mov z,cl
int 03
code ends
end start
RESULT:CL=0010