by Jesmin Akther | Jul 5, 2021 | Microprocessor & Microcontroller
Peripheral Devices
In order to communicate with the outside world microcomputers, use peripherals (I/O devices). Commonly used peripherals are: A/D converter, D/A converter, CRT, printers, Hard disks, floppy disks, magnetic tapes etc. Peripherals are connected to the microcomputer through electronic circuits known as interfacing circuits.
Programmable Peripheral interface (PPI)
A programmable peripheral interface is a multiport device. The ports may be programmed in a variety of ways as required by the programmer. The device is very useful for interfacing peripheral devices. The term PIA, Peripheral Interface Adapter is also used by some manufacturer.
Intel 8255
The Intel 8255 is a programmable peripheral interface (PPI). It has two versions, i.e. the Intel 8255A and Intel 8255A-5. General descriptions for both are same. There are some differences in their electrical characteristics. Hereafter, they will be referred to as 8255. Its main functions are to interface peripheral devices to the microcomputer.
It has three 8-bit ports, namely Port A, Port B and Port C. The port C has been further divided into two 4-bit ports, port C upper and Port C lower. Thus a total of 4-ports are available, two 8-bit ports and two 4-bit ports. Each port can be programmed either as an input port or an output port.
Operating Modes of 8255
The Intel 8255 has the following three modes of operation which are selected by software:
Mode 0 – Simple Input/output: The 8255 has two 8-bit ports (Port A and Port B) and two 4-bit ports (Port Cupper and Port Clower). In Mode 0 operation, a port can be operated as a simple input or output port. Each of the 4 ports of 8255 can be programmed to be either an input or output port.
Mode 1-Strobed Input/output: Mode 1 is strobed input/output mode of operation. The Port A and Port B both are designed to operate in this mode of operation. When Port A and Port B are programmed in Mode 1, six pins of Port C are used for their control.
Mode 2 -Bidirectional Port: Mode 2 is strobed bidirectional mode of operation. In this mode Port A can be programmed to operate as a bidirectional port. The mode 2 operation is only for Port A. When Port A is programmed in Mode 2, the port B can be used either Mode 1 or Mode 0.
Architecture of Intel 8255

Intel 8255A pin description
It is a 40 pin I.C. package. It operates on a single 5 Vd.c. supply. Its important characteristics are as follows:
Ambient temperature 0 to 700C, Voltage on any pin: 0.5 V to 7 V. Power dissipation 1 Watt VIL = Input low voltage = Minimum 0.5 V, Maximum 0.8 V. VIH = Input high voltage = Minimum 2 V, Maximum Vcc. VOL = Output low voltage = 0.45 V VOH = Output High Voltage = 2.4 V IDR = Darlington drive connect = Minimum 1 mA, Maximum 4 mA of any 8 pins of the port.

The pins of various ports are as follows:
PA0 – PA7 8 Pins of port A PB0 – PB7 8 pins of port B PC0 – PC3 4 pins of port Clower PC4 – PC7 4 pins of Port Cupper
The important control signals are:
CS (Chip Select): It is a chip select signal. The LOW status of this signal enables communication between the CPU and 8255.
RD (READ): When RD goes LOW the 8255 sends out data or status information to the CPU on the data bus. In other words it allows the CPU to read data from the input port of 8255.
WR (Write): When WR goes LOW the CPU writes data or control word into 8255. The CPU writes data into the output port of 8255 and the control word into the control word register.
RESET: RESET is an active high signal. It clears the control register and sets all ports in the input mode.
A0 and A1: The selection of input port and control word register is done using A0 and A1 in conjunction with RD and WR. A0 and A1 are normally connected to the least significant bits of the address bus. If two 8255 units are used the addresses of ports are as follows:
For the First unit of 8255, i.e. 8255.1:
Port/Control word register |
Port/Control word register Address |
Port A |
00 |
Port B |
01 |
Port C |
02 |
Control word register |
03 |
For the 2nd unit of 8255, i.e. 8255.2:
Port/Control word register |
Port/Control word register Address |
Port A |
08 |
Port B |
09 |
Port C |
0A |
Control word register |
0B |
8253 Programmable Interval Timer
A programmable counter or interval timer is used in real time application for timing and counting function such as BCD or binary counting, generation of accurate time delay, generation of square wave of desired frequency, rate generation, hardware/software triggered strobe signal, one shot signal of wanted width etc.
Popular programmable interval timer chips are Intel 8253 and 8254. Both are pin to pin compatible and operate in the following six modes:
Mode 0: Interrupt on terminal count
Mode 1: Programmable one-shot
Mode 2: Rate generator
Mode 3: Square wave generator
Mode 4: Software triggered mode
Mode 5: Hardware triggered mode
The 8254 is compatible to 8086, 8088, 8085 and most other microprocessors. The 8253 is compatible to 8085 microprocessor. The 8254 is superset of the 8253.
Architecture of Intel 8253/54

Intel 8253
The 8253 is 24-pin IC and operates at 5 Vd.c.. It contains three independent 16-bit counters. The programmer can program 8253 to operate in any one of the 6 operating modes. It operates under software control.

The description of its important pins is as follows:
RD (Read): When this pin is LOW the CPU reads data.
WR: (Write): When this is low, the CPU outputs data in the form of mode information or loading of counters.
A0, A1: These pins are connected to the address bus. These are used to select one of three counters. These are also used to address the control word registers for mode selection.
CS: Chip Select.
D0 – D7: Bidirectional data bus.
CLK0, CLK1 and CLK2 are clock for Counter 0, Counter 1 and Counter 2 respectively.
GATE0, GATE1 and GATE2 are gate terminals of Counter 0, Counter 1 and Counter 2 respectively.
OUT0, OUT1 and OUT2 are output terminals of Counter 0, Counter 1 and Counter 2 respectively.
The 8253 contains a data buffer, read/write logic and control word register as described below:
Data Bus Buffer: This buffer is within 8253. It is a 3-state, bidirectional, 8-bit buffer. It is used to interface 8253 to the system data bus through D0 – D7 lines.
Read/Write logic: The 8253 contains a read/write logic which accepts input from the system bus and then makes control signals for the operation of 8253. The following table displays the status of pins associated with read/write logic for several controls:
CS |
A1 |
A0 |
RD |
WR |
Result |
0 |
0 |
0 |
0 |
1 |
Read Counter No. 0 |
0 |
0 |
1 |
0 |
1 |
Read Counter No. 1 |
0 |
1 |
0 |
0 |
1 |
Read Counter No. 2 |
0 |
0 |
0 |
1 |
0 |
Read Counter No. 0 |
0 |
0 |
1 |
1 |
0 |
Read Counter No. 1 |
0 |
1 |
0 |
1 |
0 |
Read Counter No. 2 |
0 |
1 |
1 |
1 |
0 |
Write Mode word |
0 |
1 |
1 |
0 |
1 |
No-operation 3 state |
0 |
X |
X |
1 |
1 |
No-operation 3 state |
0 |
X |
X |
X |
X |
Disable 3 -state |
Keep it mind, X indicates undefined state. It means that it does not matter whether the state is 0 or 1.
Counter Word Register: When the pins A0, A1 are 11, the control word register is selected. The control word format is shown below:
D7 D6 D5 D4 D3 D2 D1 D0
SC1 |
SC0 |
RL1 |
RL0 |
M2 |
M1 |
M0 |
BCD |
The bits D7 and D6 of the control word are to select one of the 3 counters. D5 and D4 are for loading/reading the count. D3, D2 and D1 are for the selection of operating mode of the selected counter. These are six modes of operation for each counter of 8253. The six modes of operation are: MODE 0, MODE 1, MODE 2, MODE 3, MODE 4 and MODE 5. The bit D0 is for the selection of binary or BCD counting.
8253/54 Operational Modes
8253/54 can be operated in 6 different modes. These modes are:
MODE 0 : Interrupt on Terminal Count
- Mode 0 is used for the generation of accurate time delay under software control.
- One of the counters of 8253 is initialized and loaded with suitable count for the desired time delay.
- When counting is finished the counter interrupts the CPU. On interruption the microprocessor completes the required task which is to be performed after the desired time delay.
- For MODE 0 operation GATE is kept high. While counting is going on the counter output OUT remains LOW. When the terminal count is reached i.e. count reaches 0, the output becomes HIGH until the count is reloaded or new count is loaded.
- When the count is reloaded or OUT becomes LOW and the counter starts its counting operation again.
MODE 1 : Programmable One-Shot
- In MODE 1 the counter acts as a retriggerable and programmable one-shot.
- The LOW to HIGH transition of the signal applied to GATE acts as a trigger signal.
- In this mode of operation OUT becomes firstly HIGH after the mode is set. After mode set operation the counter is loaded by a count value of N. The counter decrements count, and the output (OUT) goes LOW for N clock cycles for every LOW to HIGH transition of the GATE input.
MODE 2 : RATE Generator
- In MODE 2 the counter acts as a simple divide by N counter.
- When this mode is set the output of the counter turn out to be firstly HIGH.
- After mode set operation the counter is loaded by a count of value N.
- For MODE 2 operation GATE is kept HIGH.
- In this mode the output remains HIGH for (N-1) clock pulses and then goes LOW for one clock pulse.
MODE 3 : Square Wave Generation
- In MODE 3 the counter acts as a square wave generator. After mode set operation the counter is loaded by a count of value N.
- For MODE 3 operation GATE is kept back HIGH.
- For even values of N the output remains HIGH for N/2 clock pulses abd then goes LOW for next N/2 clock pulses.
MODE 4 : Software Triggered Strobe
- In MODE 4 operation the output of the counter becomes firstly HIGH after the mode is set.
- GATE is kept HIGH for this mode of operation. The counter begins counting instantly after the count is loaded into the count register.
- When the counter reaches terminal count (i.e. counter content = 0) the output goes LOW for one clock period, then it returns to HIGH.
- The output signal may be used as strobe.
- This mode of operation is referred to as a software triggered strobe because the generation of the strobe signal is triggered by loading the count into the count register.
MODE 5 : Hardware Triggered Strobe
- operation GATE input acts as a trigger.
- After the mode is set, the output becomes initially HIGH.
- A count value of N is loaded into the counter.
- a LOW to HIGH transition of the GATE input the counter starts decrementing the count.
- The counting begins at the first negative edge of the clock after the rising edge of the GATE input.
- On terminal count the output goes LOW for one clock period, and then it goes HIGH again.
- the LOW to HIGH transition of the GATE input causes triggering, this mode is referred to as hardware triggered strobe.
by Jesmin Akther | Jul 4, 2021 | Microprocessor & Microcontroller
Microcontroller
Microcontroller contains all essential components of a microcomputer such as CPU, RAM, ROM/EPROM, I/O lines etc. Some single chip microcontrollers contain devices to perform specific functions such as DMA channels, A/D converter, serial port, pulse width modulation, etc. So, a microcomputer made on a single semiconductor chip is called single-chip microcomputer. Since, single chip microcomputers are generally used in control applications, they are also called microcontrollers. A common questions on Microprocessor and Microcontroller differences mention below.
Difference between Microprocessor and Microcontroller
Microprocessors |
Microcontrollers |
It is used for big applications. |
It is used to execute a single task within an application. |
Microprocessor is the heart of computer system. |
It is the heart of the embedded system. |
It is just a processor. Memory and I/O components have to be having to be connected externally. |
Microcontroller contains external processor along with internal memory and I/O components. |
Since I/O and memory connected externally, the circuit becomes large. |
Since I/O and memory present internally, the circuit is small. |
Can’t be used in compact systems and hence inefficient. |
Can be used in compact systems and microcontroller is an efficient technique. |
Cost of entire system increases. |
Cost of entire system is low. |
Power consumption is high. |
Power consumption is low. |
Most of the microprocessors do not have power saving modes. |
Most of the microcontrollers have power saving mode. |
Difficult to replace. |
Easy to replace. |
Mainly used in personal computers. |
Used mainly in washing machine, MP3 players. |
8051 Architecture
In 1980, Intel introduced a powerful 8051 series of 8 -bit microcontrollers. They are the second generation of 8-bit microcontrollers. The 8051 microcontrollers are used for a variety of applications involving limited calculations and relatively some control strategies. They are used for industrial and commercial control applications, appliances control, instrumentation etc.
The 8051 contains Boolean processor, full duplex serial port and power saving circuitry in addition to essential components such as 8-bit CPU, RAM, ROM/EPROM/OTPROM, timer/counter and parallel I/O lines.
The following figure shows the block diagram or architecture of Intel 8051 microcontroller.

8051 Pins Description
The pin diagram of 8051 microcontroller consists of 40 pins as given below:

Pin 1-8 (Port1): These are 8-bit bidirectional I/O port with internal pull-up resisters. It does not perform any task; it is just an I/O port
Pin 9 (RST): It is a Reset input pin which is used to reset the microcontroller to its initial position.
Pin 10 to 17 (Port 3): It is also an 8-bit bidirectional I/O port with internal pull-up resisters. Additionally, it performs some special functions:
PORT 3 Pin |
Function |
Description |
P3.0 |
RXD |
Serial Input |
P3.1 |
TXD |
Serial Output |
P3.2 |
INT0 |
External Interrupt 0 |
P3.3 |
INT1 |
External Interrupt 1 |
P3.4 |
T0 |
Timer 0 |
P3.5 |
T1 |
Timer 1 |
P3.6 |
WR |
External Memory Write |
P3.7 |
RD |
External Memory Read |
Pin 18 and 19: It is XTAL1 and XTAL1 pins respectively. These pins are used for connecting an external crystal to get the system clock.
Pin 20 (GND): It is a ground pin. It provides the power supply to the circuit.
Pin 21 to 28 (Port 2): These pins are bidirectional I/O port. Higher order address bus signals are multiplexed with this bidirectional port.
Pin 29 (PSEN): It is a Program Enable Pin. Using this PSEN pin external program memory can be read.
Pin 30 (ALE/PROG): This pin is the Address Latch Enable pin. Using this pin, external address can be separated from data.
Pin 31 (EA/VPP): Named as external Access Enable Pin (EA). It is used to enable or disable the external memory interfacing.
Pin 32 – 39 (Port 0): These are also a bidirectional I/O pins but without any internal pull-ups. Hence, it requires external pins in order to use port 0 pins as I/O port. Lower order data and address bus signals are multiplexed with this port.
Pin 40 (VCC): This pin is used to supply power to the circuit.
8051 I/O ports
8051 microcontroller have 4 I/O ports each of 8-bit, which can be configured as input or output. Hence, total 32 I/O pins allows the microcontroller to be connected with the peripheral devices.
Note: Pin can be configured as 0 for output and 1 for the input.
1) PORT 0
P0 can be used as a bidirectional I/O port or it can be used for address/data connected for accessing external memory. When control is 1 the port is used for address or data interfacing. When the control is 0 then the port can be used as a bidirectional I/O port.

Fig: Structure of port 0 pin
PORT 0 as an Input Port
If the control is 0 then the port is used as an input port and 1 is written to the latch. In this type of situation both the output MOSFETs are off. Since the output pin has floats therefore, whatever data written on pin is directly read by read pin.
PORT 0 as an Output Port
If we want to write 1 on pin of P0, a ‘1’ written to the latch which turns ‘off’ the lower FET while due to ‘0’ control signal upper FET also turns off.
Suppose we want to write ‘0’ on pin of port 0, when ‘0’ is written to the latch, the pin is pulled down by the lower FET. Hence the output becomes zero.
2) PORT 1
PORT 1 is dedicated only for I/O interfacing. When used as an output port, not needed to connect additional pull-up resistor like port 0.
To use PORT 1 as an input port ‘1’ has to be written to the latch. In this mode 1 is written to the pin by the external device then it read fine.

Fig: Structure of port 1 pin
3) PORT 2
PORT 2 is used for higher external address byte or a normal I/O port. Here, the I/O operation is similar to PORT 1. Latch of PORT 2 remains stable when Port 2 pin are used for external memory access.

Fig: Structure of port 2 pin
4) PORT 3
Following are the alternate functions of PORT 3:

Fig: Structure of port 3 pin
PORT 3 Pin |
Function |
Description |
P3.0 |
RXD |
Serial Input |
P3.1 |
TXD |
Serial Output |
P3.2 |
INT0 |
External Interrupt 0 |
P3.3 |
INT1 |
External Interrupt 1 |
P3.4 |
T0 |
Timer 0 |
P3.5 |
T1 |
Timer 1 |
P3.6 |
WR |
External Memory Write |
P3.7 |
RD |
External Memory Read |
It works as an I/O port same like port 2. Alternate functions of port 3 makes its architecture different than other ports.
8051 interrupts
Interrupt is a process of creating a temporary halt main program and pass the control to the external sources and execute their task and then passes the control to the main program where it held left off.
8051 has 5 interrupt signals, i.e.
- INT0,
- TFO,
- INT1,
- TF1,
- RI/TI.
The number of interrupt sources differs from version to version. It varies from 5 to 15.
The important interrupt sources are: one from the serial port, two from timers, two from external interrupts INT0 and INT1.
Each of the interrupts can individually be enabled/disabled by setting/clearing a bit in the special function register IE (interrupt enable). The IE register also contains a global disable bit, which disables all the interrupts.
Each interrupt can also be programmed to one of the priority level scheme by setting/clearing bits in the special function register IP (Interrupt Priority register).
A low priority interrupt can be interrupted by a high priority interrupt, but it cannot be interrupted by another low priority interrupt. A high priority interrupt can’t be interrupted by a low priority interrupt.
by Jesmin Akther | Jul 4, 2021 | Microprocessor & Microcontroller
Multiprocessor Configuration
Multiprocessor is the set of multiple processors that executes instructions simultaneously. There are basically three configurations of multiprocessor:
- Coprocessor Configuration
- Closely Coupled Configuration
- Loosely Coupled Configuration
Coprocessor Configuration
A coprocessor performs the similar task which the microprocessor performs. It is a specially designed circuit on microprocessor chip used to decline the work load of the main processor. It shares the same memory, I/O system, bus, control logic and clock generator. The coprocessor handles special task like mathematical calculations, graphical display on screen etc.
Intel 8086 and 8087 cannot perform complex mathematical operations, so in these cases, the microprocessor requires the math coprocessor like Intel 8087 math coprocessor, which can easily perform complex mathematical operations very quickly.

Fig: Block Diagram of Coprocessor configuration
Connection between processor and coprocessor
- The processor and coprocessor are connected via RQ-/GT-, TEST, QS0 and QS1 signals.
- TEST signal is connected to BUSY pin of coprocessor and the remaining 3 pins are connected to the same pins of the coprocessor.
- RQ-/GT- : This signal is used for the arbitration of bus.
- QS0 and QS1: These are used to track the status of the queue of the host processor.
- TEST: This signal is used to take care of the coprocessor?s activity i.e. the coprocessor is idle or busy.
Closely Coupled Configuration
Closely coupled configuration is exactly similar to the coprocessor configuration. Like coprocessor, it also shares the same memory, I/O system bus, and control logic and control generator with the host processor. However, the host processor and the coprocessor fetch and execute their own instructions. The system bus is handled by the coprocessor and the host processor independently.

Fig: Block diagram of closely coupled configuration
How are the processor and independent processor connected?
- Memory space is used to provide the connection between independent processor and host processor.
- It does not use any instructions like WAIT, ESC etc. for communication.
- The host processor sends the commands to the ports for managing the memory and for waking up the independent processor.
- The independent processor accesses the memory and executes the task.
- When the task has been completed, it sends an acknowledgement to the host processor by using the status signal or an interrupt request.
Loosely Coupled Configuration
A loosely coupled multiprocessor configuration consists of the number of modules of the microprocessor based systems, which are connected through a common system bus. Each module of this configuration consists of their own clock generator, memory, I/O devices and is connected through a local bus.

Fig: Block Diagram of loosely coupled configuration
8087 Numeric Data Processor
8087 numeric data processor is also known as Math Co-Processor, Floating point unit or Numeric processor extension.
It was first designed by Intel to pair with 8086/8088 resulting in easier and faster calculations.
Once the instructions are identified by the 8086/8088 processor, then it is sent to the 8087 co-processor for further execution.
The data types supported by Intel 8087 are:
- Binary integers
- Packed decimal numbers
- Real numbers
- Temporary real format
Features of 8087 numeric data processor
- It supports different data types like integer, float, and real types ranging from 2-10 bytes.
- It follows IEEE floating point standards.
- The processing speed is so high.
8087 Architecture
- 8087 architecture is divided into two group i.e.
- Control Unit (CU)
- Numeric Extension Unit (NEU)
- The Control Unit is responsible for all the communication between the processor and the memory such as it reads and writes memory operands, receives and decodes instructions, maintains parallel queue, etc.
- The Numeric Extension Unit (NEU) is responsible for all the numeric processor instructions like arithmetic, logical, transcendental, and data transfer instructions. It has 8 register stacks, which holds the operands for instructions and their results.
- CU and NEU works asynchronously with each other.
- Intel 8087 uses QS0 and QS1 pins to obtain and identify the instructions fetched by the host CPU.
- All the coprocessor instructions are identified by Escape (ESC) instructions code bits i.e. they start with ‘F’. The coprocessor only executes the Escape instructions while other instructions are executed by the microprocessor.
- Once CPU recognizes ESC code, it triggers the execution of the numeric processor instruction in 8087.
- While executing, the ESC code identifies the coprocessor instruction that require memory operand or not. If the CPU does not require any memory operand, it will directly execute otherwise physical address of the operand is calculated using any one of the addressing modes allowed in 8086 and 1 dummy read cycle is initiated by the CPU.
- Then 8087 is ready with execution outputs, CU gets the control of bus from 8086 and executes a write cycle to write the outputs in the memory at the pre specialized address.
- Microcode control unit generates control signals required for execution of the instruction.
- Programmable shifter is used to shift the operands during the execution of instructions like FMUL and FDIV.

Fig: Architecture of 8087 Coprocessor
8087 Pin Description

Fig: Pin diagram of Intel 8087
- AD0 – AD15: These are time multiplexed address/data lines, which carry addresses during T1 clock cycle and data during T2, T3, Tw and T4 clock cycles.
- A19/S6 – A16/S3: These are the time multiplexed address/status lines. These functions are similar to the pins of Intel 8086.
- BHE/S7: During the first clock cycle, it is used to enable data on to the higher byte of the 8086 data bus and after that during T2, T3, Tw and T4 clock cycles, it works as status line S7.
- QS1 – QS0: These are queue status input signal which is responsible for status of instruction queue.
Conditions of QS1 – QS0 are given below:
QS0 |
QS1 |
Status |
0 |
0 |
No operation |
0 |
1 |
1st byte of opcode from the queue |
1 |
0 |
Empty the queue |
1 |
1 |
Subsequent byte from the queue |
- INT: It is an interrupt signal used to indicate an unmasked exception has been received during the execution.
- BUSY (Output): When BUSY signal high then it indicates a busy state to the CPU.
- READY (input): This signal is used to reject the internal activities of the coprocessor and prepare it for further execution whenever required by the CPU.
- S0, S1, S2: These are the status signals which gives the status of the operation which is used by the Bus Controller 8087 to generate memory and I/O control signals.
S2 |
S1 |
S0 |
Status |
0 |
X |
X |
Unused |
1 |
0 |
0 |
Unused |
1 |
0 |
1 |
Memory Read |
1 |
1 |
0 |
Memory Write |
1 |
1 |
1 |
Passive |
- RQ/GT1 and RQ/GT0: These are Request/Grant signals used by the 8087 processors to get the control of the bus from the host processor 8086/8088 for operand transfers.
- CLK (input): This input gives the basic timings for the processor operation.
- VCC: Power supply +5V.
Memory and I/O Interfacing
Several memory chips and I/O devices are connected to a microprocessor.
The following figure shows a schematic diagram to interface memory chips and I/O devices to a microprocessor.

Memory Interfacing
When we are executing any instruction, the address of memory location or an I/O device is sent out by the microprocessor. The corresponding memory chip or I/O device is selected by a decoding circuit.
Memory requires some signals to read from and write to registers and microprocessor transmits some signals for reading or writing data.
The interfacing process includes matching the memory requirements with the microprocessor signals. Therefore, the interfacing circuit should be designed in such a way that it matches the memory signal requirements with the microprocessor’s signals.
I/O interfacing
As we know, keyboard and displays are used as communication channel with outside world. Therefore, it is necessary that we interface keyboard and displays with the microprocessor. This is called I/O interfacing. For this type of interfacing, we use latches and buffers for interfacing the keyboards and displays with the microprocessor. However, the main drawback of this interfacing is that the microprocessor can perform only one function.
8279 Programmable Keyboard
The Intel 8279 is a programmable keyboard interfacing device. Data input and display are the integral part of microprocessor kits and microprocessor-based systems.
8279 has been designed for the purpose of 8-bit Intel microprocessors.
8279 has two sections namely keyboard section and display section.
The function of the keyboard section is to interface the keyboard which is used as input device for the microprocessor. It can also interface toggle or thumb switches.
The purpose of the display section is to drive alphanumeric displays or indicator lights. It is directly connected to the microprocessor bus.
The microprocessor is relieved from the burden of scanning the keyboard or refreshing the display.
Some important Features are:
- Simultaneous keyboard display operations
- Scanned sensor mode
- Scanned keyboard mode
- 8-character keyboard FIFO
- Strobed input entry mode
- 2-key lock out or N-key roll over with contact debounce
- Single 16-charcter display
- Dual 8 or 16 numerical display
- Interrupt output on key entry
- Programmable scan timing and mode programmable from CPU
8257 DMA Controller
The data transfer from fast I/O devices to the memory or from the memory to I/O devices through the accumulator is a time consuming process. For this situation, the Direct Memory Access (DMA) technique is preferred. In DMA data transfer scheme, data is directly transferred from an I/O device to RAM or from RAM to an I/O device.
Using a DMA controller, the device requests the CPU to hold its address, data and control bus, so the device is free to transfer data directly to/from the memory. The DMA data transfer is initiated only after receiving HLDA signal from the CPU.
DMA operations:
Following are the operations performed by a DMA:
- Initially, the device has to send DMA request (DRQ) to DMA controller for sending the data between the device and the memory.
- The DMA controller sends Hold request (HRQ) to the CPU and waits for the CPU for the HLDA.
- When CPU gets the HLDA signal then, it leaves the control over the bus and acknowledges the HOLD request through HLDA signal.
- Now the CPU is in the HOLD state and the DMA controller has to manage the operations over the buses between the CPU, memory and I/O devices.
Intel 8257
- The Intel 8257 is a programmable DMA controller.
- It is a 4-channel programmable Direct Memory Access (DMA) controller.
- It is a 40 pin I.C. package and requires +5V supply for its operation.
- It can perform three operations, namely read, write, and verify.
- Each channel incorporates two 16-bit registers, namely DMA address register and byte count register.
- Each channel can transfer data up to 64kb and can be programmed independently.
- It operates in 2 -modes: Master mode and Slave mode.
by Jesmin Akther | Jul 4, 2021 | Microprocessor & Microcontroller
Interrupts in 8086 microprocessor
Interrupt is a process of generating a temporary halt throughout program execution and permits peripheral devices to access the microprocessor.

Microprocessor answers to these interrupts with an interrupt service routine (ISR), which is a short program or subroutine to instruct the microprocessor on how to handle the interrupt.
There are different types of interrupt in 8086:
Hardware Interrupts
Hardware interrupts are that type of interrupt which are caused by any peripheral device by sending a signal through a specified pin to the microprocessor.
The Intel 8086 has two hardware interrupt pins:
- NMI (Non-Maskbale Interrupt)
- INTR (Interrupt Request) Maskable Interrupt.
NMI: NMI is a single Non-Maskable Interrupt having higher priority than the maskable interrupt.
- It cannot be disabled (masked) by user using software.
- It is used by the processor to handle emergency conditions.
For example: It can be used to save program and data in case of power failure. An external electronic circuitry is used to detect power failure, and to send an interrupt signal to 8086 through NMI line.
INTR: The INTR is a maskable interrupt. It can be enabled/disabled using interrupt flag (IF). After receiving INTR from external device, the 8086 acknowledges through INTA signal.
It executes two consecutive interrupt acknowledge bus cycles.
Software Interrupt
A microprocessor can also be interrupted by internal irregular conditions like as overflow; division by zero; etc. A programmer can also interrupt microprocessor by inserting INT instruction at the chosen point in the program while debugging a program. Such an interrupt is called a software interrupt. The interrupt caused by an internal abnormal conditions also came under the heading of software interrupt.
Example of software interrupts are:
- TYPE 0 (division by zero)
- TYPE 1 (single step execution for debugging a program)
- TYPE 2 represents NMI (power failure condition)
- TYPE 3 (break point interrupt)
- TYPE 4 (overflow interrupt)
Interrupt pointer table for 8086

Fig: Interrupt pointer table for 8086
The 8086 can handle up to 256, hardware and software interrupts.
1KB memory performances as a table to contain interrupt vectors (or interrupt pointers), and it is called interrupt vector table or interrupt pointer table. The 256 interrupt pointers have been numbered from 0 to 255 (FF hex). The number assigned to an interrupt pointer is known as type of that interrupt. For example, Type 0, Type 1, Type 2,………..Type 255 interrupt.
Addressing modes of 8086
An operand which way is specified for an instruction in the accumulator, in a general-purpose register or in memory location, is called addressing mode.
The 8086 microprocessors have 8 addressing modes. Two addressing modes have been as long as for instructions which operate on register or immediate data.
These two addressing modes are:
Register Addressing: In register addressing, the operand is placed in one of the 16-bit or 8-bit general purpose registers.
Example
- MOV AX, CX
- ADD AL, BL
- ADD CX, DX
Immediate Addressing: In immediate addressing, the operand is specified in the instruction itself.
Example
- MOV AL, 35H
- MOV BX, 0301H
- MOV [0401], 3598H
- ADD AX, 4836H
The remaining 6 addressing modes specify the location of an operand which is placed in a memory.
These 6 addressing modes are:
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 0102 to AL. the operand is placed at the given offset (0102) 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.
Effective address (Offset) = [BX + 8-bit or 16-bit displacement].
Example
- MOV AL, [BX+05]; an example of 8-bit displacement.
- MOV AL, [BX + 1346H]; example of 16-bit displacement.
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.
Offset (Effective Address) = [SI or DI + 8-bit or 16-bit displacement]
Example
- MOV AX, [SI + 05]; 8-bit displacement.
- MOV AX, [SI + 1528H]; 16-bit displacement.
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.
Effective Address (Offset) = [BX or BP] + [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
- ADD AX, [BX + SI]
- MOV CX, [BX + SI]
Based Indexed with Displacement: In this mode of addressing, the operand’s offset is given by:
Effective Address (Offset) = [BX or BP] + [SI or DI] + 8-bit or 16-bit displacement
Example
- MOV AX, [BX + SI + 05]; 8-bit displacement
- MOV AX, [BX + SI + 1235H]; 16-bit displacement
by Jesmin Akther | Jul 4, 2021 | Microprocessor & Microcontroller
8086 pins configuration

The description of the pins of 8086 is as follows:
Address Data Bus PIN in details:
AD0-AD15 (Address Data Bus): Bidirectional address/data lines. These are low order address bus. They are multiplexed with data. When these lines are used to transmit memory address, the symbol A is used instead of AD, for example, A0- A15.
A16 – A19 (Output): High order address lines. These are multiplexed with status signals.
A16/S3, A17/S4: A16 and A17 are multiplexed with segment identifier signals S3 and S4.
A18/S5: A18 is multiplexed with interrupt status S5.
A19/S6: A19 is multiplexed with status signal S6.
BHE/S7 (Output):
Bus High Enable/Status. During T1, it is low. It enables the data onto the most significant half of data bus, D8-D15. 8-bit device connected to upper half of the data bus use BHE signal. It is multiplexed with status signal S7. S7 signal is available during T3 and T4.
RD (Read):
For read operation. It is an output signal. It is active when LOW. Ready (Input): The addressed memory or I/O sends acknowledgment through this pin. When HIGH, it denotes that the peripheral is ready to transfer data.
RESET (Input): System reset. The signal is active HIGH.
CLK (input): Clock 5, 8 or 10 MHz
INTR: Interrupt Request.
NMI (Input): Non-maskable interrupt request.
TEST (Input): Wait for test control. When goes to LOW the microprocessor continues execution otherwise waits.
VCC: Power supply +5V dc.
GND: Ground.
Operating Modes of 8086
There are two operating modes of operation for Intel 8086, namely the minimum mode and the maximum mode. When only one 8086 CPU is to be used in a microprocessor system, the 8086 is used in the Minimum mode of operation. In a multiprocessor system 8086 operates in the Maximum mode.
Pin Description for Minimum Mode
In this minimum mode of operation, the pin MN/MX is connected to 5V D.C. supply i.e. MN/MX = VCC.
The description about the pins from 24 to 31 for the minimum mode is as follows:
INTA (Output):
Pin number 24 interrupts acknowledgement. On receiving interrupt signal, the processor issues an interrupt acknowledgment signal. It is active LOW.
ALE (Output):
Pin no. 25. Address latch enable. It goes HIGH during T1. The microprocessor 8086 sends this signal to latch the address into the Intel 8282/8283 latch.
DEN (Output):
Pin no. 26. Data Enable. When Intel 8287/8286 octal bus transceiver is used this signal. It is active LOW.
DT/R (output):
Pin No. 27 data Transmit/Receives. When Intel 8287/8286 octal bus transceiver is used this signal controls the direction of data flow through the transceiver. When it is HIGH, data is sent out. When it is LOW, data is received.
M/IO (Output):
Pin no. 28, Memory or I/O access. When this signal is HIGH, the CPU wants to access memory. When this signal is LOW, the CPU wants to access I/O device.
WR (Output): Pin no. 29, Write. When this signal is LOW, the CPU performs memory or I/O write operation.
HLDA (Output):
Pin no. 30, Hold Acknowledgment. It is sent by the processor when it receives HOLD signal. It is active HIGH signal. When HOLD is removed HLDA goes LOW.
HOLD (Input):
Pin no. 31, Hold. When another device in microcomputer system wants to use the address and data bus, it sends HOLD request to CPU through this pin. It is an active HIGH signal.
Pin Description for Maximum Mode
In the maximum mode of operation, the pin MN/¯MX is made LOW. It is grounded. The description about the pins from 24 to 31 is as follows:
QS1, QS0 (Output): Pin numbers 24, 25, Instruction Queue Status. Logics are given below:
QS1 |
QS0 |
Operation |
0 |
0 |
No operation |
0 |
1 |
1st byte of opcode from queue. |
1 |
0 |
Empty the queue |
1 |
1 |
Subsequent byte from queue |
S0, S1, S2 (Output):
Pin numbers 26, 27, 28 Status Signals. These signals are connected to the bus controller of Intel 8288. This bus controller generates memory and I/O access control signals. Logics for status signal are given below:
S2 |
S1 |
S0 |
Operation |
0 |
0 |
0 |
Interrupt acknowledgement |
0 |
0 |
1 |
Read data from I/O port |
0 |
1 |
0 |
Write data from I/O port |
0 |
1 |
1 |
Halt |
1 |
0 |
0 |
Opcode fetch |
1 |
0 |
1 |
Memory read |
1 |
1 |
0 |
Memory write |
1 |
1 |
1 |
Passive state |
LOCK (Output):
Pin no. 29. It is an active LOW signal. When this signal is LOW, all interrupts are masked and no HOLD request is granted. In a multiprocessor system all other processors are informed through this signal that they should not ask the CPU for relinquishing the bus control.
RG/GT1, RQ/GT0 (Bidirectional):
Pin numbers 30, 31, Local Bus Priority Control. Other processors ask the CPU by these lines to release the local bus. In the maximum mode of operation signals WR, ALE, DEN, DT/R etc. are not available directly from the processor. These signals are available from the controller 8288.
The 8086 microprocessor supports 8 types of instructions
- Data Transfer Instructions
- Arithmetic Instructions
- Bit Manipulation Instructions
- String Instructions
- Program Execution Transfer Instructions (Branch & Loop Instructions)
- Processor Control Instructions
- Iteration Control Instructions
- Interrupt Instructions
Data Transfer Instructions:
These instructions are used to transfer the data from the source operand to the destination operand. Following are the list of instructions under this group:
Instruction to transfer a word
MOV: Used to copy the byte or word from the provided source to the provided destination.
PPUSH: Used to put a word at the top of the stack.
POP: Used to get a word from the top of the stack to the provided location.
PUSHA: Used to put all the registers into the stack.
POPA: Used to get words from the stack to all registers.
XCHG: Used to exchange the data from two locations.
XLAT: Used to translate a byte in AL using a table in the memory.
Instructions for input and output port transfer
IN: Used to read a byte or word from the provided port to the accumulator.
OUT: Used to send out a byte or word from the accumulator to the provided port.
Instructions to transfer the address
LEA: Used to load the address of operand into the provided register.
LDS: Used to load DS register and other provided register from the memory
LES: Used to load ES register and other provided register from the memory.
Instructions to transfer flag registers
LAHF: Used to load AH with the low byte of the flag register.
SAHF: Used to store AH register to low byte of the flag register.
PUSHF : Used to copy the flag register at the top of the stack.
POPF: Used to copy a word at the top of the stack to the flag register.
Arithmetic Instructions
These instructions are used to perform arithmetic operations like addition, subtraction, multiplication, division, etc. Following is the list of instructions under this group:
Instructions to perform addition
- ADD: Used to add the provided byte to byte/word to word.
- ADC: Used to add with carry.
- INC: Used to increment the provided byte/word by 1.
- AAA: Used to adjust ASCII after addition.
- DAA: Used to adjust the decimal after the addition/subtraction operation.
Instructions to perform subtraction
- SUB: Used to subtract the byte from byte/word from word.
- SBB: Used to perform subtraction with borrow.
- DEC: Used to decrement the provided byte/word by 1.
- NPG: Used to negate each bit of the provided byte/word and add 1/2’s complement.
- CMP: Used to compare 2 provided byte/word.
- AAS: Used to adjust ASCII codes after subtraction.
- DAS: Used to adjust decimal after subtraction.
Instruction to perform multiplication
- MUL : Used to multiply unsigned byte by byte/word by word.
- IMUL: Used to multiply signed byte by byte/word by word.
- AAM: Used to adjust ASCII codes after multiplication.
Instructions to perform division
DIV − Used to divide the unsigned word by byte or unsigned double word by word.
- IDIV − Used to divide the signed word by byte or signed double word by word.
- AAD − Used to adjust ASCII codes after division.
- CBW − Used to fill the upper byte of the word with the copies of sign bit of the lower byte.
- CWD − Used to fill the upper word of the double word with the sign bit of the lower word.
Bit Manipulation Instructions
These instructions are used to perform operations where data bits are involved, i.e. operations like logical, shift, etc.
Following is the list of instructions under this group:
Instructions to perform logical operation
- NOT − Used to invert each bit of a byte or word.
- AND − Used for adding each bit in a byte/word with the corresponding bit in another byte/word.
- OR − Used to multiply each bit in a byte/word with the corresponding bit in another byte/word.
- XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with the corresponding bit in another byte/word.
- TEST − Used to add operands to update flags, without affecting operands.
Instructions to perform shift operations
- SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in LSBs.
- SHR − Used to shift bits of a byte/word towards the right and put zero(S) in MSBs.
- SAR − Used to shift bits of a byte/word towards the right and copy the old MSB into the new MSB.
Instructions to perform rotate operations
- ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to Carry Flag [CF].
- ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to Carry Flag [CF].
- RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to MSB.
- RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF to LSB.
String Instructions
String is a group of bytes/words and their memory is always allocated in a sequential order.
Following is the list of instructions under this group:
- REP − Used to repeat the given instruction till CX ≠ 0.
- REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
- REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
- MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to another.
- COMS/COMPSB/COMPSW − Used to compare two string bytes/words.
- INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the provided memory location.
- OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the provided memory location to the I/O port.
- SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in AL or string word with a word in AX.
- LODS/LODSB/LODSW − Used to store the string byte into AL or string word into AX.
Program Execution Transfer Instructions (Branch and Loop Instructions)
These instructions are used to transfer/branch the instructions during an execution. It includes the following instructions −
Instructions to transfer the instruction during an execution without any condition:
- CALL − Used to call a procedure and save their return address to the stack.
- RET − Used to return from the procedure to the main program.
- JMP − Used to jump to the provided address to proceed to the next instruction.
- Instructions to transfer the instruction during an execution with some conditions −
- JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
- JAE/JNB − Used to jump if above/not below instruction satisfies.
- JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
- JC − Used to jump if carry flag CF = 1
- JE/JZ − Used to jump if equal/zero flag ZF = 1
- JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
- JGE/JNL − Used to jump if greater than/equal/not less than instruction satisfies.
- JL/JNGE − Used to jump if less than/not greater than/equal instruction satisfies.
- JLE/JNG − Used to jump if less than/equal/if not greater than instruction satisfies.
- JNC − Used to jump if no carry flag (CF = 0)
- JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
- JNO − Used to jump if no overflow flag OF = 0
- JNP/JPO − Used to jump if not parity/parity odd PF = 0
- JNS − Used to jump if not sign SF = 0
- JO − Used to jump if overflow flag OF = 1
- JP/JPE − Used to jump if parity/parity even PF = 1
- JS − Used to jump if sign flag SF = 1
Processor Control Instructions
These instructions are used to control the processor action by setting/resetting the flag values.
Following are the instructions under this group:
- STC − Used to set carry flag CF to 1
- CLC − Used to clear/reset carry flag CF to 0
- CMC − Used to put complement at the state of carry flag CF.
- STD − Used to set the direction flag DF to 1
- CLD − Used to clear/reset the direction flag DF to 0
- STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.
- CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.
Iteration Control Instructions
These instructions are used to execute the given instructions for number of times. Following is the list of instructions under this group −
- LOOP − Used to loop a group of instructions until the condition satisfies, i.e., CX = 0
- LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF = 1 & CX = 0
- LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies ZF = 0 & CX = 0
- JCXZ − Used to jump to the provided address if CX = 0
Interrupt Instructions
These instructions are used to call the interrupt during program execution.
- INT − Used to interrupt the program during execution and calling service specified.
- INTO − Used to interrupt the program during execution if OF = 1
- IRET − Used to return from interrupt service to the main program
by Jesmin Akther | Jul 4, 2021 | Microprocessor & Microcontroller
8086 Microprocessor
8086 Microprocessor is a boosted version of 8085 Microprocessor designed by Intel in 1976. This is a 16-bit Microprocessor having 20 address lines and 16 data lines which provides up to 1MB storage. It consists of powerful instruction set provides operations such as multiplication and division easily. It supports two modes of operation such as Maximum mode and Minimum mode. Maximum mode is suitable for system having multiple processors and Minimum mode is suitable for system having a single processor.
Features of 8086
The most prominent features of 8086 microprocessor are as follows:
- It has an instruction queue of storing six instruction bytes from the memory resulting in faster processing.
- It was the first 16-bit processor having 16-bit ALU, 16-bit registers, internal data bus, and 16-bit external data bus resulting in faster processing.
- This is available in 3 versions based on the frequency of operation:
- 8086 → 5MHz
- 8086-2 → 8MHz
- (c)8086-1 → 10 MHz
- It uses two stages of pipelining, such as Fetch Stage and Execute Stage, which improves performance.
- Fetch stage can prefetch up to 6 bytes of instructions and stores them in the queue.
- Execute stage executes these instructions.
- It has 256 vectored interrupts.
- This is consists of 29,000 transistors.
Comparison between 8085 & 8086 Microprocessor
- Size: 8085 is 8-bit microprocessor, whereas 8086 is 16-bit microprocessor.
- Address Bus: 8085 has 16-bit address bus while 8086 has 20-bit address bus.
- Memory: 8085 can access up to 64Kb, whereas 8086 can access up to 1 Mb of memory.
- Instruction: 8085 doesn’t have an instruction queue, whereas 8086 has an instruction queue.
- Pipelining: 8085 doesn’t support a pipelined architecture while 8086 supports a pipelined architecture.
- I/O: 8085 can address 2^8 = 256 I/O’s, whereas 8086 can access 2^16 = 65,536 I/O’s.
- Cost: The cost of 8085 is low whereas that of 8086 is high.
Architecture of 8086
The following diagram depicts the architecture of 8086 Microprocessor:

8086 Microprocessor is divided into two functional units that is EU (Execution Unit) and BIU (Bus Interface Unit).
Execution Unit
EU gives instructions to BIU stating from where to fetch the data and then decode and execute those instructions. Its function is to control operations on data using the instruction decoder & ALU. EU has no direct connection with system buses as shown in the above figure, it performs operations over data over BIU. Let us now discuss the functional parts of 8086 microprocessors.
ALU
It handles all arithmetic and logical operations, like +, −, ×, /, OR, AND, NOT operations.
Flag Register
It is a 16-bit register that behaves like a flip-flop, that is it changes its status according to the result stored in the accumulator. It has 9 flags and they are divided into 2 groups one is Conditional Flags and the other is Control Flags.
Conditional Flags
It represents the result of the last arithmetic or logical instruction executed. Following is the list of conditional flags:
Carry flag:
This flag indicates an overflow condition for arithmetic operations.
Auxiliary flag:
When an operation is performed at ALU, it results in a carry/barrow from lower nibble (that isD0 – D3) to upper nibble (that isD4 – D7), then this flag is set, that is carry given by D3 bit to D4 is AF flag. The processor uses this flag to perform binary to BCD conversion.
Parity flag:
This flag is used to indicate the parity of the result, that is when the lower order 8-bits of the result contains even number of 1’s, then the Parity Flag is set. For odd number of 1’s, the Parity Flag is reset.
Zero flag:
This flag is set to 1 when the result of arithmetic or logical operation is zero else it is set to 0.
Sign flag:
This flag holds the sign of the result, that is when the result of the operation is negative, then the sign flag is set to 1 else set to 0.
Overflow flag:
This flag represents the result when the system capacity is exceeded.
Control Flags
Control flags controls the operations of the execution unit. Following is the list of control flags −
Trap flag:
It is used for single step control and allows the user to execute one instruction at a time for debugging. If it is set, then the program can be run in a single step mode.
Interrupt flag:
It is an interrupt enable/disable flag, that is used to allow/prohibit the interruption of a program. It is set to 1 for interrupt enabled condition and set to 0 for interrupt disabled condition.
Direction flag:
It is used in string operation. As the name suggests when it is set then string bytes are accessed from the higher memory address to the lower memory address and vice-a-versa.
General purpose register
There are 8 general purpose registers, i.e., AH, AL, BH, BL, CH, CL, DH, and DL. These registers can be used individually to store 8-bit data and can be used in pairs to store 16bit data. The valid register pairs are AH and AL, BH and BL, CH and CL, and DH and DL. It is referred to the AX, BX, CX, and DX respectively.
AX register:
It is also known as accumulator register. It is used to store operands for arithmetic operations.
BX register:
It is used as a base register. It is used to store the starting base address of the memory area within the data segment.
CX register:
It is referred to as counter. It is used in loop instruction to store the loop counter.
DX register:
This register is used to hold I/O port address for I/O instruction.
Stack pointer register
It is a 16-bit register, which holds the address from the start of the segment to the memory location, where a word was most recently stored on the stack.
BIU (Bus Interface Unit)
BIU takes care of all data and addresses transfers on the buses for the EU like sending addresses, fetching instructions from the memory, reading data from the ports and the memory as well as writing data to the ports and the memory. EU has no direction connection with System Buses so this is possible with the BIU. EU and BIU are connected with the Internal Bus. It has the following functional parts which are mention below:
Instruction queue:
BIU contains the instruction queue. BIU gets upto 6 bytes of next instructions and stores them in the instruction queue. When EU executes instructions and is ready for its next instruction, then it simply reads the instruction from this instruction queue resulting in increased execution speed. Fetching the next instruction while the current instruction executes is called pipelining.
Segment register:
BIU has 4 segment buses, that is CS, DS, SS& ES. It holds the addresses of instructions and data in memory, which are used by the processor to access memory locations. It also contains 1 pointer register IP, which holds the address of the next instruction to executed by the EU.
CS:
This is stands for Code Segment. It is used for addressing a memory location in the code segment of the memory, where the executable program is stored.
DS:
This is stands for Data Segment. It consists of data used by the program and is accessed in the data segment by an offset address or the content of other register that holds the offset address.
SS:
This is stands for Stack Segment. It handles memory to store data and addresses during execution.
ES:
This is stands for Extra Segment. ES is additional data segment, which is used by the string to hold the extra destination data.
Instruction pointer:
This is is a 16-bit register used to hold the address of the next instruction to be executed.
by Jesmin Akther | Jul 1, 2021 | Microprocessor & Microcontroller
Microprocessor Application
Microprocessor based systems are found everywhere today. It just not in computers and smartphones also in automatic testing of products, speed control of motors, traffic light control, communication equipment, television, satellite communication, home appliances, such as microwave oven, washing machine, gaming controller, industrial controllers and even specialized applications such as military applications.
Microprocessor Applications
A microprocessor makes everyday life smoother because of its low cost, low power, small weight, and vast application in each and every field. There are many applications of microprocessors. Some of the important applications are:
Most processors nowadays are 64bit. With Graphics Processing Unit (GPU) being included inside mobile processors, these devices can now offer high quality graphics, Virtual Reality capability, 3D capability and 4k recording, plus the improved processor technology means greater power efficiency. This includes smartphones, tablets, portable media players – have rather powerful microprocessors, so much so that they can compete with a desktop computer. The latest processor from MediaTek is the Helio P90. The system support features such as intelligent imaging, better and quick photography, faster and more simple gaming, and advanced connectivity with dual 4G SIM.
The programmable thermostat allows the control of temperature at homes. In this system, a microprocessor works with the temperature sensor to determine and adjust the temperature accordingly.
- High-end coffee makers, Washing machines, and radio clocks contain microprocessor technology.
Some other home items that contain microprocessors are: microwaves, toasters, televisions, VCRs, DVD players, ovens, stoves, clothes washers, stereo systems, home computers, alarm clocks, hand-held game devices, thermostats, video game systems, bread machines, dishwashers, home lighting systems and even some refrigerators with digital temperature control.
- Industrial Applications of Microprocessors
Some industrial items which use microprocessors technology include: cars, boats, planes, trucks, heavy machinery, elevators, gasoline pumps, credit-card processing units, traffic control devices, computer servers, most high tech medical devices, surveillance systems, security systems, and even some doors with automatic entry.
Automobiles, trains and planes also use microprocessor technology.
Consumer vehicles-buses, cars, trucks -integrate microprocessors to communicate important information throughout the vehicle. E.g., navigation systems provide information using microprocessors and global positioning system (GPS) technology.
- Computers and Electronics
Microprocessor-drives technology is the brain of the computer. They are used in all type of computers ranging from microcomputers to supercomputers.
A cell phone or mobile device executes game instructions by way of the microprocessor.
VCRs, televisions and gaming platforms also contain microprocessors for executing complex instructions and tasks.
Many medical devices, like an insulin pump, are typically controlled by a microprocessor. The microprocessors perform various functions, such as processing data from bio-sensors, storing measurements, and analyzing results.
Microprocessor is also very useful in the field of instrumentation. Function generators, frequency counters, frequency synthesizers, spectrum analyses and many other instruments are available, when microprocessors are used as controller.
The use of microprocessor in entertainment equipment, toys and home entertaining applications is making them more useful and full of features.
A number of modern devices in the home are microprocessor based i.e. camera; washing machines; calculators; hi-fi systems; telephones; microwave ovens; burglar alarms etc. The input are usually simple numeric keyboards, sensors, buttons or while the output include lights, simple LCD screens displays, motors and relays, LEDs, buzzers etc.
- Office Automation and Publication
Microprocessor based system with software packages has changed the office environment. systems are based on Microprocessors based are being used for spread sheet operations, word processing, storage etc.
The Publication technology has revolutionized by the microprocessor.
In communication the telephone industry is most important. In this industry, microprocessors are used in digital telephone sets, telephone exchanges and modem etc. The use of microprocessor in satellite communication, television, has made teleconferencing possible. Railway reservation and airline reservation system also uses microprocessor technology. WAN (Wide Area Network) and LAN (Local Area Network) for communication of vertical information through computer network.
by Jesmin Akther | Jul 1, 2021 | Assembly Language
Macros with File and Memory Management in Assemble Language
In Assembly macros is modular programming , file management concern with data input output standard and memory management convey the allocation of these data uses.
Macros use in Assembly
Macro is another way of ensuring modular programming in assembly language.It is a sequence of instructions, assigned by a name and could be used anywhere in the program.
- In NASM, macros are defined with %macro and %endmacro directives.
- The macro begins with the %macro directive and ends with the %endmacro directive.The Syntax for macro definition −
%macro macro_name number_of_params
<macro body>
%endmacro
Where, number_of_params specifies the number parameters, macro_name specifies the name of the macro. The macro is invoked by using the macro name along with the necessary parameters. When you need to use some sequence of instructions many times in a program. For example, a very common need for programs is to write a string of characters in the screen. For displaying a string of characters, you need the following sequence of instructions:
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
In the above example of displaying a character string, the registers EAX, EBX, ECX and EDX have been used by the INT 80H function call. So, each time you need to display on screen, you need to save these registers on the stack, invoke INT 80H and then restore the original value of the registers from the stack. So, it could be useful to write two macros for saving and restoring data. Some instructions like IMUL, IDIV, INT, etc., need some of the information to be stored in some particular registers and even return values in some specific register(s). If the program was already using those registers for keeping important data, then the existing data from these registers should be saved in the stack and restored after the instruction is executed.
Example: There is an example shows defining and using macros:
; A macro with two parameters
; Implements the write system call
%macro write_string 2
mov eax, 4
mov ebx, 1
mov ecx, %1
mov edx, %2
int 80h
%endmacro
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
write_string msg1, len1
write_string msg2, len2
write_string msg3, len3
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg1 db 'Hello, programmers!',0xA,0xD
len1 equ $ - msg1
msg2 db 'Welcome to the world of,', 0xA,0xD
len2 equ $- msg2
msg3 db 'Linux assembly programming! '
len3 equ $- msg3
OUTPUT:
Hello, programmers!
Welcome to the world of,
Linux assembly programming!
Assembly in File Management
The system considers any input or output data as stream of bytes. There are three standard file streams:
- Standard input (stdin),
- Standard output (stdout), and
- Standard error (stderr).
File Descriptor
A file descriptor is a 16-bit integer assigned to a file as a file id. So, when a new file is created or an existing file is opened, the file descriptor is used for accessing the file. File descriptor of the standard file streams – stdin, stdout and stderr are 0, 1 and 2, respectively.
File Pointer
A file pointer specifies the location for a subsequent read/write operation in the file in terms of bytes. Each file is considered as a sequence of bytes. Each open file is associated with a file pointer that specifies an offset in bytes, relative to the beginning of the file. When a file is opened, the file pointer is set to zero.
File Handling System Calls
The following table briefly describes the system calls related to file handling:
%eax Name %ebx %ecx %edx
2 sys_fork struct pt_regs – –
3 sys_read unsigned int char * size_t
4 sys_write unsigned int const char * size_t
5 sys_open const char * int int
6 sys_close unsigned int – –
8 sys_creat const char * int –
19 sys_lseek unsigned int off_t unsigned int
The steps required for using the system calls are same, as discussed earlier:
Put the system call number in the EAX register.
Store the arguments to the system call in the registers EBX, ECX, etc.
Call the relevant interrupt (80h).
The result is usually returned in the EAX register.
Creating and Opening a File
For creating and opening a file, perform the following tasks −
Put the system call sys_creat() number 8, in the EAX register.
Put the filename in the EBX register.
Put the file permissions in the ECX register.
The system call returns the file descriptor of the created file in the EAX register, in case of error, the error code is in the EAX register.
Opening an Existing File
For opening an existing file, perform the following tasks:
Put the system call sys_open() number 5, in the EAX register.
Put the filename in the EBX register.
Put the file access mode in the ECX register.
Put the file permissions in the EDX register.
The system call returns the file descriptor of the created file in the EAX register, in case of error, the error code is in the EAX register.
Among the file access modes, most commonly used are: read-only (0), write-only (1), and read-write (2).
Reading from a File
For reading from a file, perform the following tasks:
Put the system call sys_read() number 3, in the EAX register.
Put the file descriptor in the EBX register.
Put the pointer to the input buffer in the ECX register.
Put the buffer size, i.e., the number of bytes to read, in the EDX register.
The system call returns the number of bytes read in the EAX register, in case of error, the error code is in the EAX register.
Writing to a File
For writing to a file, perform the following tasks −
Put the system call sys_write() number 4, in the EAX register.
Put the file descriptor in the EBX register.
Put the pointer to the output buffer in the ECX register.
Put the buffer size, i.e., the number of bytes to write, in the EDX register.
The system call returns the actual number of bytes written in the EAX register, in case of error, the error code is in the EAX register.
Closing a File
For closing a file, perform the following tasks:
Put the system call sys_close() number 6, in the EAX register.
Put the file descriptor in the EBX register.
The system call returns, in case of error, the error code in the EAX register.
Updating a File
For updating a file, perform the following tasks :
Put the system call sys_lseek () number 19, in the EAX register.
Put the file descriptor in the EBX register.
Put the offset value in the ECX register.
Put the reference position for the offset in the EDX register.
The reference position could be:
Beginning of file – value 0
Current position – value 1
End of file – value 2
The system call returns, in case of error, the error code in the EAX register.
Example: The following program creates and opens a file named myfile.txt, and writes a text ‘Welcome to Tutorials Point’ in this file. Next, the program reads from the file and stores the data into a buffer named info. Lastly, it displays the text as stored in info.
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
;create the file
mov eax, 8
mov ebx, file_name
mov ecx, 0777 ;read, write and execute by all
int 0x80 ;call kernel
mov [fd_out], eax
; write into the file
mov edx,len ;number of bytes
mov ecx, msg ;message to write
mov ebx, [fd_out] ;file descriptor
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
; close the file
mov eax, 6
mov ebx, [fd_out]
; write the message indicating end of file write
mov eax, 4
mov ebx, 1
mov ecx, msg_done
mov edx, len_done
int 0x80
;open the file for reading
mov eax, 5
mov ebx, file_name
mov ecx, 0 ;for read only access
mov edx, 0777 ;read, write and execute by all
int 0x80
mov [fd_in], eax
;read from file
mov eax, 3
mov ebx, [fd_in]
mov ecx, info
mov edx, 26
int 0x80
; close the file
mov eax, 6
mov ebx, [fd_in]
int 0x80
; print the info
mov eax, 4
mov ebx, 1
mov ecx, info
mov edx, 26
int 0x80
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
file_name db 'myfile.txt'
msg db 'Welcome to draftsbook'
len equ $-msg
msg_done db 'Written to file', 0xa
len_done equ $-msg_done
section .bss
fd_out resb 1
fd_in resb 1
info resb 26
OUTPUT:
Written to file
Welcome to draftsbook
Memory Management
The sys_brk() system call is provided by the kernel, to allocate memory without the need of moving it later. This call allocates memory right behind the application image in the memory. This system function allows you to set the highest available address in the data section. This system call takes one parameter, which is the highest memory address needed to be set. This value is stored in the EBX register. In case of any error, sys_brk() returns -1 or returns the negative error code itself. The following example demonstrates dynamic memory allocation.
Example
The following program allocates 16kb of memory using the sys_brk() system call:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov eax, 45 ;sys_brk
xor ebx, ebx
int 80h
add eax, 16384 ;number of bytes to be reserved
mov ebx, eax
mov eax, 45 ;sys_brk
int 80h
cmp eax, 0
jl exit ;exit, if error
mov edi, eax ;EDI = highest available address
sub edi, 4 ;pointing to the last DWORD
mov ecx, 4096 ;number of DWORDs allocated
xor eax, eax ;clear eax
std ;backward
rep stosd ;repete for entire allocated area
cld ;put DF flag to normal state
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, len
int 80h ;print a message
exit:
mov eax, 1
xor ebx, ebx
int 80h
section .data
msg db "Allocated 16 kb of memory!", 10
len equ $ - msg
OUTPUT:
Allocated 16 kb of memory!
by Jesmin Akther | Jul 1, 2021 | Assembly Language
Procedures and Recursion used in Assembly Language:
By nature in Assembly there is an inclination towards to be big size of Assembly program. Also there is some way to keep remain in smaller in code size by subroutines or procedures. The article is concerned with the procedures and its various kind of implementation with example.
Procedures in Assembly
Assembly language programs intend to be large in size. Procedures or subroutines are very vital in assembly language which are identified by a name. Following this name, the body of the procedure is defined which accomplishes a well-defined job. End of the procedure is indicated by a return statement.
Syntax
proc_name:
procedure body
...
ret
The procedure is called from another function by using the CALL instruction. The CALL instruction should have the name of the called procedure as an argument as shown below:
CALL proc_name
The called procedure returns the control to the calling procedure by using the RET instruction.
Example: Write a very simple procedure named sum that adds the variables stored in the ECX and EDX register and returns the sum in the EAX register.
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov ecx,'4'
sub ecx, '0'
mov edx, '5'
sub edx, '0'
call sum ;call sum procedure
mov [res], eax
mov ecx, msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov ecx, res
mov edx, 1
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
sum:
mov eax, ecx
add eax, edx
add eax, '0'
ret
section .data
msg db "The sum is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
OUTPUT:
The sum is:
9
Stacks Data Structure
A stack is an array-like data structure in the memory in which data can be stored and removed from a location called the ‘top’ of the stack. The data desires to be stored is ‘pushed’ into the stack and data to be retrieved is ‘popped’ out from the stack in LIFO structure. So, the data stored first is retrieved last. Assembly language provides two instructions for stack operations: PUSH and POP. These instructions have syntaxes like:
PUSH operand
POP address/register
The memory space used in the stack segment is used for implementing stack. The registers SS and ESP (or SP) are used for implementing the stack. The top of the stack, which points to the last data item injected into the stack is pointed to by the SS: ESP register, where the SS register points to the opening of the stack segment and the SP (or ESP) gives the offset into the stack segment. The stack implementation has the following characteristics:
Only words or doublewords could be saved into the stack, not a byte. The stack grows in the reverse direction. For example, toward the lower memory address. The top of the stack points to the last item inserted in the stack; it points to the lower byte of the last word inserted. For storing the values of the registers in the stack before using them for some use; it can be done in following way:
; Save the AX and BX registers in the stack
PUSH AX
PUSH BX
; Use the registers for other purpose
MOV AX, VALUE1
MOV BX, VALUE2
...
MOV VALUE1, AX
MOV VALUE2, BX
; Restore the original values
POP BX
POP AX
Example
The following program displays the entire ASCII character set. The main program calls a procedure named display, which displays the ASCII character set.
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
call display
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
display:
mov ecx, 256
next:
push ecx
mov eax, 4
mov ebx, 1
mov ecx, achar
mov edx, 1
int 80h
pop ecx
mov dx, [achar]
cmp byte [achar], 0dh
inc byte [achar]
loop next
ret
section .data
achar db '0'
OUTPUT:
0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}
…
…
Recursion in Assembly
A recursive procedure is one that calls itself. There are two types of recursion:
In direct recursion, the procedure calls itself and in indirect recursion, the first procedure calls a second procedure, which in turn calls the first procedure. Recursion could be observed in numerous mathematical algorithms. For instance, suppose the case of calculating the factorial of a number. Factorial of a number is given by the equation:
Fact (n) = n * fact (n-1) for n > 0
For instance: factorial of 5 is 1 x 2 x 3 x 4 x 5 = 5 x factorial of 4 and this can be a good instance of showing a recursive procedure. Every recursive algorithm must have an ending condition. In the case of factorial algorithm, the end condition is reached when n is 0.
Example: The following program shows how factorial n is implemented in assembly language. To keep the program simple, we will calculate factorial 3.
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov bx, 3 ;for calculating factorial 3
call proc_fact
add ax, 30h
mov [fact], ax
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov edx,1 ;message length
mov ecx,fact ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
proc_fact:
cmp bl, 1
jg do_calculation
mov ax, 1
ret
do_calculation:
dec bl
call proc_fact
inc bl
mul bl ;ax = al * bl
ret
section .data
msg db 'Factorial 3 is:',0xa
len equ $ - msg
section .bss
fact resb 1
OUTPUT:
Factorial 3 is:
6
by Jesmin Akther | Jul 1, 2021 | Assembly Language
Numbers Strings and Array in Assembly:
In Assembly Language the preferable data types consisted on Numbers, Strings and Arrays respectively. The article discuss each of the with the declaration and explanation with code.
Numbers in Assembly
Numerical data is generally represented in binary system. Arithmetic instructions operate on binary data. When numbers are displayed on screen or entered from keyboard, they are in ASCII form. We have converted this input data in ASCII form to binary for arithmetic calculations and converted the result back to binary. The following code shows this:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov eax,'3'
sub eax, '0'
mov ebx, '4'
sub ebx, '0'
add eax, ebx
add eax, '0'
mov [sum], eax
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov ecx,sum
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db "The sum is:", 0xA,0xD
len equ $ - msg
segment .bss
sum resb 1
OUTPUT:
The sum is:
7
Such conversions, however, have an overhead, and assembly language programming allows processing numbers in a more efficient way, in the binary form. Decimal numbers can be represented in two forms:
- ASCII form
- BCD or Binary Coded Decimal form
ASCII Representation
In ASCII representation, decimal numbers are stored as string of ASCII characters. For example, the decimal value 1234 is stored as:
31 32 33 34H
Where, 31H is ASCII value for 1, 32H is ASCII value for 2, and so on. There are four instructions for processing numbers in ASCII representation :
- AAA − ASCII Adjust After Addition
- AAS − ASCII Adjust After Subtraction
- AAM − ASCII Adjust After Multiplication
- AAD − ASCII Adjust Before Division
These instructions do not take any operands and assume the required operand to be in the AL register. The following example uses the AAS instruction to demonstrate the concept:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
sub ah, ah
mov al, '9'
sub al, '3'
aas
or al, 30h
mov [res], ax
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov edx,1 ;message length
mov ecx,res ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'The Result is:',0xa
len equ $ - msg
section .bss
res resb 1
OUTPUT
The Result is:
6
BCD Representation
There are two types of BCD representation:
- Unpacked BCD representation
- Packed BCD representation
In unpacked BCD representation, each byte stores the binary equivalent of a decimal digit. For example, the number 1234 is stored as:
01 02 03 04H
There are two instructions for processing these numbers:
- AAM − ASCII Adjust After Multiplication
- AAD − ASCII Adjust Before Division
The four ASCII adjust instructions, AAA, AAS, AAM, and AAD, can also be used with unpacked BCD representation. In packed BCD representation, each digit is stored using four bits. Two decimal digits are packed into a byte. For example, the number 1234 is stored as:
12 34H
There are two instructions for processing these numbers:
- DAA − Decimal Adjust After Addition
- DAS − decimal Adjust After Subtraction
There is no support for multiplication and division in packed BCD representation.
Example
The following program adds up two 5-digit decimal numbers and displays the sum. It uses the above concepts:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov esi, 4 ;pointing to the rightmost digit
mov ecx, 5 ;num of digits
clc
add_loop:
mov al, [num1 + esi]
adc al, [num2 + esi]
aaa
pushf
or al, 30h
popf
mov [sum + esi], al
dec esi
loop add_loop
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov edx,5 ;message length
mov ecx,sum ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'The Sum is:',0xa
len equ $ - msg
num1 db '12345'
num2 db '23456'
sum db ' '
OUTPUT:
The Sum is:
35801
Strings in Assembly
The variable length strings can have as many characters as required. Generally, we specify the length of the string by either of the two ways:
- Explicitly storing string length
- Using a sentinel character
We can store the string length explicitly by using the $ location counter symbol that represents the current value of the location counter. In the following example:
msg db ‘Hello, world!’,0xa ;our dear string
len equ $ – msg ;length of our dear string
$ points to the byte after the last character of the string variable msg. Therefore, $-msg gives the length of the string. We can also write:
msg db ‘Hello, world!’,0xa ;our dear string
len equ 13 ;length of our dear string
Alternatively, you can store strings with a trailing sentinel character to delimit a string instead of storing the string length explicitly. For example:
message DB ‘I am loving it!’, 0
String Instructions
Each string instruction may require a source operand, a destination operand or both. For 32-bit segments, string instructions use ESI and EDI registers to point to the source and destination operands, respectively. For 16-bit segments, But, the SI and the DI registers are used to point to the source and destination, respectively. There are five basic instructions for processing strings. They are:
- MOVS: This instruction moves 1 Byte, Word or Doubleword of data from memory location to another.
- LODS: This instruction loads from memory. If the operand is of one byte, it is loaded into the AL register, if the operand is one word, it is loaded into the AX register and a doubleword is loaded into the EAX register.
- STOS: This instruction stores data from register (AL, AX, or EAX) to memory.
- CMPS: This instruction compares two data items in memory. Data could be of a byte size, word or doubleword.
- SCAS: This instruction compares the contents of a register (AL, AX or EAX) with the contents of an item in memory.
Each of the above instruction has a byte, word, and doubleword version, and string instructions can be repeated by using a repetition prefix.
These instructions use the ES: DI and DS:SI pair of registers, where DI and SI registers contain valid offset addresses that refers to bytes stored in memory. SI is normally associated with DS (data segment) and DI is always associated with ES (extra segment).
The DS:SI (or ESI) and ES:DI (or EDI) registers point to the source and destination operands, respectively. The source operand is assumed to be at DS:SI (or ESI) and the destination operand at ES:DI (or EDI) in memory.
For 16-bit addresses, the SI and DI registers are used, and for 32-bit addresses, the ESI and EDI registers are used.
Repetition Prefixes
The REP prefix, when set before a string instruction, for example – REP MOVSB, causes repetition of the instruction based on a counter placed at the CX register. REP executes the instruction, decreases CX by 1, and checks whether CX is zero. It repeats the instruction processing until CX is zero.
- The Direction Flag (DF) determines the direction of the operation.
- Use CLD (Clear Direction Flag, DF = 0) to make the operation left to right.
- Use STD (Set Direction Flag, DF = 1) to make the operation right to left.
The REP prefix also has the following variations:
REP: It is the unconditional repeat. It repeats the operation until CX is zero.
REPE or REPZ: It is conditional repeat. It repeats the operation while the zero flag indicates equal/zero. It stops when the ZF indicates not equal/zero or when CX is zero.
REPNE or REPNZ: It is also conditional repeat. It repeats the operation while the zero flag indicates not equal/zero. It stops when the ZF indicates equal/zero or when CX is decremented to zero.
Arrays in Assembly
The data definition directives to the assembler are used for allocating storage for variables. The variable could be initialized with some specific value. The initialized value could be specified in hexadecimal, decimal or binary form. For example, we can define a word variable ‘months’ in either of the following way:
MONTHS DW 12
MONTHS DW 0CH
MONTHS DW 0110B
The data definition directives can also be used for defining a one-dimensional array. Let us define a one-dimensional array of numbers.
NUMBERS DW 34, 45, 56, 67, 75, 89
The above definition declares an array of six words each initialized with the numbers 34, 45, 56, 67, 75, 89. This allocates 2×6 = 12 bytes of consecutive memory space. The symbolic address of the first number will be NUMBERS and that of the second number will be NUMBERS + 2 and so on. Let us take up another example. You can define an array named inventory of size 8, and initialize all the values with zero, as :
INVENTORY DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
Which can be abbreviated as:
INVENTORY DW 0, 0 , 0 , 0 , 0 , 0 , 0 , 0
The TIMES directive can also be used for multiple initializations to the same value. Using TIMES, the INVENTORY array can be defined as:
INVENTORY TIMES 8 DW 0
Example
The following example demonstrates the above concepts by defining a 3-element array x, which stores three values: 2, 3 and 4. It adds the values in the array and displays the sum 9:
section .text
global _start ;must be declared for linker (ld)
_start:
mov eax,3 ;number bytes to be summed
mov ebx,0 ;EBX will store the sum
mov ecx, x ;ECX will point to the current element to be summed
top: add ebx, [ecx]
add ecx,1 ;move pointer to next element
dec eax ;decrement counter
jnz top ;if counter not 0, then loop again
done:
add ebx, '0'
mov [sum], ebx ;done, store result in "sum"
display:
mov edx,1 ;message length
mov ecx, sum ;message to write
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
global x
x:
db 2
db 4
db 3
sum:
db 0
OUTPUT:
9
by Jesmin Akther | Jul 1, 2021 | Assembly Language
Conditions and Loop with code in Assembly
Conditional run in assembly language is accompanied by many looping and branching instructions. Firstly we discuss here with conditions in assembly then the loops in Assembly.
Conditions in Assembly
Conditional execution in assembly language is accomplished by several looping and branching instructions. These instructions can change the flow of control in a program. Conditional execution is observed in two scenarios:
- Unconditional jump
This is performed by the JMP instruction. Conditional execution often involves a transfer of control to the address of an instruction that does not follow the currently executing instruction. Transfer of control may be forward, to execute a new set of instructions or backward, to re-execute the same steps.
2. Conditional jump
This is performed by a set of jump instructions j<condition> depending upon the condition. The conditional instructions transfer the control by breaking the sequential flow and they do it by changing the offset value in IP.
Unconditional Jump
This is performed by the JMP instruction. Conditional execution often involves a transfer of control to the address of an instruction that does not follow the currently executing instruction. Transfer of control may be forward, to execute a new set of instructions or backward, to re-execute the same steps.
Syntax
The JMP instruction provides a label name where the flow of control is transferred immediately. The syntax of the JMP instruction is:
Example
MOV AX, 00 ; Initializing AX to 0
MOV BX, 00 ; Initializing BX to 0
MOV CX, 01 ; Initializing CX to 1
L20:
ADD AX, 01 ; Increment AX
ADD BX, AX ; Add AX to BX
SHL CX, 1 ; shift left CX, this in turn doubles the CX value
JMP L20 ; repeats the statements
Conditional Jump
If some specified condition is satisfied in conditional jump, the control flow is transferred to a target instruction. There are numerous conditional jump instructions depending upon the condition and data. The syntax for the J<condition> set of instructions.
Example:
CMP AL, BL
JE EQUAL
CMP AL, BH
JE EQUAL
CMP AL, CL
JE EQUAL
NON_EQUAL: ...
EQUAL: ...
Example
The following program displays the largest of three variables. The variables are double-digit variables. The three variables num1, num2 and num3 have values 47, 22 and 31, respectively:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov ecx, [num1]
cmp ecx, [num2]
jg check_third_num
mov ecx, [num2]
check_third_num:
cmp ecx, [num3]
jg _exit
mov ecx, [num3]
_exit:
mov [largest], ecx
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov ecx,largest
mov edx, 2
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1
int 80h
section .data
msg db "The largest digit is: ", 0xA,0xD
len equ $- msg
num1 dd '47'
num2 dd '22'
num3 dd '31'
segment .bss
largest resb 2
OUTPUT:
The largest digit is:
47
Loop uses in Assembly
The JMP instruction can be used for implementing loops. For example, the following code snippet can be used for executing the loop-body 10 times.
MOV CL, 10
L1:
<LOOP-BODY>
DEC CL
JNZ L1
The processor instruction set, however, includes a group of loop instructions for implementing iteration. The basic LOOP instruction has the following syntax :
LOOP label
Where, label is the target label that identifies the target instruction as in the jump instructions. The LOOP instruction assumes that the ECX register contains the loop count. When the loop instruction is executed, the ECX register is decremented and the control jumps to the target label, until the ECX register value, i.e., the counter reaches the value zero. The above code snippet could be written as:
mov ECX,10
l1:
<loop body>
loop l1
Example
The following program prints the number 1 to 9 on the screen:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov ecx,10
mov eax, '1'
l1:
mov [num], eax
mov eax, 4
mov ebx, 1
push ecx
mov ecx, num
mov edx, 1
int 0x80
mov eax, [num]
sub eax, '0'
inc eax
add eax, '0'
pop ecx
loop l1
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .bss
num resb 1
OUTPUT
123456789:
by Jesmin Akther | Jul 1, 2021 | Assembly Language
Assembly Logical Instructions
The processor instruction set offers the instructions Boolean logic namely AND, OR, XOR, TEST, and NOT. This is tests, sets, and clears the bits according to the need of the program.
The format for these instructions:
Instruction
|
Format
|
AND |
AND operand1, operand2 |
OR |
OR operand1, operand2 |
XOR |
XOR operand1, operand2 |
TEST |
TEST operand1, operand2 |
The first operand in all the cases could be either in register or in memory. The second operand could be either in register/memory or an immediate or constant value. Although, memory-to-memory operations are not possible. These instructions compare or match bits of the operands and set the CF, OF, PF, SF and ZF flags.
The AND Instruction
The AND instruction is used for logical expressions bitwise AND operation. The operation returns 1, if the matching bits from both the operands are 1, otherwise it returns 0. For example:
Operand1: 0101
Operand2: 0011
——————————————–
After AND Operation -> Operand1:0001
The AND operation can be used for clearing one or more bits. For example, say the BL register contains 0011 1010. If you need to clear the high-order bits to zero, you AND it with 0FH.
AND BL, 0FH ; This sets BL to 0000 1010
Example
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov ax, 8h ;getting 8 in the ax
and ax, 1 ;and ax with 1
jz evnn
mov eax, 4 ;system call number (sys_write)
mov ebx, 1 ;file descriptor (stdout)
mov ecx, odd_msg ;message to write
mov edx, len2 ;length of message
int 0x80 ;call kernel
jmp outprog
evnn:
mov ah, 09h
mov eax, 4 ;system call number (sys_write)
mov ebx, 1 ;file descriptor (stdout)
mov ecx, even_msg ;message to write
mov edx, len1 ;length of message
int 0x80 ;call kernel
outprog:
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
even_msg db 'Even Number!' ;message showing even number
len1 equ $ - even_msg
odd_msg db 'Odd Number!' ;message showing odd number
len2 equ $ - odd_msg
OUTPUT
Even Number!
Change the value in the ax register with an odd digit, such as:
mov ax, 9h ; getting 9 in the ax
The program would display:
Odd Number!
Similarly to clear the entire register you can AND it with 00H.
The OR Instruction
The OR instruction is used for performing bitwise OR operation. The operations returns 1, if the matching bits from either or both operands are one. It returns 0, if both the bits are zero.
For example,
Operand1: 0101
Operand2: 0011
——————————————–
After OR -> Operand1: 0111
The OR operation can be used for setting one or more bits. For example, let us assume the AL register contains 0011 1010, you need to set the four low-order bits, you can OR it with a value 0000 1111, i.e., FH.
OR BL, 0FH ; This sets BL to 0011 1111
Example
The following example demonstrates the OR instruction. Let us store the value 5 and 3 in the AL and the BL registers, respectively, then the instruction,
OR AL, BL
should store 7 in the AL register:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov al, 5 ;getting 5 in the al
mov bl, 3 ;getting 3 in the bl
or al, bl ;or al and bl registers, result should be 7
add al, byte '0' ;converting decimal to ascii
mov [result], al
mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, 1
int 0x80
outprog:
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .bss
result resb 1
OUTPUT
7
The XOR Instruction
The XOR instruction implements from the bitwise XOR operation. The XOR operation sets the resultant bit to 1, if and only if the bits from the operands are different. If the bits from the operands are same (both 0 or both 1), the resultant bit is cleared to 0.
For example,
Operand1: 0101
Operand2: 0011
—————————————
After XOR -> Operand1: 0110
XORing an operand with itself changes the operand to 0. This is used to clear a register.
XOR EAX, EAX
The TEST Instruction
The TEST instruction works similar with the AND operation, but unlike AND instruction, it does not change the first operand. So, if we need to check whether a number in a register is even or odd, we can also do this using the TEST instruction without changing the original number.
TEST AL, 01H
JZ EVEN_NUMBER
The NOT Instruction
The NOT instruction is implementing the bitwise NOT operation. The operation reverses the bits in an operand. The operand could be either in a register or in the memory.
For example,
Operand1: 0101 0011
After NOT -> Operand1: 1010 1100
by Jesmin Akther | Jul 1, 2021 | Assembly Language
Arithmetic Instructions in Assembly Language
The INC Instruction
The INC instruction is used for incrementing an operand by one. It works on a single operand that can be either in a register or in memory.
Syntax
The INC instruction has the following syntax
INC destination
The operand destination could be an 8-bit, 16-bit or 32-bit operand. Example
INC EBX ; Increments 32-bit register
INC DL ; Increments 8-bit register
INC [count] ; Increments the count variable
The DEC Instruction
The DEC instruction is used for decrementing an operand by one. It works on a single operand that can be either in a register or in memory.
Syntex:
DEC destination
ADD and SUB Instructions
The ADD and SUB instructions are used for performing simple addition/subtraction of binary data in byte, word and doubleword size. For example, adding or subtracting 8-bit, 16-bit or 32-bit operands, respectively.
Syntex:
ADD/SUB destination, source
The ADD/SUB instruction can take place between: Register, Memory and constant data But, like other instructions, memory-to-memory operations are not possible using ADD/SUB instructions. An ADD or SUB operation sets or clears the overflow and carry flags.
Example
The following example will ask two digits from the user, store the digits in the EAX and EBX register, respectively, add the values, store the result in a memory location ‘res’ and finally display the result.
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
segment .data
msg1 db "Enter a digit ", 0xA,0xD
len1 equ $- msg1
msg2 db "Please enter a second digit", 0xA,0xD
len2 equ $- msg2
msg3 db "The sum is: "
len3 equ $- msg3
segment .bss
num1 resb 2
num2 resb 2
res resb 1
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num1
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num2
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0x80
; moving the first number to eax register and second number to ebx
; and subtracting ascii '0' to convert it into a decimal number
mov eax, [num1]
sub eax, '0'
mov ebx, [num2]
sub ebx, '0'
; add eax and ebx
add eax, ebx
; add '0' to to convert the sum from decimal to ASCII
add eax, '0'
; storing the sum in memory location res
mov [res], eax
; print the sum
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, res
mov edx, 1
int 0x80
exit:
mov eax, SYS_EXIT
xor ebx, ebx
int 0x80
OUTPUT:
Enter a digit:
3
Please enter a second digit:
4
The sum is:
7
The MUL or IMUL Instruction
T
here are two instructions for multiplying binary data. The MUL (Multiply) instruction handles unsigned data and the IMUL (Integer Multiply) handles signed data. Both instructions affect the Carry and Overflow flag.
Syntax
MUL/IMUL multiplier
Example The following example multiplies 3 with 2, and displays the result −
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov al,'3'
sub al, '0'
mov bl, '2'
sub bl, '0'
mul bl
add al, '0'
mov [res], al
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov ecx,res
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db "The result is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
OUTPUT
The result is:
6
The DIV/IDIV Instructions
The division operation generates two elements, a quotient and a remainder. In case of multiplication, overflow does not occur because double-length registers are used to keep the product. However, in case of division, overflow may occur. The processor generates an interrupt if overflow occurs. The DIV (Divide) instruction is used for unsigned data and the IDIV (Integer Divide) is used for signed data.
Syntax: DIV/IDIV divisor
The dividend is in an accumulator. Both the instructions can work with 8-bit, 16-bit or 32-bit operands. The operation affects all six status flags. Following section explains three cases of division with different operand sizes.
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov ax,'8'
sub ax, '0'
mov bl, '2'
sub bl, '0'
div bl
add ax, '0'
mov [res], ax
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov ecx,res
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db "The result is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
OUTPUT:
4
by Jesmin Akther | Jun 28, 2021 | Microprocessor & Microcontroller
Programming with 8085
Let’s see some simple examples to demonstrate the use of some important instructions of 8085.
Problem: Write an assembly language program to add two 8 bit numbers stored at address 2050 and address 2051 in 8085 microprocessor. The starting address of the program is taken as 2000.
Example –

8085 instruction example
Algorithm :
- Load the first number from memory location 1050 to accumualator.
- Move the content of accumulator to register H.
- Load the second number from memory location 1051 to accumalator.
- Then add the content of register H and accumulator using “ADD” instruction and storing result at 2050
- The carry generated is recovered using “ADC” command and is stored at memory location 2051
Program:
Memory Address |
Mnemonics |
Comment |
2000 |
LDA 2050 |
A<-[2050] |
2003 |
MOV H, A |
H<-A |
2004 |
LDA 2051 |
A<-[2051] |
2007 |
ADD H |
A<-A+H |
2008 |
MOV L, A |
L←A |
2009 |
MVI A 00 |
A←00 |
200B |
ADC A |
A←A+A+carry |
200C |
MOV H, A |
H←A |
200D |
SHLD 3050 |
H→3051, L→3050 |
2010 |
HLT |
|
Explanation:
- LDA 1050 is used to moves the contents of 1050 memory location to the accumulator.
- MOV H, A is used to copies contents of Accumulator to register H to A
- LDA 1051 is used to moves the contents of 1051 memory location to the accumulator.
- ADD H is used to adds contents of A (Accumulator) and H register (F6). The result is stored in A itself. For all arithmetic instructions A is by default an operand and A stores the result as well
- MOV L, A is used to copies contents of A (24) to L
- MVI A 00 is used to moves immediate data (i.e., 00) to A
- ADC A adds contents of A(00), contents of register specified (for example: A) and carry (1). As ADC is also an arithmetic operation, A is by default an operand and A stores the result as well
- MOV H, A is used to copies contents of A (01) to H
- SHLD 2050 is used to moves the contents of L register (24) in 2050 memory location and contents of H register (01) in 2051 memory location
- HLT is used to stops executing the program and halts any further execution
The memory addresses given in the program are for a particular microprocessor kit. These addresses can be changed to suit the microprocessor kit available in your system.
Store 8-bit data in memory Program, Store 8-bit data in memory using direct addressing
MVI A, 49H : is used to Store 49H in the accumulator"
STA 2501H : "Used to Copy accumulator contents at address 2501H"
HLT : "Stop"
Store 8-bit data in memory using indirect addressing. Add two 8-bit numbers
LXI H : "is used to Load H-L pair with 2501H"
MVI M : "is used to Store 49H in memory location pointed by H-L register pair (2501H)"
HLT : "Stop"
Example
(2501 H) = 99H
(2502 H) = 39H
Result (2503 H) = 99H + 39H = D2H
Since,
1 0 0 1 1 0 0 1 (99H)
+ 0 0 1 1 1 0 0 1 (39H)
1 1 0 1 0 0 1 0 (D2H)
Program
LXI H, 2501H : "Get address of first number in H-L pair. Now H-L points to 2501H"
MOV A, M : "is used to Get first operand in accumulator"
INX H : "is used to Increment content of H-L pair. Now, H-L points 2502H"
ADD M : "is used to Add first and second operand"
INX H : "is used to H-L points 4002H"
MOV M, A : "is used to Store result at 2503H"
HLT : "Stop"
Subtract two 8-bit numbers
Example
(2501 H) = 49H
(2502 H) = 32H
Result (2503 H) = 49H - 32H = 17H
Program
LXI H, 2501H : "is used to Get address of first number in H-L pair. Now H-L points to 2501H"
MOV A, M : "is used to Get first operand in accumulator"
INX H : "is used to Increment content of H-L pair. Now, H-L points 2502H"
SUB M : "is used to Subtract first to second operand"
INX H : "is used to H-L points 4002H"
MOV M, A : "is used to Store result at 2503H"
HLT : "Stop"
Add two 16-bits numbers
Add the 16-bit number in memory locations 2501H and 2502H to the 16-bit number in memory locations 2503H and 2504H. The most significant eight bits of the two numbers to be added are in memory locations 2502H and 4004H. Store the result in memory locations 2505H and 2506H with the most significant byte in memory location 2506H.
Example
(2501H) = 15H
(2502H) = 1CH
(2503H) = B7H
(2504H) = 5AH
Result = 1C15 + 5AB7H = 76CCH
(2505H) = CCH
(2506H) = 76H
Program
Add two 16-bits number with ADD and ADC instruction
LHLD 2501H : "is used to Get 1st 16-bit number in H-L pair"
XCHG : "is used to Save 1st 16-bit number in DE"
LHLD 2503H : "is used to Get 2nd 16-bit number in H-L pair"
MOV A, E : "is used to Get lower byte of the 1st number"
ADD L : "is used to Add lower byte of the 2nd number"
MOV L, A : "is used to Store result in L-register"
MOV A, D : "is used to Get higher byte of the 1st number"
ADC H : "is used to Add higher byte of the 2nd number with CARRY"
MOV H, A : "is used to Store result in H-register"
SHLD 4004H : "is used to Store 16-bit result in memory locations 2505H and 2506H"
HLT : "Stop"
Add two 16-bits numbers with DAD instruction
LHLD 2501H : "is used to Get 1st 16-bit number"
XCHG : "is used to Save 1st 16-bit number in DE"
LHLD 2503H : "is used to Get 2nd 16-bit number in H-L"
DAD D : "is used to Add DE and HL"
SHLD 2505H : "is used to Store 16-bit result in memory locations 2505H and 2506H".
HLT : "Stop"
Subtract two 16-bit numbers
Example
(2500H) = 19H
(2501H) = 6AH
(2504H) = 15H (2503H) = 5CH
Result = 6A19H ? 5C15H = OE04H
(2504H) = 04H
(2505H) = OEH
Program
LHLD 2500H : is used to Get first 16-bit number in HL
XCHG : is used to Save first 16-bit number in DE"
LHLD 2502H : is used to Get second 16-bit number in HL"
MOV A, E : is used to Get lower byte of the first number"
SUB L : is used to Subtract lower byte of the second number"
MOV L, A : is used to Store the result in L register"
MOV A, D : is used to Get higher byte of the first number"
SBB H : is used to Subtract higher byte of second number with borrow"
MOV H, A : is used to Store l6-bit result in memory locations 2504H and 2505H"
SHLD 2504H : is used to Store l6-bit result in memory locations 2504H and 2505H"
HLT : is used to Terminate program execution"
Add contents of two memory locations
Example
(2500H) = 7FH
(2501H) = 89H
Result = 7FH + 89H = lO8H
(2502H) = 08H
(2503H) = 01H
Program
LXI H, 2500H : "is used to HL Points 2500H"
MOV A, M : "Get first operand"
INX H : "HL Points 2501H"
ADD M : "this statement Add second operand"
INX H : "is used to HL Points 2502H"
MOV M, A : "this statement Store the lower byte of result at 2502H"
MVIA, 00 : "Initialize higher byte result with 00H"
ADC A : "this statement Add carry in the high byte result"
INX H : "is used to HL Points 2503H"
MOV M, A : "this statement Store the higher byte of result at 2503H"
HLT : "Terminate program execution"
Finding 1’s complement of a number
To obtain one’s complement of a number its 0 bits are replaced by 1 and 1 by 0.
Example 1
(2501H) = 96 H = 1001 0110
(9) (6)
One's complement = 0110 1001 = 69 H
Result = (2502H) = 69H
Example 2
(2501H) = E4H
Result = (2502H) = 1BH
Program
The number is placed in the memory location 2501 H.
The result is stored in the memory location 2502 H.
LDA 2501H : "this statement Get the number IN accumulator"
CMA : "this statement take its complement"
STA 2502H : "this statement Store result in 2502H"
HLT : "Stop"
Finding 2’s complement of a number
2’s complement of a number is obtained by adding 1 to the 1’s complement of the number.
Example
To find the two’s complement of 96.
96 = 1001 0110
1's complement = 0110 1001 = 69
+ 0000 0001
2's complement = 0110 1010 = 6A
Program
The number is placed in the memory location 2501 H.
The result is to be stored in the memory location 2502 H.
LDA 2501 H : "this statement Get data in accumulator"
CMA : "Take its 1's complement"
ADI, 01 H : "this statement Add one in the number"
STA 2502 H : "Store the result in 2502 H"
HLT : "Stop"
Count number of 1’s in a number
Example
2501 H = 04
2502 H = 34 H
2503 H = A9H
2504 H = 78H
2505 H = 56H
Result = 2503 H = A9H
Program
Count number of 1’s of the content of the register D and store the count in the register B.
MVI B, 00H
MVI C, 08H
MOV A, D
BACK: RAR
JNC SKIP
INR B
SKIP: DCR C
JNZ BACK
HLT
Find larger of two numbers
Example
2501H = 98 H
2502H = 87H
Result = 98H (2503H)
Program
The first number 98H is placed in the memory location 2501 H.
The second number 87H is placed in the memory location 2502H.
The result is stored in the memory location 2503 H.
LXI H, 2501H : "this statement Address of first number in H-L pair"
MOV A, M : "1stt number in accumulator"
INX H : "this statement Address of 2nd number in H-L pair"
CMP M : "compare 2nd number with 1st number"
JNC AHEAD : "No, larger is in accumulator. Go to AHEAD"
MOV A, M : "this statement Yes, get 2nd number in the accumulator"
STA 2503 H : "Store larger number in 2503H"
HLT : "Stop"
Find smaller of two numbers
Example
2501H = 84 H
2502H = 99 H
Result = 84 H(2503H)
Program
The first number 84H is placed in the memory location 2501 H.
The second number 99H is placed in the memory location 2502H.
The result is stored in the memory location 2503 H.
LXI H, 2501H : "Address of first number in H-L pair"
MOV A, M : "this statement 1stt number in accumulator"
INX H : "Address of 2nd number in H-L pair"
CMP M : "compare 2nd number with 1st number"
JC AHEAD : "this statement Yes, smaller number is in accumulator. Go to AHEAD"
MOV A, M : "No, get 2nd number in the accumulator"
STA 2503 H : "this statement Store smaller number in 2503H"
HLT : "Stop"
Calculate the sum of series of even numbers
Example
2500 H = 4H
2501 H = 20H
2502 H = 15H
2503 H = 13H
2504 H = 22H
Result = 2505 H = 20+22= 42H
Program
The numbers are placed in the memory locations 2501 to 2504H.
The sum is to be stored in the memory location 2450H.
As there are 4 numbers in the series, count = 04
The initial value of the sum is made 00. The even number of the series are taken one by one and added to the sum.
LDA 2500H
MOV C, A : "Initialize counter"
MVI B, 00H : "sum = 0"
LXI H, 2501H :this statement Initialize pointer"
BACK: MOV A, M : "Get the number"
ANI 01H : "Mask Bit l to Bit7"
JNZ SKIP : this statement Don't add if number is ODD"
MOV A, B : this statement Get the sum"
ADD M : "SUM = SUM + data"
MOV B, A : "Store result in B register"
SKIP: INX H : "increment pointer"
DCR C : "Decrement counter"
JNZ BACK : "if counter 0 repeat"
STA 2505H : "store sum"
HLT : "Stop"
Calculate the sum of series of odd numbers
Example
2500 H = 4H
2501 H = 9AH
2502 H = 52H
2503 H = 89H
2504 H = 3FH
Result = 2505 H = 89H + 3FH= C8H
Program
The numbers are placed in the memory locations 2501 to 2504H.
The sum is to be stored in the memory location 2450H.
As there are 4 numbers in the series, count = 04
The initial value of the sum is made 00. The odd number of the series are taken one by one and added to the sum.
LDA 2500H
MOV C, A : "Initialize counter"
LXI H, 2501H : "Initialize pointer"
MVI E, 00 : "Sum low = 0"
MOV D, E : "Sum high = 0"
BACK: MOV A, M : "Get the number"
ANI 01H : "Mask Bit 1 to Bit-7"
JZ SKIP : this statement Don't add if number is even"
MOV A, E : "Get the lower byte of sum"
ADD M : "Sum = sum + data"
MOV E, A : this statement Store result in E register"
JNC SKIP
INR D : "Add carry to MSB of SUM"
SKIP: INX H : "Increment pointer"
Find the square of given number
Program
Find the square of 07 (decimal) using lookup table techniques.
The number 07 D is in the memory.
The result is to be stored in the memory location 2501H.
The table for square is stored from 2600 to 2609 H.
LDA 2500H : "Get data in accumulator"
MOV L, A : this statement Get data in register L"
MVI H, 26H : "Get 26 in register H"
MOV A, M : this statement Square of data in accumulator"
STA 2501 H : "Store Square in 2501 H"
HLT : "Stop"
Separate even numbers from given numbers
Program
A program to separate even numbers from the given list of 50 numbers and store them in other location starting from 2600H.
Starting location of 50 number list is 2500H.
LXI H, 2500H : "Initialize memory pointer 1"
LXI D, 2600H : "Initialize memory pointer2"
MVI C, 32H : "Initialize counter"
BACK:MOV A, M : this statement Get the number"
ANI 01H : "Check for even number"
JNZ SKIP : "If ODD, don't store"
MOV A, M : "Get the number"
STAX D : this statement Store the number in result list"
INX D : "Increment pointer 2"
SKIP: INX H : "Increment pointer 1"
DCR C : "Decrement counter"
JNZ BACK : "If not zero, repeat"
HLT : "Stop"
by Jesmin Akther | Jun 28, 2021 | Microprocessor & Microcontroller
Microprocessor Architecture
The microprocessor is the CPU (Central Processing Unit) of a computer. It is the heart of the computer. Here, we will describe Intel 8085 as it is one of the most popular 8-bit microprocessor. For instance, Intel 8085 is an 8-bit, NMOS microprocessor designed by Intel in 1977. 8085 configuration are mention below:
- It is a 40 pin I.C. package fabricated on a single LSI chip.
- The Intel 8085 uses a single +5Vd.c. supply for its operation.
- Intel 8085?s clock speed is about 3 MHz; the clock cycle is of 320ns.
- 8 bit data bus.
Address bus is of 16-bit, which can address up to 64KB
- 16-bit stack pointer
16 bit PC (Program Counter)
Six 8-bit registers are arranged in pairs :BC, DE, HL
Intel 8085 is used in mobile phones, microwave ovens, washing machines etc.
The below diagram shows the block diagram of Intel 8085:

Microprocessor Architecture from the bock diagram discuss below:
ALU: The Arithmetic and Logic Unit which is used to performs the arithmetic and logical operations:
- Addition
- Subtraction
- Logical AND
- Logical OR
- Logical EXCLUSIVE OR
- Complement (Logical NOT)
- Increment (add 1)
- Decrement (subtract 1)
- Left shift,
- Rotate left,
- Rotate right Clear, etc.
Timing and Control Unit
The timing and control unit is the section of the CPU. The unit is necessary for the execution of instructions.
- It is used to control data flow between CPU and peripherals with including memory.
- It is used to provide status, control and timing signals which are required for the operation of memory and I/O devices.
- It is used to control the entire operations of the microprocessor and peripherals connected to it.
- Thus control unit acts as the brain of the computer system.
Registers
Registers are used for temporary storage and manipulation of data and instructions by the microprocessor. Data remain in the registers till they are sent to the I/O devices or memory.
Intel 8085 microprocessor has the following registers:
- One 8-bit accumulator (ACC) i.e. register A
- Six general purpose registers of 8-bit, these are B,C, D, E, H and L
One 16-bit stack pointer, SP
- One 16-bit Program Counter, PC
- Instruction register
- Temporary register
In addition to the above mentioned registers the 8085 microprocessor contains a set of five flip-flops which serve as flags (or status flags). A flag is a flip-flop which indicates some conditions which arises after the execution of an arithmetic or logical instruction.
Accumulator (ACC):
The accumulator is an 8-bit register associated with the ALU. The register ‘A’ is an accumulator in the 8085. It is used to hold one of the operands of an arithmetic and logical operation.
The final result of an arithmetic or logical operation is also placed in the accumulator.
General-Purpose Registers: The 8085 microprocessor contains six 8-bit general purpose registers. They are: B, D, C, E, H and L register.
To hold data of 16-bit a combination of two 8-bit registers can be employed.
The combination of two 8-bit registers is called register pair. The valid register pairs in the 8085 are: D-E, B-C and H-L. The H-L pair is used to act as a memory pointer.
Program Counter (PC):
It is a 16-bit special purpose register. It is used to hold the address of memory of the next instruction to be executed. It keeps the track of the instruction in a program while they are being executed.
The microprocessor increments the content of the next program counter during the execution of an instruction so that at the end of the execution of an instruction it points to the next instructions address in the program.
Stack Pointer (SP):
It is a 16-bit special function register used as memory pointer. A stack is nothing but a portion of RAM. In the stack, the contents of only those registers are saved, which are needed in the later part of the program. The stack pointer (SP) controls the addressing of the stack. The Stack Pointer contains the address of the top element of data stored in the stack.
Instruction Register:
The instruction register holds the opcode (operation code or instruction code) of the instruction which is being decoded and executed.
Temporary Register:
It is an 8-bit register associated with the ALU. It holds data during an arithmetic/logical operation. It is used by the microprocessor. It is not accessible to programmer.
Flags:
The Intel 8085 microprocessor contains five flip-flops to serve as a status flags. The flip-flops are reset or set according to the conditions which arise during an arithmetic or logical operation.
The five status flags of Intel 8085 are:
- Carry Flag (CS)
- Parity Flag (P)
- Auxiliary Carry Flag (AC)
- Zero Flag(Z)
- Sign Flag(S)
If a flip-flop for a particular flag is set, then it indicates 1. When it is reset, it indicates 0.
Data and Address Bus
The Intel 8085 is an 8-bit microprocessor. Its data bus is 8-bit wide and therefore, 8 bits of data can be transmitted in parallel from or to the microprocessor.
- The Intel 8085 requires an address bus of 16-bit wide as the memory addresses are of 16-bits.
- The 8 most significant bits of the address are transmitted by the address bus, A-bus (pins A8 ? A15).
- The 8 least significant bits of the address are transmitted by data/address bus, AD-bus (pins AD0 ? AD7).
Pin Configuration

Fig: Pin diagram of Intel 8085 microprocessor
The descriptions of various pins are as follows:
Address Bus and Data Bus
A8 to A15 (Output):
These are address bus and are used for the most significant bits of the memory address or 8-bits of I/O address.
AD0 to AD7 (Input/output):
These are time multiplexed address/data bus i.e. they serve dual purpose. They are used for the least significant 8 bits of the memory address or I/O address during the first cycle. Again they are used for data during 2nd and 3rd clock cycles.
Control and Status Signals
ALE (Output):
ALE stands for Address Latch Enable signal. ALE goes high during first clock cycle of a machine cycle and enables the lower 8-bits of the address to be latched either into the memory or external latch.
IO/M (Output):
It is a status signal which distinguishes whether the address is for memory or I/O device.
S0, S1 (Output):
These are status signals sent by the microprocessors to distinguish the various types of operation given in table below:
Status codes for Intel 8085
S1 S0 Operations
0 0 HALT
0 1 WRITE
1 0 READ
1 1 FETCH
RD (Output):
RD is a signal to control READ operation. When it goes low, the selected I/O device or memory is read.
WR (Output):
WR is a signal to control WRITE operation. When it goes low, the data bus’ data is written into the selected memory or I/O location.
READY (Input):
It is used by the microprocessor to sense whether a peripheral is ready to transfer a data or not. If READY is high, the peripheral is ready. If it is low the micro processor waits till it goes high.
Interrupts and Externally Initiated Signals
HOLD (INPUT):
HOLD indicates that another device is requesting for the use of the address and data bus.
HLDA (OUTPUT):
HLDA is a signal for HOLD acknowledgement which indicates that the HOLD request has been received. After the removal of this request the HLDA goes low.
INTR (Input):
INTR is an Interrupt Request Signal. Among interrupts it has the lowest priority. The INTR is enabled or disabled by software.
INTA (Output):
INTA is an interrupt acknowledgement sent by the microprocessor after INTR is received.
RST 5.5, 6.5, 7.5 and TRAP (Inputs): These all are interrupts.
When any interrupt is recognized the next instruction is executed from a fixed location in the memory as given below:
Line Location from which next instruction is picked up
TRAP 0024
RST 5.5 002C
RST 6.5 0034
RST 7.5 003C
RST 7.5, RST 6.5 and RST 5.5 are the restart interrupts which cause an internal restart to be automatically inserted.
The TRAP has the highest priority among interrupts. The order of priority of interrupts is as follows:
TRAP (Highest priority)
RST 7.5
RST 6.5
RST 5.5
INTR (Lowest priority).
Reset Signals
RESET IN (Input):
It resets the program counter (PC) to 0. It also resets interrupt enable and HLDA flip-flops. The CPU is held in reset condition till RESET is not applied.
RESET OUT (Output):
RESET OUT indicates that the CPU is being reset.
Clock Signals
X1, X2 (Input):
X1 and X2 are terminals to be connected to an external crystal oscillator which drives an internal circuitry of the microprocessor. It is used to produce a suitable clock for the operation of microprocessor.
CLK (Output):
CLK is a clock output for user, which can be used for other digital ICs. Its frequency is same at which processor operates.
Serial I/O Signals
SID (Input):
SID is data line for serial input. The data on this line is loaded into the seventh bit of the accumulator when RIM instruction is executed.
SOD (Output):
SOD is a data line for serial output. The seventh bit of the accumulator is output on SOD line when SIM instruction is executed.
Power Supply
Vcc : +5 Vlots supply
Vss : ground reference
8085 Instructions
An instruction of computer is a command given to the computer to perform a specified operation on given data. Some instructions of Intel 8085 microprocessor are: MOV, MVI, LDA, STA, ADD, SUB, RAL, INR, MVI, etc.
Opcode and Operands
Each instruction contains two parts: Opcode (Operation code) and Operand. The 1st part of an instruction which specifies the task to be performed by the computer is called Opcode.
The 2nd part of the instruction is the data to be operated on, and it is called Operand. The Operand (or data) given in the instruction may be in various forms such as 8-bit or 16-bit data, 8-bit or 16-bit address, internal registers or a register or memory location.
Instruction Word Size
A digital computer understands instruction written in binary codes (machine codes). The binary codes of all instructions are not of the same length. According to the word size, the Intel 8085 instructions are classified into the following three types:
One byte instruction
Two byte instruction
Three byte instruction
1. One-byte instruction:
Examples of one byte instructions are:
MOV A, B ; Move the content of the register B to register A.
ADD B ; Add the content of register B to the content of the accumulator.
All the above two examples are only one byte long. All one-byte instructions contain information regarding operands in the opcode itself.
2. Two-byte instruction:
In a two byte instruction the first byte of the instruction is its opcode and the second byte is either data or address.
Example:
MVI B, 05; 05 moved to register B.
06, 05; MVI B, 05 is in the code form.
The first byte 06 is the opcode for MVI B and second byte 05 is the data which is to be moved to register B.
3. Three-byte instruction:
The first byte of the instruction is its opcode and the second and third bytes are either 16-bit data or 16-bit address.
Example:
LXI H, 2400H; Load H-L Pair with 2400H
21, 00, 24; LXI H, 2400H in the code form
The first byte 21 is the opcode for the instruction LXI H. The second 00 is 8 LSBs of the data (2400H), which is loaded into register L. The third byte 24 is 8 MSBs of the data (2400H), which is loaded into register H.
Instruction Cycle
The time required to fetch an instruction and necessary data from memory and to execute it, is called an instruction cycle. Or the total time required to execute an instruction is given by:
IC = FC + EC
Where,
IC = Instruction Cycle
FC = Fetch Cycle
EC = Execute Cycle
Timing Diagram for Instruction Cycle
- Fetch the instruction (Fetch Cycle)
In the beginning of the fetch cycle, the content of the program counter (PC), which is the address of the memory location where opcode is available, is sent to the memory. The memory puts the opcode on the data bus so as to transfer it to the CPU.
The whole operation of fetching an opcode takes three clock cycles. A slow memory may take more time.
- Decode the instruction (Decode Cycle)
The opcode fetched from the memory goes to the data register, DR and then to instruction register, IR. From the IR it goes to the decoder circuitry which decodes the instruction. Decoder circuitry is within the microprocessor.
- Execute the Instruction (Execute Cycle)
After the instruction is decoded, execution begins.
If the operand is reside the general purpose registers, execution is immediately performed. The time taken in decoding and execution of an instruction is one clock cycle. In some situations, an execute cycle may involve one or more read or write cycles or both.
Machine Cycle and State
The necessary steps carried out to perform the operation of accessing either memory or input output device, constitute a machine cycle. In other words, necessary steps carried out to perform a fetch, a read or a write operation constitutes a machine cycle. One sub-division of an operation performed in one clock cycle is called a state or T-state. In short, one clock cycle of the system clock is referred to as a state.
by Jesmin Akther | Jun 25, 2021 | Assembly Language
Assembly language Constants
There are many directives provided by NASM that define constants. Here, will particularly discuss three directives
- EQU
- %assign
- %define
The EQU Directive
The EQU directive is used for defining constants. The syntax of the EQU directive is as follows −
CONSTANT_NAME EQU expression
For example,
TOTAL_STUDENTS equ 50
You can then use this constant value in your code, like −
mov ecx, TOTAL_STUDENTS
cmp eax, TOTAL_STUDENTS
The operand of an EQU statement can be an expression −
LENGTH equ 20 WIDTH
equ 10 AREA
equ length * width
Above code segment would define AREA as 200.
Example
SYS_EXIT equ 1
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0x80
mov eax,SYS_EXIT ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg1 db 'Hello Learners!',0xA,0xD
len1 equ $ - msg1
msg2 db 'Welcome to the world of,', 0xA,0xD
len2 equ $ - msg2
msg3 db 'Draftsbook assembly programming language ! '
len3 equ $- msg3
OUTPUT
Hello, Learners!
Welcome to the world of,
Draftsbook assembly programming language !
The %assign Directive
The %assign directive can be used to define numeric constants such as the EQU directive which is allows redefinition. For instance, define the constant TOTAL as:
%assign TOTAL 10
Later in the code, you can redefine it as −
%assign TOTAL 20
This directive is case-sensitive.
The %define Directive
The %define directive can defining both numeric and string constants which is more like to the #define in C. For instance, you may define the constant PTR as:
%define PTR [EBP+4]
The above code replaces PTR by [EBP+4].
This directive also provide redefinition and it is case-sensitive.
Assembly language Variables
In the generated code or data assembly language, every instance of the variable has a fixed value. The type of a variable cannot be changed. Variables are one of the following types:
- Numeric.
- Logical.
- String.
The range of possible values of a numeric variable is the same as the range of possible values of a numeric constant or numeric expression.
Allocating Storage to Initialized Data
The syntax for storage allocation statement for initialized data is −
[variable-name] define-directive initial-value [,initial-value]
Where, variable-name is the identifier for each storage space. The assembler associates an offset value for each variable name defined in the data segment.
The five basic forms of the define directive below in table:
Directive |
Purpose |
Storage Space |
DB |
Define Byte |
allocates 1 byte |
DW |
Define Word |
allocates 2 bytes |
DD |
Define Doubleword |
allocates 4 bytes |
DQ |
Define Quadword |
allocates 8 bytes |
DT |
Define Ten Bytes |
allocates 10 bytes |
Following are some examples of using define directives:
big_number DQ 123456789
real_number1 DD 1.234
choice DB 'y'
number DW 12345
neg_number DW -12345
real_number2 DQ 123.456
Some important point need to know:
- Each byte of character is stored as its ASCII value in hexadecimal.
- Each decimal value is automatically converted to its 16-bit binary equivalent and stored as a hexadecimal number.
- Processor uses the little-endian byte ordering.
- Negative numbers are converted to its 2’s complement representation.
- Short and long floating-point numbers are represented using 32 or 64 bits, respectively.
Example
section .text
global _start ;must be declared for linker (gcc)
_start: ;tell linker entry point
mov edx,1 ;message length
mov ecx,choice ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
choice DB 'j'
OUTPUT
j
Allocating Storage Space to Uninitialized Data
The rest of directives are used for reserving space for uninitialized data. The reserve directives take a single operand that specifies the number of units of space to be reserved. Each define directive has a related reserve directive.
There are five basic forms of the reserve directive −
Directive |
Purpose |
RESB |
Reserve a Byte |
RESW |
Reserve a Word |
RESD |
Reserve a Doubleword |
RESQ |
Reserve a Quadword |
REST |
Reserve a Ten Bytes |
Multiple Definitions
Multiple data definition statements in a program is given below.
choice DB 'Y' ;ASCII of y = 79H
number1 DW 12345 ;12345D = 3039H
number2 DD 12345679 ;123456789D = 75BCD15H
In the assembler allocates contiguous memory for multiple variable definitions.
Multiple Initializations
The TIMES directive provides multiple initializations to the same value. For instance, an array named marks of size 9 can be defined and initialized to zero using the following statement:
marks TIMES 9 DW 0
The TIMES directive is useful in defining arrays and tables. Example code below:
section .text
global _start ;must be declared for linker (ld)
_start: ;tell linker entry point
mov edx,5 ;message length
mov ecx, stars ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
stars times 5 db '*'
OUTPUT
*****
by Jesmin Akther | Jun 24, 2021 | Microprocessor & Microcontroller
Types of Microprocessors:
To know about the types of microprocessor lets know what is a microprocess its use and feature evolution. Part 1: Introduction to Microprocessor Evolution with features. Some of the types are discussed with real time use examples:
Vector Processors
For vector computations this kind of processor was designed. A vector is an array of operands of the same type. Consider the following vectors:
Vector A (a1, a2, a3, ……., an)
Vector B (b1, b2, b3,……., bn)
Vector C = Vector A + Vector B
= C(c1, c2, c3, …….,cn), where c1 = a1+ b1, c2 = a2 + b2, …..,Cn= an + bn.
A vector processor adds all the elements of vector A and Vector B using a single vector instruction with hardware approach.
Examples:
DEC’s VAX 9000, IBM 390/VF,CRAY Research Y-MP family, and Hitachi’s S-810/20, etc.
Array Processors or SIMD Processors:
This Array processors are also designed for vector computations. The key difference between an array processor and a vector processor is a vector processor uses multiple vector pipelines, on the other hand an array processor deploys a number of processing elements to operate in parallel. An array processor contains multiple numbers of ALUs. Each ALU is provided with the local memory. The ALU together with the local memory is called a Processing Element (PE). An array processor is a SIMD (Single Instruction Multiple Data) processor. So using a single instruction, the same operation can be performed on an array of data which makes it suitable for vector computations.
Types of Microprocessors
Scalar and Superscalar Processors:
A processor executes scalar data is called scalar processor. The simplest scalar processor makes processing of only integer instruction using fixed-points operands. A powerful scalar processor makes processing of both integer as well floating- point numbers. It contains an integer ALU and a Floating Point Unit (FPU) on the same CPU chip.
A scalar processor may be RISC processor or CISC processor.
Examples of CISC processors are: Intel 386, 486; Motorola’s 68030, 68040; etc.
Examples of RISC scalar processors are: Intel i860, Motorola MC8810, SUN’s SPARC CY7C601, etc.
A superscalar processor has multiple pipelines and executes more than one instruction per clock cycle. Examples of superscalar processors are: Pentium, Pentium Pro, Pentium II, Pentium III, etc.
Other Three Types of Microprocessors namely, CISC, RISC, and EPIC.
They are as follows:
1. CISC (Complex Instruction Set Computer)
As the name suggests, the instructions are in a complex form. It means that a single instruction can contain many low-level instructions. For example loading data from memory, storing data to the memory, performing basic operations, etc. Besides, we can say that a single instruction has multiple addressing modes. Furthermore, as there are many operations in single instruction they use very few registers.
Examples of CISC are: Intel 386, Intel 486, Pentium, Pentium Pro, Pentium II, etc.
2. RISC (Reduced Instruction Set Computer)
As per the name, in this, the instructions are quite simple, and hence, they execute quickly. Moreover, the instructions get complete in one clock cycle and also use a few addressing modes only. Besides, it makes use of multiple registers so that interaction with memory is less.
Examples are IBM RS6000, DEC Alpha 21064, DEC Alpha 21164, etc.
3. EPIC (Explicitly Parallel Instruction Computing)
It allows the instructions to compute parallelly by making use of compilers. Moreover, the complex instructions also process in fewer clock frequencies. Furthermore, it encodes the instructions in 128-bit bundles. Where each bundle contains three instructions encoded in 41 bits each and a 5-bit template. This 5-bit template contains information about the type of instructions and that which instructions can be executed in parallel.
Examples are IA-64 (Intel Architecture-64), etc.
Characteristics of CISC are as follows:
- As the instructions are complex hence, the decoding of instructions is also complex.
- The size of instructions is greater than the one-word size.
- Instruction can take more than one clock cycle to execute.
- The number of registers is less since most of the operations are performed in memory itself.
- The address modes are also complex.
- Data types are more in number.
Characteristics of RISC are as follows:
- As the instruction is simple hence, the decoding of instructions is also simple
- The size of instructions is under the one-word size.
- Instruction takes one clock cycle to execute.
- The number of registers is more.
- The address modes are also simple.
- Here, Data types are less in number.
- It Can be used for pipelining.
Advantages of CISC are as follows:
Microprogramming in CISC is as easy as the assembly language implementation. The number of instructions for performing operations is less in number. Hence, the memory usage is low. Moreover, time consumption is also less.
Advantages of RISC are as follows:
Instructions are simple hence easy to understand and decode. The instructions complete in one clock cycle therefore, CPU can handle multiple instructions at a time. Designing the RISC processors is easier in comparison to the CISC processors.
Key difference between RISC and CISC
RISC(Reduced Instruction Set Computer) |
CISC(Complex Instruction Set Computer) |
1. RISC focuses on the software. |
1. CISC focuses on the hardware. |
2. It uses the hardwired control unit. |
2. It uses a hardwired as well as a microprogrammed control unit. |
3. RISC uses transistors for more registers. |
3. Transistors are used for storing the complex instructions. |
4. The instructions have a fixed size. |
4. The size of instructions vary. |
5. It performs only register to register arithmetic operations. |
5. Besides the register to register operations, it can also perform register to memory (or memory to register operations). |
6. Fewer registers are used. |
6. It requires more number of registers. |
7. As the instructions are individual the code is large. |
7. Multiple operations are present in single instruction hence, the code is small. |
Digital Signal Processors (DSP):
DSP microprocessors specifically designed to process signals. They receive some digitized signal information, perform some mathematical operations on the information and give the result to an output device. They implement integration, differentiation, complex fast Fourier transform, etc. using hardware.
Examples of digital signal processors are:
Texas instruments’ TMS 320C25, Motorola 56000, National LM 32900, Fujitsu MBB 8764, etc.
Symbolic Processors
Symbolic processors are designed for expert system, machine intelligence, knowledge based system, pattern-recognition, text retrieval, etc. The basic operations which are performed for artificial intelligence are: Logic interference, compare, search, pattern matching, filtering, unification, retrieval, reasoning, etc. This type of processing does not require floating point operations. Symbolic processors are also called LISP processors or PROLOG processors.
Bit-Slice Processors:
The processor of desired word length is developed using the building blocks. The basic building block is called Bit-Slice where the building blocks include 4-bit ALUs, micro programs sequencers, carry look-ahead generators, etc. The word ‘slice’ was used because the desired number of ALUs and other components were used to build an 8-bit, 16-bit or 32-bit CPU.
Examples of Bit-Slice Processors were:
AMD-2900, AMD 2909, AMD 2910, AMD 29300 series, Texas instrument’s SN-74AS88XX series, etc.
Transputers
In a multiprocessor system, a transputer is a specially designed microprocessor to operate as a component processor. Transputers were introduced in late 1980’s. They were built on VLSI chip and contained a processor, memory and communication links. The communication link was to provide point-to-point connection between transputers. A transputer contains FPU, on-chip RAM, high-speed serial link, etc.
Examples of transputers are: INMOS T414, INMOS T800, etc. Where, T414 was a 32-bit processor with 2 KB memory. The T800 was FPU version of 32-bit transputer with 4 KB memory.
Graphic Processors
Graphics Processors are specially designed processors for graphics. Intel has developed Intel 740-3D graphics chip. It is optimized for Pentium II PCs, using a hyper pipelined 3D architecture with additional 2D acceleration. Like most 3D graphics chips, the I-740 will be marketed in performance, not the main stream category. It is designed mostly for such heavy multimedia uses as games and movies.
by Jesmin Akther | Jun 24, 2021 | Microprocessor & Microcontroller
What is a Microprocessor?
A digital Computer or Computer’s Central Processing Unit (CPU) built on a single Integrated Circuit (IC) is called a microprocessor. Since it is a programmable electronics chip of computing and decision making capabilities. The microprocessor contains numerous components like transistors, registers, and diodes that work together or programmable, multipurpose, clock -driven, register-based electronic device. Any microprocessor based systems having limited number of resources are called microcomputers. It reads binary instructions from a storage device called memory, accepts binary data as input and processes data according to those instructions and provides results as output. Nowadays, microprocessor can be seen in almost all types of electronics devices such as mobile phones, printers, washing machines etc.

Block Diagram of a Microcomputer
Microprocessor Introduction
A microprocessor consists of an ALU, control unit and register array. ALU performs arithmetic and logical operations on the data received from an input device or memory. Control unit controls the instructions and flow of data within the computer. And, register array consists of registers identified by letters like B, C, D, E, H, L, and accumulator.
Evolution of Microprocessors
The category of microprocessor according to the generations or according to the size of the microprocessor:
First Generation (4 – bit Microprocessors configuration)
The first generation microprocessors were introduced in the year 1971 by Intel Corporation. It was named Intel 4004 since it was a 4-bit processor. It was a processor on a single chip. It could perform simple arithmetic and logical operations such as addition, subtraction, Boolean OR and Boolean AND. It had a control unit able to perform control functions and 4 bit data like fetching an instruction from storage memory, decoding it, and then generating control pulses to execute it. Intel introduced the enhanced version of 4004, the 4040. 4-bit Microprocessor.
- The INTEL 4004/4040 was invented in the year 1971 by Stanley Mazor & Ted Hoff.
- The clock speed of this microprocessor is 740 KHz.
- The number of transistors used in this microprocessor is 2,300 and instruction per second is 60K.
- The number of pins of this microprocessor is 16.
Second Generation (8 – bit Microprocessor configuration)
The second generation microprocessors were introduced in 1973 again by Intel. It was a first 8 – bit microprocessor which could perform arithmetic and logic operations on 8-bit words. It was Intel 8008, and another improved version was Intel 8088.
- The 8008 processor was invented in the year 1972. The clock speed of this microprocessor is 500 KHz and instruction per second is 50K.
- The 8080 microprocessor was invented in the year 1974. The clock speed is 2 MHz. The number of transistors used is 60k and instruction per second is 10 times quicker as compared with 8008 processor.
- The 8085 microprocessor was invented in the year 1976. The clock speed is 3 MHz. The number of transistors used is 6,500 and instruction per second is 769230.
- The number of pins of this microprocessor is 40
Third Generation (16 – bit Microprocessor configuration)
The third generation microprocessors, introduced in 1978 were represented by Intel’s 8086, Zilog Z800 and 80286, which were 16 – bit processors with a performance like minicomputers.
- The 8086 microprocessor was invented in the year 1978. The clock speed is 4.77, 8 & 10 MHz. The number of transistors used is 29000 and instruction per second is 2.5 Million. The number of pins of this microprocessor is 40
- The 8088 microprocessor was invented in the year 1979 and instruction per second is 2.5 Million
- The microprocessors like 80186 or 80188 were invented in the year 1982. The clock speed is 6 MHz
- The 80286 microprocessor was invented in the year 1982. The clock speed is 8 MHz. The number of transistors used is 134000 and instruction per second is 4 Million.
- The number of pins of this microprocessor is 68 and 32-bit Microprocessor
Fourth Generation (32 – bit Microprocessors configuration)
Several different companies introduced the 32-bit microprocessors, but the most popular one is the Intel 80386.
- The Intel 80386 microprocessor was invented in the year 1986. The clock speed is 16 MHz to 33 MHz. The number of transistors used is 275000. The number of pins is 132 14X14 PGA
- The Intel 80486 microprocessor was invented in the year1986. The clock speed is 16MHz to 100 MHz. The number of transistors used is 1.2 Million transistors and instruction per second is 8 KB of cache memory. The number of pins of this microprocessor is 168 17X17 PGA (Pin Grid Array)
- The PENTIUM microprocessor was invented in the year 1993. The clock speed is 66 MHz and instruction per second is Cache memory 8-bit for instructions 8- bit for data. The number of pins of this microprocessor is 237 PGA
Fifth Generation (64 – bit Microprocessors configuration)
From 1995 to now we are in the fifth generation. After 80856, Intel came out with a new processor namely Pentium processor followed by Pentium Pro CPU, which allows multiple CPUs in a single system to achieve multiprocessing.
- The INTEL core 2 microprocessor was invented2006. The clock speed is 1.2 GHz to 3 GHz.
- The number of transistors used is 291 Million and instruction per second is 64 KB of L1 cache for each core 4 MB of L2 cache.
- The i3, i5, i7 microprocessors were invented in the years 2007, 2009, 2010 2. The clock speed is 2GHz to 3.3GHz, 2.4GHz to 3.6GHz & 2.93GHz to t 3.33GHz.
Other improved 64-bit processors are Celeron, Dual, Quad, Octa Core processors.
Important Intel Microprocessors abbreviation
PGA – Pin Grid Array
MMX – Multimedia Extensions
EPIC – Explicitly Parallel Instruction Computing
SIMD – Single Instruction Multiple Data
ALU – Arithmetic and Logic Unit
MMU – Memory Management Unit
FPU – Floating Point Unit

Microprocessor-based system
- Address Bus:
It carries the address, which is a unique binary pattern used to identify a memory location or an I/O port. For example, an eight bit address bus has eight lines and thus it can address 28 = 256 different locations. The locations in hexadecimal format can be written as 00H – FFH.
- Data Bus:
The data bus is used to transfer data between memory and processor or between I/O device and processor. For example, an 8-bit processor will generally have an 8-bit data bus and a 16-bit processor will have 16-bit data bus.
- Control Bus:
The control bus carry control signals, which consists of signals for selection of memory or I/O device from the given address, direction of data transfer and synchronization of data transfer in case of slow devices.
Basic Terms used in Microprocessor:
A list of some basic terms used in microprocessor:
- Instruction Set:
The group of commands that the microprocessor can understand is called Instruction set. It is an interface between hardware and software.
- Bus :
Set of conductors intended to transmit data, address or control information to different elements in a microprocessor. A microprocessor will have three types of buses, i.e., data bus, address bus, and control bus.
- IPC (Instructions Per Cycle) :
It is a measure of how many instructions a CPU is capable of executing in a single clock.
- Clock Speed :
It is the number of operations per second the processor can perform. It can be expressed in megahertz (MHz) or gigahertz (GHz). It is also called the Clock Rate.
- Bandwidth:
The number of bits processed in a single instruction is called Bandwidth.
- Word Length:
The number of bits the processor can process at a time is called the word length of the processor. 8-bit Microprocessor may process 8 -bit data at a time. The range of word length is from 4 bits to 64 bits depending upon the type of the microcomputer.
- Data Types:
The microprocessor supports multiple data type formats like binary, ASCII, signed and unsigned numbers.
Working of Microprocessor
The microprocessor follows a sequence to execute the instruction: Fetch, Decode, and then Execute. Initially, the instructions are stored in the storage memory of the computer in sequential order. The microprocessor fetches those instructions from the stored area (memory), then decodes it and executes those instructions till STOP instruction is met. Then, it sends the result in binary form to the output port. Between these processes, the register stores the temporary data and ALU (Arithmetic and Logic Unit) performs the computing functions.
Features of Microprocessor
Low Cost:
Microprocessors are available at very low cost. It will reduce the cost of a computer system.
High Speed:
the microprocessor can work at very high speed. It can execute millions of instructions per second.
Small Size:
A microprocessor is fabricated in a very less footprint due to very large scale and ultra large scale integration technology. Because of this, the size of the computer system is reduced.
Versatile:
The same chip can be used for several applications, therefore, microprocessors are versatile.
Low Power Consumption:
Microprocessors are using metal oxide semiconductor technology, which consumes less power.
Less Heat Generation:
Microprocessors uses semiconductor technology and will not emit much heat as compared to vacuum tube devices.
Reliable:
The failure rate is very less. Hence it is very reliable.
Portable :
small in size and low power consumption microprocessors are portable.
by Jesmin Akther | Jun 24, 2021 | Microprocessor & Microcontroller
8085 microprocessor Instruction and Data Formats.
There are numerous techniques to require data for instructions are:
- In the instruction itself 8-bit or 16-bit data may be directly given.
- In the instruction itself, the address of the memory location, I/O port or I/O device, where data resides, may be given.
- In some instructions, only one register is specified. The content of the specified register is one of the operands.
- Some instructions specify two registers and contents of the registers are the required data.
- In some instructions, data is implied and in the most instructions of this type operate on the content of the accumulator.
To specify various kind of data for instructions, the machine codes of all instructions are not of the same length. It may vary as 1-byte, 2-byte or 3-byte instruction.
8085 microprocessor Addressing Modes
Each instruction requires some data on that it has to operate. There are different techniques which are called addressing modes. Intel 8085 uses the following addressing modes:
In this addressing mode, the address of the operand or data is given in the instruction itself.
Example
STA 2400H: This stores the content of the accumulator in the memory location 2400H.
32, 00, 24: The above instruction in the code form.
In the above instruction, 2400H is the memory address for data stored given in the instruction itself. The 2nd and 3rd bytes of the instruction specify the address of the memory location. It is clear, that the source of the data is accumulator.
In register addressing mode, the operand is in one of the general-purpose registers. The opcode specifies the address of the register(s) in addition to the operation to be performed.
Example:
MOV A, B: Move the content of B register to register A.
78: The instruction in the code form.
In the above MOV A, B is 78H. In addition the operation to be performed the opcode also specifies source and destination registers.
The opcode 78H can be printed in binary form as 01111000. The first two bits, i.e. 0 1 are for MOV operation, the next three bits 1 1 1 are the binary code for register A, and the last three bits 000 are the binary code for register B.
- Register Indirect Addressing
The Register Indirect mode of addressing operand is specified by a register pair.
Example
- LXI H, 2500 H – Load H-L pair with 2500H.
- MOV A, M – Move the content of the memory location, whose address is in H-L pair (i.e. 2500 H) to the accumulator.
- HLT – Halt.
In the above program MOV A, M is register indirect addressing. This instruction, the operand is in the memory. The address of the memory is not directly given in the instruction. The address of the memory resides in H-L pair and this has already been specified by an earlier instruction in the program, i.e. LXI H, 2500 H.
In this addressing mode the operand is specified within the instruction itself.
Example
immediate addressing. 2500 is 16-bit data which is given in the instruction itself. It is to be loaded into H-L pair.
There are certain instructions which operate on the content of the accumulator. Such instructions do not require the address of the operand.
Example
CMA, RAL, RAR, etc.
Status Flags
There is a set of five flip-flops which indicate status (condition) arising after the execution of arithmetic and logic instructions. These are:
- Carry Flag (CS)
- Parity Flag (P)
- Auxiliary Carry Flags (AC)
- Zero Flags (Z)
- Sign Flags (S)
Symbols and Abbreviations:
The symbol and abbreviations which have been used while explaining Intel 8085 instructions are as follows:
Symbol with details |
|
Addr |
16-bit address of the memory location. |
|
Data |
8-bit data |
|
data 16 |
16-bit data |
|
r, r1, r2 |
One of the registers A, B, C, D, E, H or L |
|
A, B, C, D, H, L |
8-bit register |
|
A |
Accumulator |
|
H-L |
Register pair H-L |
|
B-C |
Register pair B-C |
|
D-E |
Register pair D-E |
|
PSW |
Program Status Word |
|
M |
Memory whose address is in H-L pair |
|
H |
Appearing at the end of the group of digits specifies hexadecimal, e.g. 2500H |
|
Rp |
One of the register pairs. |
|
Rh |
The high order register of a register pair |
|
Rl |
The low order register of a register pair |
|
PC |
16-bit program counter, PCH is high order 8 bits and PCL low order 8 bits of register PC. |
|
CS |
Carry Status |
|
[] |
The contents of the register identified within bracket |
|
[ [] ] |
The content of the memory location whose address is in the register pair identified within brackets |
|
^ |
AND operation |
|
∨ |
OR operation |
|
⊕ or ∀ |
Exclusive OR |
|
← |
Move data in the direction of arrow |
|
⇔ |
Exchange contents |
|
Intel 8085 Instructions:
An instruction of a computer is a command given to the computer to perform a specified operation on given data. In microprocessor, the instruction set is the collection of the instructions that the microprocessor is designed to execute.
The programmer writes a program in assembly language using these instructions. These instructions have been classified into the following groups:
Data Transfer Group:
Instructions which are used to transfer the data from a register to another register from memory to register or register to memory come under this group
Instruction
Example |
Description |
States |
Flags |
Addressing |
Machine Cycles |
MOV A, B |
Move the content of the one register to another |
4 |
none |
Register |
1 |
MOV B, M |
Move the content of memory to register |
7 |
none |
Register Indirect |
2 |
MOV M, C |
Move the content of register to memory |
7 |
none |
Register Indirect |
2 |
MVI r, data
[r] ←dataExample:MVI M, 08 |
Move immediate data to register |
7 |
None |
Immediate Register |
3 |
LXI H, 2500H |
Load Register pair immediate |
10 |
None |
Immediate |
3 |
LDA 2400 H |
Load Accumulator direct |
13 |
None |
Direct |
4 |
STA 2000H |
Store accumulator direct |
13 |
None |
Direct |
4 |
LHLD 2500H |
Load H-L pair direct |
16 |
None |
Direct |
5 |
SHLD 2500 H |
Store H-L pair direct |
16 |
None |
Direct |
5 |
LDAX B |
Load accumulator indirect |
7 |
None |
Register Indirect |
2 |
STAX D |
Store accumulator indirect |
7 |
None |
Register Indirect |
2 |
XCHG
[H-L] ↔[D-E] |
Change the contents of H-L with D-E pair |
4 |
None |
Register |
1 |
Arithmetic Group:
The instructions of this group perform arithmetic operations such as addition, subtraction, increment or decrement of the content of a register or a memory.
Instruction Example |
Explanation |
States |
Flags |
Addre-ssing |
Machine Cycles |
ADD r
[A] ←[A]+[r]Example: ADD K |
Add register to accumulator |
4 |
All |
Register |
1 |
ADD M
[A] ← [A] + [[H-L]]Example: ADD K |
Add memory to accumulator |
7 |
All |
Register indirect |
2 |
ACC r
[A] ← [A] + [r] + [CS]Ex:ACC K |
Add register with carry to accumulator |
4 |
All |
Register |
1 |
ADC K |
Add memory with carry to accumulator |
7 |
All |
Register indirect |
2 |
ADI 55K |
Add immediate data to accumulator |
7 |
All |
Immediate |
2 |
ACI 55K |
Add with carry immediate data to accumulator |
7 |
All |
Immediate |
2 |
DAD K |
Add register paid to H-L pair |
10 |
CS |
Register |
3 |
SUB K |
Subtract register from accumulator |
4 |
All |
Register |
1 |
SUB M
[A] ← [A] – [[H-L]]Example: SUB K |
Subtract memory from accumulator |
7 |
ALL |
Register indirect |
2 |
SBB K |
Subtract memory from accumulator with borrow |
7 |
All |
Register indirect |
2 |
SUI 55K |
Subtract immediate data from accumulator |
7 |
All |
Immediate |
2 |
XCHG |
Subtract immediate data from accumulator with borrow |
7 |
All |
Immediate |
2 |
INR K |
Increment register content |
4 |
All except carry flag |
Register |
1 |
INR K |
Increment memory content |
10 |
All except carry flag |
Register indirect |
3 |
DCR K |
Decrement register content |
4 |
All except carry flag |
Register |
1 |
DCR K |
Decrement memory content |
10 |
All except carry flag |
Register indirect |
3 |
INX K |
Increment memory content |
6 |
None |
Register |
1 |
DCX K |
Decrement register pair |
6 |
None |
Register |
1 |
DAA |
Decimal adjust accumulator |
4 |
|
|
1 |
Logical Group
The instructions in this group perform logical operation such as AND, OR, compare, rotate, etc.
Instruction Set |
Explanation |
States |
Flags |
Addressing |
Machine Cycles |
ANA r
[A] ←[A]∧[r] |
AND register with accumulator |
4 |
All |
Register |
1 |
ANA M
[A] ←[A]∧[[H-]] |
AND memory with accumulator |
4 |
All |
Register indirect |
2 |
ANI data
[A] ← [A] ∧ [data] |
AND immediate data with accumulator |
7 |
All |
Immediate |
2 |
ORA r
[A] ←[A]∨[r] |
OR-register with accumulator |
4 |
All |
Register |
1 |
ORA M
[A] ←[A]∨[[H-L]] |
OR-memory with accumulator |
7 |
All |
Register indirect |
2 |
ORI data
[A] ← [A] ∨ [data] |
OR -immediate data with accumulator |
7 |
All |
Immediate |
2 |
CMP r
[A]-[r] |
Compare register with accumulator |
4 |
All |
Register |
1 |
CMP M
[A] – [[H-L]] |
Compare memory with accumulator |
7 |
All |
Register indirect |
2 |
CPI data
[A] – data |
Compare immediate data with accumulator |
7 |
All |
Immediate |
2 |
RLC
[An+1] ←[An], [A0] ←[A7], [CS] ←[A7] |
Rotate accumulator left |
4 |
Cs |
Implicit |
1 |
RRC
[A7] ←[A0], [CS] ←[A0], [An] ←[An+1] |
Rotate accumulator right |
|
CS |
Implicit |
1 |
RAL
[An+1] ←[An], [CS] ←[A7], [A0] ←[CS] |
Rotate accumulator left through carry |
|
CS |
Implicit |
1 |
RAR
[An] ←[An+1], [CS] ←[A0], [A7] ←[CS] |
Rotate accumulator right through carry |
|
CS |
Implicit |
1 |
Branch Control Group: This group contains the instructions for conditional and unconditional jump, subroutine call and return, and restart.
Others are Unconditional Jump, Conditional Jump, Unconditional CALL, Conditional CALL, Unconditional Return, Conditional Return, Restart, Stack, I/O and Machine Control Group.
by Jesmin Akther | Jun 19, 2021 | JAVA
Java String
In Java, string is an object of sequence of char values like an array of characters. There are two ways to create String object:
String literal and new keyword
1) String Literal
Java String literal is created by using double quotes. For Example:
char[] names={‘d’,’r’,’a’,’f’,’t’,’s’,’b’,’o’,’o’,’k’,’.’,’c’,’o’,’m’};
- String site=new String(names); //String new keyword
Is same as:
String names =”draftsbook.com”; // String Literal
Each time create a string literal, the JVM checks the “string constant pool” first. If the string already in the pool, a reference to the pool instance is returned. If the string doesn’t exist in the pool, a new string instance is created and placed in the pool. For instance:
String site1=” draftsbook.com “;
String site2=” draftsbook.com “; //It doesn’t create a new instance
In the above example, only one object will be created. Firstly, JVM will not find any string object with the value “draftsbook.com “in string constant pool. So, it will create a new object. After that it will find the string with the value “draftsbook.com “in the pool, it will not create a new object but will return the reference to the same instance. Keep in mind that String objects are stored in a special memory area known as the “string constant pool”. Why Java uses String literal? In order to make Java more memory efficient. Because no new objects are created if it exists already in the string constant pool. Learn about or easily understand String, StringBuffer read this writing What is the defference among String, StringBuffer and StringBuilder.
2) By new keyword
String site=new String(“draftsbook.com”);//creates two objects and one reference variable.
In such case, JVM will create a new string object in normal or non-pool or a heap memory in addition to the literal ” draftsbook.com ” will be positioned in the string constant pool. The variable site will refer to the object in a heap.Learn about or easily understand String, StringBuffer read this writing What is the defference among String, StringBuffer and StringBuilder.
Java String Example
public class ExString{
public static void main(String args[]){
String site1=" draftsbook.com ";//creating string by java string literal
char[] names={'d','r','a','f','t','s','b','o','o','k',’.’,’c’,’o’,’m’};
String site2=new String(names);//converting char array to string
String site3=new String("https://draftsbook.com/");//creating java string by new keyword
System.out.println(site1);
System.out.println(site2);
System.out.println(site3);
}
}
OUTPUT
draftsbook
draftsbook.com
Home
Java String class allows a several number of methods to do operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.Learn about or easily understand String, StringBuffer read this writing What is the defference among String, StringBuffer and StringBuilder.
The interface is used to represent the sequence of characters. String, StringBuffer and StringBuilder classes implement it. It means, we can create strings in java by using these three classes.
The Java String is immutable which means it cannot be changed. Whenever alteration any string, a new instance is created. For mutable strings, you can use StringBuffer and StringBuilder classes.
- Java String class methods
The java.lang.String class provides many useful methods to perform operations on sequence of char values.Learn about or easily understand String, StringBuffer read this writing What is the defference among String, StringBuffer and StringBuilder.
Method and Description of Java:
- char charAt(int index) : This method returns char value for the particular index
- int length() : This method returns string length
- static String format(String format, Object… args): This method returns a formatted string.
- static String format(Locale l, String format, Object… args) : This method returns formatted string with given locale.
- String substring(int beginIndex) : This method returns substring for given begin index.
- String substring(int beginIndex, int endIndex): This method returns substring for given begin index and end index.
- boolean contains(CharSequence s) : This method returns true or false after matching the sequence of char value.
- static String join(CharSequence delimiter, CharSequence… elements): This method returns a joined string.
- static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) : This method returns a joined string.
- boolean equals(Object another): This method checks the equality of string with the given object.
- boolean isEmpty(): This method checks if string is empty.
- String concat(String str) : This method concatenates the specified string.
- String replace(char old, char new) : This method replaces all occurrences of the specified char value.
- String replace(CharSequence old, CharSequence new) : This method replaces all occurrences of the specified CharSequence.
- static String equalsIgnoreCase(String another): This method compares another string. It doesn’t check case.
- String[] split(String regex) : This method returns a split string matching regex.
- String[] split(String regex, int limit) : This method returns a split string matching regex and limit.
- String intern(): This method returns an interned string.
- int indexOf(int ch) : This method returns the specified char value index.
- int indexOf(int ch, int fromIndex) : This method returns the specified char value index starting with given index.
- int indexOf(String substring): This method returns the specified substring index.
- int indexOf(String substring, int fromIndex): This method returns the specified substring index starting with given index.
- String toLowerCase(): This method returns a string in lowercase.
- String toLowerCase(Locale l): This method returns a string in lowercase using specified locale.
- String toUpperCase() : This methodreturns a string in uppercase.
- String toUpperCase(Locale l) : This method returns a string in uppercase using specified locale.
- String trim(): This method removes beginning and ending spaces of this string.
- static String valueOf(int value): This methodconverts given type into string. It is an overloaded method.
Strings are actually are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects. The Java platform provides the String class to create and manipulate strings.Learn about or easily understand String, StringBuffer read this writing What is the defference among String, StringBuffer and StringBuilder.
Creating Strings
The most direct way to create a string is to write −
String greeting = “Hello world!”;
Whenever invoke a string literal in code, the compiler creates a String object with its value in this case, “Hello world!’. Since with other object, can create String objects by using the new keyword and a constructor. The String class has 11 constructors that allow to provide the initial value of the string using different sources, such as an array of characters.
Example
public class stringExample {
public static void main(String args[]) {
char[] hArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String hString = new String(hArray);
System.out.println( hString );
}
}
Output
hello.
The String class is immutable, so that once it is created a String object cannot be changed. If there is a necessity to make a lot of modifications to Strings of characters, then use String Buffer & String Builder Classes.
String Length
One accessor method that can use with strings is the length() method, which returns the number of characters contained in the string object. The following program is an example of length(), method String class.
Example
public class stringExample {
public static void main(String args[]) {
String palindrome = "Mim saw It as Sin";
int lengthPalin = palindrome.length();
System.out.println( "String Length is : " + lengthPalin);
}
}
Output
String Length is: 17
Concatenating Strings : The String class includes a method for concatenating two strings,
string1.concat(string2);
This returns a new string that is string1 with string2 added to it at the end. can also use the concat() method with string literals, as in
“My name is “.concat(“Md”)
Strings are more commonly concatenated with the + operator, as in
“Hello,” + ” world” + “!”
OUTPUT
“Hello, world!”
Let us look at the following example
public class stringExample {
public static void main(String args[]) {
String stringSen = "saw It as ";
System.out.println("Mim " + stringSen + "Sin");
}
}
Output
Dot saw I was Mim
Creating Format Strings
The methods printf() and format() are use to print output with formatted numbers. The String class has an equivalent class method, format(), that returns a String object rather than a PrintStream object. Using String’s static format() method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of:
Example
System.out.printf(“The value of the float variable is ” +
“%f, while the value of the integer ” +
“variable is %d, and the string ” +
“is %s”, floatVar, intVar, stringVar);
You can write as:
String fs;
fs = String.format(“The value of the float variable is ” +
“%f, while the value of the integer ” +
“variable is %d, and the string ” +
“is %s”, floatVar, intVar, stringVar);
System.out.println(fs);
String Methods
A few list of methods supported by String class are given below:
Method & Description:
The method is used to returns the character at the specified index.
The method is used to Compares this String to another Object.
- int compareTo(String anotherString)
The method is used to Compares two strings lexicographically.
- int compareToIgnoreCase(String str)
The method is used to Compares two strings lexicographically, ignoring case differences.
- String concat(String str)
The method is used to Concatenates the specified string to the end of this string.
- boolean contentEquals(StringBuffer sb)
The method is used to Returns true if and only if this String represents the same sequence of characters as the specified StringBuffer.
- static String copyValueOf(char[] data)
The method is used to Returns a String that represents the character sequence in the array specified.
- static String copyValueOf(char[] data, int offset, int count)
The method is used to Returns a String that represents the character sequence in the array specified.
- boolean endsWith(String suffix)
The method is used to Tests if this string ends with the specified suffix.
- boolean equals(Object anObject)
The method is used to Compares this string to the specified object.
- boolean equalsIgnoreCase(String anotherString)
The method is used to Compares this String to another String, ignoring case considerations.
The method is used to Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array.
- byte[] getBytes(String charsetName)
The method is used to Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
- void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
The method is used to Copies characters from this string into the destination character array.
The method is used to Returns a hash code for this string.
The method is used to Returns the index within this string of the first occurrence of the specified character.
- int indexOf(int ch, int fromIndex)
The method is used to Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
The method is used to Returns the index within this string of the first occurrence of the specified substring.
- int indexOf(String str, int fromIndex)
The method is used to Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
The method is used to Returns a canonical representation for the string object.
The method is used to Returns the index within this string of the last occurrence of the specified character.
- int lastIndexOf(int ch, int fromIndex)
The method is used to Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
- int lastIndexOf(String str)
The method is used to Returns the index within this string of the rightmost occurrence of the specified substring.
- int lastIndexOf(String str, int fromIndex)
The method is used to Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
The method is used to Returns the length of this string.
- boolean matches(String regex)
The method is used to Tells whether or not this string matches the given regular expression.
- boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
The method is used to Tests if two string regions are equal.
- boolean regionMatches(int offset, String other, int onset, int len)
The method is used to Tests if two string regions are equal.
- String replace(char oldChar, char newChar)
The method is used to Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
- String replaceAll(String regex, String replacement
The method is used to Replaces each substring of this string that matches the given regular expression with the given replacement.
- String replaceFirst(String regex, String replacement)
The method is used to Replaces the first substring of this string that matches the given regular expression with the given replacement.
- String[] split(String regex)
The method is used to Splits this string around matches of the given regular expression.
- String[] split(String regex, int limit)
The method is used to Splits this string around matches of the given regular expression.
- boolean startsWith(String prefix)
The method is used to Tests if this string starts with the specified prefix.
- boolean startsWith(String prefix, int toffset)
The method is used to Tests if this string starts with the specified prefix beginning a specified index.
- CharSequence subSequence(int beginIndex, int endIndex)
The method is used to Returns a new character sequence that is a subsequence of this sequence.
- String substring(int beginIndex)
The method is used to Returns a new string that is a substring of this string.
- String substring(int beginIndex, int endIndex)
The method is used to Returns a new string that is a substring of this string.
The method is used to Converts this string to a new character array.
The method is used to Converts all of the characters in this String to lower case using the rules of the default locale.
- String toLowerCase(Locale locale)
The method is used to Converts all of the characters in this String to lower case using the rules of the given Locale.
The method is used to This object (which is already a string!) is itself returned.
The method is used to Converts all of the characters in this String to upper case using the rules of the default locale.
- String toUpperCase(Locale locale)
The method is used to Converts all of the characters in this String to upper case using the rules of the given Locale.
The method is used to Returns a copy of the string, with leading and trailing whitespace omitted.
- static String valueOf(primitive data type x)
The method is used to Returns the string representation of the passed data type argument.