The X86 Microprocessor & Alp: Microprocessors and Microcontrollers
The X86 Microprocessor & Alp: Microprocessors and Microcontrollers
The X86 Microprocessor & Alp: Microprocessors and Microcontrollers
15CS44
MODULE – 1
THE x86 MICROPROCESSOR & ALP
THE x86 MICROPROCESSOR
BRIEF HISTORY OF THE x86 FAMILY:
A study of history is not essential to understand the microprocessor, but it provides a historical
perspective of the fast-paced evolution of the computer.
Evolution from 8080/8085 to 8086:
In 1978, Intel Corporation introduced a 16-bit microprocessor called the 8086. This processor
was a major improvement over the previous generation 8080/8085 series Intel microprocessors in
several ways:
1. The 8080 / 8085 was an 8-bit system (meaning that, the microprocessor could work on only 8
bits of data at a time; data larger than 8 bits need to be broken into 8-bit pieces to be
processed by the CPU). I n contrast, the 8086 is a 16-bit microprocessor.
2. The 8086's capacity of 1 mega-byte of memory exceeded the 8080/8085's capability of
handling a maximum of 64K bytes of memory.
3. The 8086 was a pipelined processor, as opposed to the non-pipelined 8080/8085 (In a
system with pipelining, the data and address buses are busy transferring data, while the
CPU is processing information; thereby increasing the effective processing power of the
micro -processor).
Table: Evolution of Intel microprocessors up to the 8088
Product 8008 8080 8085 8086 8088
Year introduced 1972 1974 1976 1978 1979
Technology PMOS NMOS NMOS NMOS NMOS
Number of pins 18 40 40 40 40
Number of transistors 3000 4500 6500 29,000 29,000
Number of instructions 66 111 113 133 133
Physical memory 16KB 64KB 64KB 1MB 1MB
Virtual memory None None None None None
Internal data bus 8 8 8 16 16
External data bus 8 8 8 16 8
Address bus 8 16 16 20 20
Data types 8 8 8 8/16 8/16
Evolution from 8086 to 8088:
The 8086 is a microprocessor with a 16-bit data bus internally and externally, meaning that all
registers are 16 bits wide and there is a 16-bit data bus to transfer data in and out of the CPU.
1
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Although the introduction of the 8086 marked a great advancement over the previous generation of
microprocessors, there was still some resistance in using the 16-bit external data bus:
At that time, all peripherals were designed around an 8-bit microprocessor
In addition, a printed circuit board with a 16-bit data bus was much more expensive.
Therefore, Intel came out with the 8088 version. It is identical to the 8086 as far as programming is
concerned, but externally it has an 8-bit data bus instead of a 16-bit bus. It has the same memory
capacity, 1MB.
Success of the 8086:
In 1981, Intel's fortunes ch anged forever w.hen IBM picked up the 8088 as their microprocessor of
choice in designing the IB M PC. The 8088-based IBM PC was an enormous success, because IBM and
Microsoft made it an open system (meaning that, all documentation and specifications of the
h ardware and software of the PC were made public) . This made it possible for man y other vendors
to clone the hardware successfully and thus generated a major growth in both hardware an d
software designs based on the IBM PC. This is in contrast with the Apple computer, whic h was a
closed system ( blocking any attempt at cloning by other manufacturers, both domestically and overseas).
2
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o Later Intel introduced the 386SX, which is internally identical to the 80386 but has a 16-bit
external data bus and a 24-bit address bus, which gives a capacity of 16M bytes (224 = 16M bytes)
of memory. This makes the 386SX system much cheaper.
o With the introduction of the 80486 in 1989, Intel put a greatly enhanced vers ion of the
80386 and the math-coprocessor on a single chip plus additional features such as cache
memory. Cache memory is static RAM with a very fast access time. Note that, all programs
written for the 8088/86 will run on 286, 386, and 486 computers.
In 1992, Intel released the newest x86 microprocessor – the Intel Pentium:
By using submicron fabrication technology, Intel designers were able to utilize more than 3
million transistors on the Pentium chip.
The Pentium had speeds of 60 and 66 MHz (twice that of 80486 and over 300 times faster than
that of the original 8088).
Separate 8K cache memory for code and data.
64-bit external data bus with 32-bit register and 32-bit address bus capable of addressing 4GB of
memory.
Improved floating-point processor.
Pentium is packaged in a 273-pin PGA chip.
It uses BICMOS technology, which combines the speed of bipolar transistors with the power
efficiency of CMOS technology.
Table: Evolution of Intel’s Microprocessors (from the 8086 to the Pentium Pro)
Product 8086 80286 80386 80486 Pentium Pentium Pro
Year introduced 1978 1982 1985 1989 1993 1995
Technology NMOS NMOS CMOS CMOS BICMOS BICMOS
Clock rate (MHz) 3 – 10 10 – 16 16 – 33 25 – 33 60, 66 150
Number of pins 40 68 132 168 273 387
Number of transistors 29,000 134,000 275,000 1.2 million 3.1 million 5.5 million
Physical memory 1MB 16MB 4GB 4GB 4GB 64GB
Virtual memory None 1GB 64TB 64TB 64TB 64TB
Internal data bus 16 16 32 32 32 32
External data bus 16 16 32 32 64 64
Address bus 20 24 32 32 32 36
Data types 8/16 8/16 8/16/32 8/16/32 8/16/32 8/16/32
In 1995, Intel introduced the Pentium Pro, the sixth generation of the x86 family.
Pentium Pro is an enhanced version of Pentium that uses 5.5 million transistors.
It was designed to be used for 32-bit servers and workstations.
3
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o In 1997, Intel introduced its Pentium II processor. This 7.5-million-transistor processor 'featured
MMX (Multi-Media extension) technology incorporated into the CPU. MMX allows for fast
graphics and audio processing.
o In 1998 the Pentium II Xcon processor was released. Its primary market is for servers and
workstations.
o In 1999 the Celeron was released. Its lower cost and good performance make it ideal for PCs used
to meet educational and home business needs.
o In 1999, Intel released the Pentium III. This 9.5-million-transistor processor includes 70 new
instructions called SIMD that enhance video and audio performance in such areas as 3-D
imaging, and streaming audio that have become common features of on-line computing. In 1999,
Intel also introduced the Pentium III Xeon processor, designed more for servers and business
workstations with multiprocessor configurations.
Table: Evolution of Intel’s Microprocessors (from the Pentium II to Itanium)
Product Pentium II Pentium III Pentium 4 Itanium II
Year introduced 1997 1999 2000 2002
Technology BICMOS BICMOS BICMOS BICMOS
Number of transistors 7.5 million 9.5 million 42 million 220 million
Cache size 512K 512K 512K 3MB
Physical memory 64GB 64GB 64GB 64GB
Virtual memory 64TB 64TB 64TB 64TB
Internal data bus 32 32 32 64
External data bus 64 64 64 64
Address bus 36 36 36 64
Data types 8/16/32 8/16/32 8/16/32 8/16/32/64
o The Pentium 4, which debuted late in 1999; had the speeds of 1.4 to 1.5 GHz. The Pentium 4
represents the first completely new architecture since the development of the Pentium Pro. The
new 32-bit architecture, called NetBurst, is designed for heavy multimedia processing such as
video, music, and graphic file manipulation on the Internet. The system bus operates at 400
MHz. In addition, new cache and pipelining technology and an expansion of the multimedia
instruction set are designed to make the P4 a high- end media processing microprocessor.
o Intel has selected Itanium as the new brand name for the first product in its 64-bit family of
processors, formerly called Merced. The evolution of microprocessors is increasingly
influenced by t h e evolu tion of the Internet. The Itanium architecture is designed to meet
4
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Internet-driven needs for powerful servers and high-performance work-stations. The Itanium will
have the ability to execute many instruc tions simultaneously plus extremely large memory
capabilities.
Pipelining:
There are two ways to make the CPU process information faster:
5
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
1. Increase the working frequency – The designers can make the CPU work faster by increasing
the frequency under which it runs. But, it is technology dependent, meaning that the designer
must use whatever technology is available at the time, with consideration for cost. The
technology and materials used in making ICs (integrated circuits) determine the working
frequency, power consumption and the number of transistors packed into a single-chip
microprocessor.
2. Change the internal architecture of the CPU – The processing power of the CPU can be altered
by changing the internal working of the CPU. (In 8085, the CPU had to fetch an instruction
from memory, then execute it and then fetch again, execute it, and so on; i.e., 8085 CPU could
either fetch or execute at a given time).
The idea of pipelining is to allow the CPU to fetch and execute at the same time as shown in
following Fig.
6
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
For example, when a jump instruction is executed, the BIU starts to fetch
information from the new location in memory and information in the queue that
was fetched previously is discarded. In this situation the EU must wait until the
BIU fetches the new instruction. This is referred to in computer science
terminology as a branch penalty. In a pipelined CPU, this means that too
much jumping around reduces the efficiency of a program.
Pipelining in the 8088/86 has two stages, fetch and execute, but in more powerful
computers, pipelining can have many stages. The concept of pipelining
combined with an increased number of data bus pins has, in recent years, led to
the design of very powerful microprocessors.
Registers:
In the CPU, registers are used to store information temporarily. Information could ne one or two bytes of
data to be processed or the address of the data. The registers of 8088/86 fall into six categories; as given
in the following Table.
Table: Register of 8088/86/286 by Category
Category Bits Register Names
16 AX, BX, CX, DX
General
8 AH, AL, BH, BL, VH, CL, DH, DL
Pointer 16 SP (Stack Pointer), BP (Base Pointer)
Index 16 SI (Source Index), DI (Destination Index)
Segment 16 CS (Code Segment), DS (Data Segment), SS (Stack Segment), ES (Extra Segment)
Instruction 16 IP (Instruction Pointer)
Flag 16 FR (Flag Register)
The general-purpose registers in 8088/86 can be accessed as either 16-bit or 8-bit registers. All other
registers can be accessed only as the full 16 bits. In 8088/86, data types are either 8 or 16 bits. To access
12-bit data, a 16-bit register must be used with the highest 4 bits set to 0.
7
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
BX as a base addressing register
CX as a counter in loop operations
DX to point to data in I/O operations.
o Today, one can use m a n y different programming languages, such as C/C++, BASIC, C#,
and numerous others. These languages are called high-level languages; because the
programmer does not have to be concerned with the internal details of the CPU.
o An assembler is used to translate an Assembly language program into machine code
(sometimes called object code); high- level languages are translated into machine code by a
program called a compiler. For instance, to write a program in C, one must use a C compiler to
translate the program into machine language.
o There are numerous assemblers available for translating x86 Assembly language programs into
machine code. M ost commonly used assemblers, MASM / TASM.
8
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The operands are the data items being manipulated, and the mnemonics are the commands to
the CPU, telling it what to do with those items.
E.g.:
Opcode (Mnemonic) Source operand (register
Relative addressing)
AGAIN: ADD AX, COUNT [BX] ; ADD ELEMENT OF COUNT TO AX.
MOV Instruction:
The MOV instruction copies data from one location to another. The format is –
The Following Figure shows the operation of the MOV BX, CX instruction.
The MOV instruction does not affect the source operand. The following program first loads CL with
value 55H, then moves this value around to various registers inside the CPU.
9
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
In 8086 CPU, data can be moved among all the registers (except the flag register) as long as the source
and destination registers match in size.
2. If a value less than FFH is moved into a 16-bit register, the rest of the bits are assumed to be all
zeros. E.g.: MOV BX, 5 ; result will be BX = 0005, i.e., BH = 00 and BL = 05.
3. Moving a value that is too large into a register will cause an error.
ADD Instruction:
The ADD instruction has the following format –
The ADD instruction tells the CPU to add the source and the destination operands and put the result in the
destination.
10
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Executing above program results in AL (or DH) = 59H (25H + 34H = 59H) and BL (or CL) = 34H.
Notice that, the contents of the source operand do not change.
It is not necessary to move both data items into registers before adding them together.
Hence, for MOV and ADD instructions, the source operand may be an immediate data – this is called an
immediate operand. Please note, the destination operand has always been a register.
The largest number that an 8-bit register can hold is FFH. To use numbers larger than FFH (255 decimal),
16-bit registers (such as AX, BX, CX, or DX) must be used.
Running the above program(s) give DX (or CX) = 9F3H (34E + 6A5 = 9F3H) and AX = 34EH.
11
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
in RAM or ROM within the 1M byte memory range. This address can have a range of 00000H –
FFFFFH for the 8086, and real mode 286, 386, and 486 CPUs.
2. The offset address – is a location within a 64K byte segment range. Hence, an offset address can
range from 0000H – FFFFH.
3. The logical address – consists of a segment value and an offset address.
12
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The program above shows that the byte at address 1132:0100 contains B0, which is the opcode for
moving a value into register AL, and address 1132:0I101I contains the operand (in this case 57) to be
moved to AL. Therefore, the instruction "MOV AL, 57” has a machine code of B057, where B0 is the
opcode and 57 is the operand.
13
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The following are the physical addresses and contents of each location for the above program.
Data Segment:
Assume that a program is being written to add 5 bytes of data, such as 25H, 12H, 15H, IFH, and 2BH.
One way to add them is as follows:
In the program above, the data and code are mixed together. The problem with wri ting the program
this way is that, if the data changes, the code must be searched for every place the data is included, and
the data retyped.
The idea to overcome the problem is to set aside an area of memory is strictly for data. I n x86
microprocessors, the area of memory set aside for data is called the data segment. Just as the code
segmen t is associated with CS and IP as its segment register and offset, the data segment uses register
DS and an offset value .
The following demonstrates how data can be stored in the data segment and the program rewritten so
that it can be used for any set of data. Assume that the offset for the data segment begins at 200H.
NOTE:
1. The offset address is enclosed in brackets. The brackets indicate that the operand represents the
address of the data and not the data itself. If the brackets were not included, as in "MOV AL,
0200", the CPU would attempt to move 200 into AL instead of the contents of offset address
200.
2. DEBUG assumes that all numbers are in hex (no "H" suffix is required), whereas
MASM/ T A S M assumes that they are in decimal and the "H" must be included for hex data.
14
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
This program will run with any set of data. Changing the data has no effect on the code. Although
this program is an improvement over the preceding one, it can be improved even further.
If the data had to be stored at a different offset address (say 450H), the program would have to
be rewritten. One way to solve this problem would be 'to use a register to hold the offset address, and
before each ADD, to increment the register to access the next byte.
The 8088/86 allows only the use of registers BX, SI, and DI as offset registers for the data
segment In other words, while CS uses only the IP register as an offset, DS uses only BX, DI, and SI
to hold the offset address of the data.
Table: Default Segments and Offset Register Pairs
Segment Offset Special Purpose
CS IP Instruction address
DS SI, DI, BX, an 8- or 16-bit number Data address
SS SP or BP Stack address
ES SI, DI, BX for string instructions String destination address
The term pointer is often used for a register holding an offset address. In the following example, BX is
used as a pointer.
The INC instruction adds 1 to (increments) its operand. "INC BX" achieves the same result as
"ADD BX, 1".
15
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
In this case, the low byte goes to the low memory location and the high byte goes to the high memory
location. In the above example, memory location DS: 1500 contains F3H and memory location DS: 1501
contains 35H (DS: 1500 = F3 and DS: 1501 = 35). This is called little endian conversion.
16
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
NOTE: In the big endian method, the high byte goes to the low address, where as in the little endian
method, the high byte goes to the high address and the low byte goes to the low address. All Intel
microprocessors use the little endian conversion.
17
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
memory up to 640K, if they needed additional memory. The need for expansion depends on the
Windows version being used and the memory needs of the application software being run.
The Win dows operating system first allocates the available RAM on the PC for its own use
and then lets the rest be used for applications such as word processors. The complicated task of
managing RAM memory is left to Windows, since the amount of memory used by Windows varies
among its various versions and the memory needs of the application packages vary. For this reason we
do not assign any values f:or the CS, D S , and SS registers; since such an assignment means
specifying an exact physical address in the range 00000-9FFFFH , and this is beyond the knowledge
of the user.
Another reason is that assigning a physical address might work on a given PC but it might not
work on a PC with a different OS version and RAM size. In other w ords, the program would not be
portable to another PC.
Therefore, memory management is one of the most important functions of the operating system
and should be left to Windows.
Video RAM:
From A0000H to BFFFFH is set aside for video. The amount used and the location vary depending on the
video board installed on the PC.
18
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
BIOS, which stands for basic input-output system, contains programs to test RAM and other
components connected to the CPU. It also contains pro grams that allow Windows to communicate with
peripheral devices such as the keyboard, video, printer, and disk.
It is the function of BIOS to test all the devices connected to the PC when the computer is turned
on and to report any errors. For example, if the keyboard is disconnected from the PC before the
computer is turned on, BIOS will report an error on the screen , indicating that condition.
After testing and setting up the peripherals; BIOS will load Windows from disk into RAM and
hand over control of the PC toWindows. Windows always controls the PC once it is loaded.
THE STACK:
What is Stack, and Why is it Needed?
o There must be some place for the CPU to store information safely and temporary. The stack is a
section of read/write memory (RAM) used by the CPU to store information temporarily.
o The CPU needs this storage area since there are only a limited number of registers.
o The disadvantage of the stack is its access time – since the stack is in RAM, it takes much longer
to access compared to the access time of registers. Note that, the registers are inside the CPU and
RAM is outside.
19
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o The reason that the SP is decremented after the push is to make sure that the stack is growing
downward from upper addresses to lower addresses. This is the opposite of the IP (instruction
pointer). As was seen in the preceding section, the IP points to the next instruction to be executed
and is incremented as each instruction is executed.
Notice, how the data is stored on the stack. In the x86, the lower byte is always stored in the memory
location with the lower address.
20
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
NOTE:
21
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
1. A single physical address may belong to many different logical addresses. This shows the
dynamic behavior of the segment and offset concept in the 8086 CPU.
2. When adding the offset to the shifted segment register; if an address beyond the maximum
allowed range (FFFFFH) is resulted, then wrap-around will occur.
3. In calculating the physical address, it is possible that two segments can overlap, as illustrated in
the following Fig.
22
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o The flag register is a 16-bit register sometimes referred to as the status register. Although the
register is 16 bits wide, only some of the bits are used. The rest are either undefined or reserved
by Intel.
o Six of the flags are called conditional flags, meaning that they indicate some condition that
resulted after an instruction was executed. These six are CF, PF, AF, ZF, SF, and OF.
o The three remaining flags are sometimes called control flags, since they are used to control the
operation of instructions before they are executed.
BIT 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0F DF IF TF SF ZF AF PF CF
23
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
IF, Interrupt En able Flag – This bit is set or cleared to enable or disable only the external
maskable interrupt requests.
DF, the Direction Flag – This bit is used to control the direction of string operations. If D = 1, the
registers are automatically decremented; if D = 0, the registers are automatically incremented. The state of
the D flag bit is controlled by STD (set D flag) and CLD (clear D flag) instructions.
OF, the Overflow Flag – This flag is set whenever the result of a signed number operation is too
large, causing the high-order bit to overflow into the sign bit. In general, the carry flag is used to
detect errors in unsigned arithmetic operations. The overflow flag is only used to detect errors in signed
arithmetic operations.
24
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
25
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The following example shows the implementation of the looping concept in the program,
which adds 5 bytes of data. Register CX is used to hold the counter and BX is the offset
pointer (SI or Dl could have been used instead). AL is initialized before the start of the loop.
In each iteration; ZF is checked by the JNZ instruction. JNZ stands for "Jump Not Zero"
meaning that, if ZF = 0, jump to a new address. If ZF = 1, the jump is not performed and the
instruction below the jump will be executed.
Notice that the JNZ instruction must come immediately after the instruction that decrements
CX since JNZ needs to check the effect of "DEC CX" on ZF. If any other instruction(s) were
placed between them, that instruction(s) might affect the zero flag.
26
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
1. Register AddressingMode
The register addressing mode involves the use of registers to hold the data to be manipulated.
Memory is not accessed when this addressing mode is executed; therefore, it is relati vely fast.
PA = DS : Direct Address
Notice the bracket around the address. In the absence of this bracket, executing the command will give an
error since it is interpreted to move the value 2400 (16-bit data) into register DL, an 8-bit register.
Before After
27
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Eg: MOV BX, [5634] BX ABCDH 8645H
Before After
DS:5634H 45H
DS:5635H 86H
Notice that BX is in brackets. In the absence of brackets, the code is interpreted as an instruction moving
the contents of register BX to AL (which gives an error because source and destination do not match);
instead of the contents of the memory location whose offset address is in BX. The physical address is
calculated by shifting DS left one hex position and adding BX to it. The same rules apply when using
register SI or DI.
28
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
PA = DS BX
or : or + 8 or 16 bit displacement
SS BP
Alternative codings are “MOV CX, [BX+10]” or “MOV CX, 10[BX]”. In the case of BP register –
PA = DS SI
or : or + 8 or 16 bit displacement
SS DI
29
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
PA = DS BX SI
or : or + or + 8 or 16bit displacement
SS BP DI
The coding of the instructions above can vary. The last example can also be written as –
Segment Overrides:
The following Table summarizes the offset registers that can be used with the four segment registers.
Table: Default Segments and Offset Register Pairs
Segment Offset Special Purpose
CS IP Instruction address
DS SI, DI, BX, an 8- or 16-bit number Data address
SS SP or BP Stack address
ES SI, DI, BX for string instructions String destination address
The x86 CPU allows the program to override the default segment and use any segment register. To do
that, one needs to specify the segment in the code.
For example, in "MOV AL, [BX]", the physical address of the operand to be moved into AL is
DS: BX. To override that default, specify the desired segment in the instruction as "MOV AL, ES: [BX]
". Now the address of the operand being moved to AL is ES: BX instead of DS: BX.
The following Table shows more examples of segment overrides shown next to the default address in the
absence of the override.
Table: Sample Segment Overrides
30
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
ASSEMBLY LANGUAGE PROGRAMMING
DIRECTIVES AND A SIMPLE PROGRAM:
A given Assembly language program (ALP) is a series of statements. There are two types of statements in
x86 ALP:
1. Assembly language instructions – instructions that are given to the microprocessor to do
the specific task. The Assembly language instruction can be translated into object code or
machine language. (E.g.: MOV, ADD, etc.)
2. Pseudo instructions/Directives – instructions that give directions to the assembler about
how it should translate the Assembly language instructions into machine code. These
instructions are not translated into machine code. They are used by the assembler to
organize the program as well as other output files. (E.g.: DB, DW, ASSUME, etc.)
An Assembly language instruction consists of four fields:
Brackets indicate that the field is optional; do not type the brackets.
E.g.:
Opcode (Mnemonic) Source operand (register
Relative addressing)
AGAIN: ADD AX, COUNT [BX] ; ADD ELEMENT OF COUNT TO AX.
1. The label field allows the program to refer to a line of code by name. The label field cannot exceed 31
characters. Labels for directives do not need to end with a colon. A label must end with a colon when it
refers to an opcode generating instruction; the colon indicates to the assembler that this refers to code
within this code segment.
2, 3. The Assembly language mnemonic (instruction) and operand(s) fields together perform the real work
of the program and accomplish the tasks for which the program was written. In Assembly language
statements such as ADD AL, BL or MOV AX, 6764; ADD and MOV are mnemonic opcode, and “AL, BL”
and “AX, 6764” are the operands.
4. The comment filed begins with a “;”. The assembler ignores comments. The comments are optional,
but are highly recommended for someone to read and understand the program.
31
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Model Definition:
The first statement in an Assembly language program is the MODEL directive. This directive selects the
size of the memory model. Among the options for the memory model are SMALL, MEDIUM,
COMPACT, and LARGE.
•MODEL SMALL ; this directive defines the model as small
SMALL is one of the most widely used memory models for Assembly language programs This model
uses a maximum of 64K bytes of memory for code and another 64KB for data. The other models are
defined as follows:
Segment Definition:
The x86 CPU has four segment registers: CS (code segment), DS (data segment), SS (stack segment), and
ES (extra segment). Every line of an Assembly language program must correspond to one of these
segments. The simplified segment definition format uses three simple directives: ".CODE", ".DATA",
and ".STACK", which correspond to the CS, DS, and SS registers, respectively.
Segments of a Program:
Although one can write an Assembly language program that uses only one segment, normally a program
consists of at least three segments: the stack segment, the data segment, and the code segment.
Assembly language statements are grouped into segments in order to be recognized by the assembler and
consequently by the CPU.
The stack segment defines storage for the stack
The data segment defines the data that the program will use
The code segment contains the Assembly language instructions.
32
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
33
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
No segment register can be loaded directly. Hence, two lines are required, as shown above.
END – directive ends the entire program by indicating to OS that the entry point MAIN has ended. The
label for the entry point (MAIN, here) and the END must match.
34
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
• The list file (.lst) lists all the opcodes and the offset addresses, as well as errors that the
assembler detected. This file can be displayed on the monitor by the command: C>type
myfile.lst | more.
• The cross-reference file (.crf) provides an alphabetical list of all symbols and tables used
in the program as well as program line numbers in which they are referenced.
o The object file (.obj) is the input for the LINK program, which produces the executable program
(.exe). The LINK program sets up the file, so that, it can be loaded by the OS and executed.
o We use DEBUG to execute the program and analyze the results.
• When the program is working successfully, it can be run at the OS level by typing the
command: C>myfile. When the program name is typed in at the OS level, the OS loads
the program in memory. This is referred as mapping; which means that the program is
mapped into the physical memory of the PC.
• When there are many segments for code or data, there is a need to see where each is
located and how many bytes are used by each. The “.map” file gives the name of each
segment, where it starts, where it stops, and its size in bytes.
Fig: Steps to Create a Program & Creating and Running the .exe File
35
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The PAGE directive tells the printer how the list should be printed. In the default mode, the output will
have 66 lines per page and with a maximum of 80 characters per line. The default settings can be altered
to 60 and 132 as follows:
When the list is printed in more than one page, the assembler can be instructed to print the title of the
program on the top of each page by using the TITLE directive. The text after the TITLE pseudo-
instruction cannot be more than 60 ASCII characters.
36
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
DEC destination – subtract 1 from the specified destination. The destination may be a register or
a memory location.
Flags affected: AF, OF, PF, SF, and ZF. The CF is not affected.
Eg: DEC AL ; Subtract 1 from the contents of AL.
JNZ label – jump if not zero; if ZF = 0, jumps to the label specified. Checks for zero flag.
37
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
38
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
OFFSET: It is an operator which tells the assembler to determine the offset or displacement of a named
data item (variable) from the start of the segment.
Eg: MOV AX, OFFSET MES1 ; Loads the offset of variable MES1 in AX register.
The ORG directive can be used to set the offset addresses for data items. In the above program, the ORG
directive causes SUM to be stored at DS: 0010.
39
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
40
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
• In a NEAR jump, the IP is updated and CS remains the same, since control is still inside
the current code segment.
o If control is transferred to a memory location outside the current code segment, it is a FAR or
intersegment (between segments) jump.
• In a FAR jump, because control is passing outside the current code segment, both CS and
IP have to be updated to the new values.
Conditional Jumps:
In the conditional jump, control is transferred to a new location if a certain condition is met. The flag
register is the one that indicates the current condition. For example, with "JNZ label", the processor looks
at the zero flag to see if it is raised. If not, the CPU starts to fetch and execute instructions from the
address of the label. If ZF = I, it will not jump but will execute the next instruction below the JNZ.
Table: 8086 Conditional Jump Instructions
Short Jumps:
o All conditional jumps are short jumps. In a short jump, the address of the target must be within –
128 to +127 bytes of the IP.
o The conditional jump (short jump) is a two byte instruction: One byte is the opcode of the J
condition and the second byte is a value between 00 and FF.
41
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o An offset range of 00 to FF gives 256 possible addresses; these are split between backward jumps
(to –128) and forward jumps (to +127).
o In a jump backward, the second byte is the 2's complement of the displacement value. To
calculate the target address, the second byte is added to the IP of the instruction after the jump.
o The instruction "JNZ AGAIN" was assembled as "JNZ 000D", and 000D is the address of the
instruction with the label AGAIN. The instruction "JNZ 000D" has the opcode 75 and the target
address FA, which is located at offset addresses 0011 and 0012.
o This is followed by "MOV SUM, AL", which is located beginning at offset address 0013. The IP
value of this MOV (0013), is added to FA to calculate the address of label AGAIN (0013+ FA=
000D) and the carry is dropped.
o In reality, FA is the 2's complement of -6, meaning that the address of the target is -6 bytes from
the IP of the next instruction.
o Similarly, the target address for a forward jump is calculated by adding the IP of the following
instruction to the operand. In that case the displacement value is positive, as shown next.
42
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
o In the program above, "JB NEXT" has the opcode 72 and the target address 06 and is located at
IP = 000A and 000B.
o The jump will be 6 bytes from the next instruction, which is IP = 000C. Adding gives us 000CH
+ 0006H = 0012H, which is the exact address of the NEXT label.
o Look also at "JA NEXT", which has 77 and 02 for the opcode and displacement, respectively.
The IP of the following instruction, 0010, is added to 02 to get 0012, the address of the target
location.
Note that, regardless of whether the jump is forward or backward, for conditional jumps, the address of
the target address can never be more than –128 to +127 bytes away from the IP associated with the
instruction following the jump lf any attempt is made to violate this rule, the assembler will generate a
"relative jump out of range" message. These conditional jumps are sometimes referred to as SHORT
jumps.
Unconditional Jumps:
"JMP label" is an unconditional jump in which control is transferred unconditionally to the target
location label. The unconditional jump can take the following forms:
1. SHORT JUMP – which is specified by the format "JMP SHORT label". This is a jump in which
the address of the target location is within –128 to +127 bytes of memory relative to the address
of the current IP.
In this case, the opcode is EB and the operand is 1 byte in the range 00 to FF. The
operand byte is added to the current IP to calculate the target address. If the jump is
backward, the operand is in 2's complement. This is exactly like the J condition case.
Coding the directive "short" makes the jump more efficient; i.e., it will be assembled into
a 2-byte instruction instead of a 3-byte instruction.
2. NEAR JUMP, which is the default, has the format "JNP label". This is a near jump (within the
current code segment) and has the opcode E9. The target address can be any of the addressing
modes of direct, register, register indirect, or memory indirect:
(a) Direct JUMP: is exactly like the short jump explained earlier, except that the target
address can be anywhere in the segment within the range +32767 to –32768 of the
current IP.
(b) Register indirect JUMP: the target address is in a register. For example, in "JMP
BX", IP takes the value BX.
43
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
(c) Memory indirect JMP: the target address is the contents of two memory locations
pointed at by the register. Example: "JMP [DI]" will replace the IP with the contents of
memory locations pointed at by DI and DI + 1.
3. FAR JUMP, which has the format "JMP FAR PTR label". This is a jump out of the current code
segment, meaning that not only the IP but also the CS is replaced with new values.
CALL Statement:
o Another control transfer instruction is the CALL instruction, which is used to call a procedure.
CALLs to procedures are used to perform tasks that need to be performed frequently. This makes
a program more structured.
o The target address could be in the current segment, in which case it will be a NEAR call or
outside the current CS segment, which is a FAR call.
o To make sure that after execution of the called subroutine the microprocessor knows where to
come back, the microprocessor automatically saves the address of the instruction following the
call on the stack. It must be noted that in the NEAR call only the IP is saved on the stack, and in a
FAR call both CS and IP are saved.
o When a subroutine is called, control is transferred to that subroutine and the processor saves the
IP (and CS in the case of a FAR call) and begins to fetch instructions from the new location.
o After finishing execution of the subroutine, for control to be transferred back to the caller, the last
instruction in the called subroutine must be RET (return). The RET instruction in the case of
NEAR and FAR is different. For NEAR calls, the IP is restored; for FAR calls, both CS and IP
are restored.
o This will ensure that control is given back to the caller. As an example, assume that SP = FFFEH
and the following code is a portion of the program unassembled in DEBUG:
44
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
45
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The first character of the name must be an alphabetic character or special character. It cannot be a
digit.
Names may be up to 31 characters long.
46
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
The following are some of the data directives used by the x86 microprocessor and supported by all
software vendors.
ORG (origin) – is used to indicate the beginning of the offset address. The number that comes
after ORG can be either in hex or in decimal. If the number is not followed by H, it is decimal
and the assembler will convert it to hex.
DB (define byte) – directive allows allocation of memory in byte-sized chunks. This is indeed the
smallest allocation unit permitted. DB can be used to define numbers in decimal, binary, hex, and
ASCII. For decimal, the D after the decimal number is optional, but using B (binary) and H
(hexa- decimal) for the others is required. Regardless of which one is used, the assembler will
convert numbers into hex. To indicate ASCII, simply place the string in single quotation marks
('like this'). Either single or double quotes can be used around ASCII strings.
DUP (duplicate) – is used to duplicate a given number of characters. This can avoid a lot of
typing. For example, contrast the following two methods of filling six memory locations with
FFH:
DW (define word) – is used to allocate memory 2 bytes (one word) at a time. The following are
some examples of DW:
47
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
EQU (equate) – is used to define a constant without occupying a memory location. EQU does not
set aside storage for a data item but associates a constant value with a data label so that when the
label appears in the program; its constant value will be substituted for the label.
o EQU can also be used outside the data segment, even in the middle of a code segment.
Using EQU for the counter constant in the immediate addressing mode:
COUNT EQU 25 COUNT DB 25
When executing the instructions "MOV CX, When executing the same instruction "MOV CX,
COUNT", the register CX will be loaded with the COUNT" it will be in the direct addressing mode.
value 25.
What is the real advantage of EQU? First, note that EQU can also be used in the data segment:
COUNT EQU 25
COUNTER1 DB COUNT
COUNTER2 DB COUNT
Assume that there is a constant (a fixed value) used in many different places in the data and code
segments. By the use of EQU, one can change it once and the assembler will change all of them, rather
than making the programmer tries to find every location and correct it.
DD (define double word) – directive is used to allocate memory locations that are 4 bytes (two
words) in size. Again, the data can be in decimal, binary, or hex. In any case the data is converted
to hex and placed in memory locations according to the rule of low byte to low address and high
byte to high address. DD examples are:
DQ (define quad word) – is used to allocate memory 8 bytes (four words) in size. This can be
used to represent any variable up to 64 bits wide:
48
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
DT (define ten bytes) – is used for memory allocation of packed BCD numbers. The application
of DT will be seen in the multibyte addition of BCD numbers. For now, observe how they are
located in memory. Notice that the "H" after the data is not needed. This directive allocates 10
bytes, but a maximum of 18 digits can be entered.
It is essential to understand the way operands are stored in memory. The following Fig shows the memory
dump of the data section, including all the examples discussed here.
Looking at the memory dump shows that, all of the data directives use the little endian format for storing
data (the least significant byte is located in the memory location of the lower address and the most
significant byte resides in the memory location of the higher address).
For example, look at the case of "DATA20 DQ 4523C2", residing in memory starting at offset
00C0H. C2, the least significant byte, is in location 00C0, with 23 in 00C1, and 45, the most significant
byte, in 00C2. It must also be noted that for ASCII data, only the DB directive can be used to define data
of any length, and the use of DO, DQ, or DT directive for ASCII strings of more than 2 bytes gives an
assembly error. When DB is used for ASCII numbers, notice how it places them backwards in memory.
For example, see “DATA4 DB „2591‟ ” at origin 10H: 32, ASCII for 2, is in memory location 10H; 35,
ASCII for 5, is in 11H; and so on.
49
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
FULL SEGMENT DEFINITION:
The way that segments have been defined in the programs above is a newer definition referred to as
simple segment definition. It is supported by Microsoft's MASM 5.0 and higher and/or Borland's TASM
version 1 and higher. The older, more traditional definition is called the full segment definition.
Segment Definition:
In the full segment definition, the ".MODEL" directive is not used. Further, the directives "
.STACK",".DATA", and" .CODE" are replaced by SEGMENT and ENDS directives that
surround each segment.
The SEGMENT and the ENDS directives indicate to the assembler the beginning and ending of a
segment and have the following format:
The label, or name, must follow naming conventions and must be unique.
The [options] field gives important information to the assembler for organizing the segment, but
is not required.
The ENDS label must be the same label as in the SEGMENT directive.
The following Fig shows the full segment definition and simplified format, side by side.
50
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Stack Segment Definition:
The stack segment shown below contains the line: "DB 64 DUP (?)" to reserve 64 bytes of memory for
the stack. The following three lines in full segment definition are comparable to ".STACK 64" in simple
definition:
Example:
51
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
If an extra segment had been used, ES would also be included in the ASSUME statement.
The ASSUME statement is needed because a given Assembly language program can have several
code segments; one or two or three or more data segments and more than one stack segment. But
only one of each can be addressed by the CPU at a given time; since, only one of each of the
segment registers available inside the CPU.
ASSUME tells the assembler which of the segments defined by the SEGMENT directives should
be used.
Fig: emu8086
NOTE: emu8086 requires putting brackets around variables, unlike MASM/TASM.
52
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
EXE vs COM Files:
All program examples so far were designed to be assembled and linked into EXE files. The COM file,
similar to the EXE file, contains the executable machine code and can be run at the OS level.
53
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
2. Using comments within the program and documentation accompanying the program also will
help someone else to know what the program does. It may even help the programmer who wrote
the program remember how it worked years later!
3. The main routine should consist of calls to subroutines that perform the work of the program.
This is sometimes called top-down programming. Use subroutines to accomplish tasks that are
repeated. This saves time in coding and also makes the program easier to read.
4. Data control is very important. It can be very frustrating and time consuming to track through a
long program to find where a variable was changed. First of all, the programmer should document
the purpose of each variable, and which subroutines might alter its value. Further, each subroutine
should document its input and output variables, and which input variables might be altered within
it.
Flow Charts & Pseudocode:
Flowcharts use graphic symbols to represent different types of program operations. These symbols are
connected together into a flowchart to show the flow of execution of the program.
54
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
Control Structures:
Structured programming used three basic types of program control structures –
1. Sequence
55
MICROPROCESSORS AND MICROCONTROLLERS
15CS44
____________*********____________
*********
56