x86 Proccessors
x86 Proccessors
Figure 1-5 ASCII code. Glenn A. Gibson, YuCheng Liu, MICROCOMPUTERS FOR ENGINEERS AND SCIENTISTS 1980, Prentice-Hall, Inc.
3
As an example, the character string
Space
44
D
4F 45 2C 0D 0A 4A 4F 48 4E 20 50 2E 2D 35 30
O E , J O H N P . 5 0
4
Figure 1-9 Typical CPU architecture. Glenn A. Gibson, James R. Young, INTRODUCTION TO PROGRAMMINGUSING FORTRAN 77, 1982, p. 16. Reprinted by permission of Prentice-Hall, Inc., Englewood Cliffs, NJ.
5
Figure 1-10 Instruction sequencing.
6
Figure 2-1 8086 pin assignments. (Reprinted by permission of Intel Corporation, Copyright 1981.)
CPU 8088
7
Figure 2-2 8086s internal configuration.
In addition to serving as arithmetic registers, the BX, the CX and the DX registers play special addressing, counting, and I/O roles:
BX may be used as a base register in address calculations. CX is used as an implied counter by certain instructions. DX is used to hold the I/O address during certain I/O operations.
+5V
+12V -5V
10
11
D4 D7 A
D0 D3
A10
A11
A12 A A13 A14 A15
MEMR MEMW
D4 D7 A
D0 D3
12
13
14
15
157BB
16
17
Figure 2-5 Separation of a program's code, its data, and its stack.
18
19
Overflow Flag
Carry Flag
Direction Flag
21
SF (Sign Flag)
Is equal to the MSB of the result. Since in 2's complement negative numbers have a 1 in the MSB and for nonnegative numbers this bit is 0, this flag indicates whether the previous result was negative or nonnegative.
22
ZF (Zero Flag)
Is set to 1 if the result is zero and 0 if the result is nonzero.
23
PF (Parity Flag)
Is set to 1 if the low-order 8 bits of the result contain an even number of 1s; otherwise it is cleared.
24
CF (Carry Flag)
An addition causes this flag to be set if there is a carry out of the MSB, and a subtraction causes it to be set if a borrow is needed. Other instructions also affect this flag and its value will be discussed when these instructions are defined.
25
26
OF (Overflow Flag)
Is set if an overflow occurs, i.e., a result is out of range. More specifically, for addition this flag is set when there is a carry into the MSB and no carry out of the MSB or vice versa. For subtraction, it is set when the MSB needs a borrow and there is no borrow from the MSB, or vice versa. As an example, if the previous instruction performed the addition 0010 0011 0100 0101 + 0011 0010 0001 1001 0101 0101 0101 1110 then following the instruction: SF=0 ZF=0 PF=0 CF=0 AF=0 OF=0
11
27
DF (Direction Flag)
Used by string manipulation instructions. If clear, the string is processed from its beginning with the first element having the lowest address. Otherwise, the string is processed from the high address towards the low address.
10
28
29
TF (Trap Flag)
If set, a trap is executed after each instruction.
30
Fetching the next instruction from the address indicated by the PC.
Putting it in the instruction register and decoding it while the PC is incremented to point to the next instruction. Executing the instruction and, if a branch is to be taken, resetting the PC to the branch address. Repeating steps 1 through 3. The operation of the 8086 follows this basic pattern, but there are differences and some of the operations may be overlapped.
31
Figure 2-9 Filling the instruction queue after a branch
Figure 2-9(a) shows how the queue is filled by a sequence of the form: 1-byte instruction. 2-byte instruction. 3-byte instruction.
32
Immediate
The datum is either 8 bits or 16 bits long and is part of instruction. Direct The 16-bit effective address of the datum is part of the instruction. Register The datum is in the register that is specified by the instruction. For a 16-bit operand, a register may be AX, BX, CX, DX, SI, DI, SP, or BP, and for an 8-bit operand a register may be AL, AH, BL, BH, CL, CH, DL or DH.
*EA is added to 1610 times the contents of the appropriate segment register
33
Register Relative
The effective address is the sum of an 8- or 16-bit displacement and the contents of a base register of an index register, i.e.,
*EA is added to 1610 times the contents of the appropriate segment register
34
of an 8- or 16-bit displacement
and a based indexed address, i.e.,
*EA is added to 1610 times the contents of the appropriate segment register
35
Direct: EA = 1B57 Physical address = 1B57 + 21000 = 22B57 Register: No effective address - datum is in specified register. Register indirect assuming register BX: EA = 0158 Physical address = 0158 + 21000 = 21158
36
37
*EA is added to 1610 times the contents of the appropriate segment register
38
Intersegment Direct Replaces the contents of IP with part of the instruction and the contents of CS with another part of the instruction. The purpose of this addressing mode is to provide a means of branching from one code segment to another.
*EA is added to 1610 times the contents of the appropriate segment register
39
*EA is added to 1610 times the contents of the appropriate segment register
40
Displacement = 20A1
the effective branch address is the contents of: 20A1 + (DS)*1610 With register relative addressing assuming register BX, the effective branch address is the contents of: 1256 + 20A1 + (DS)*1610 With based indexed addressing assuming registers BX and SI, the effective branch address is the contents of: 1256 + 528F + (DS)*1610
41
Instruction length
The op code/addressing mode byte(s) may be followed by:
- No additional bytes. - A 2-byte EA (for direct addressing only.) - A 1- or 2-byte displacement. - A 1- or 2-byte immediate operand. - A 1- or 2-byte displacement followed by a 1- or 2-byte immediate operand. - A 2-byte displacement and a 2-byte segment address (for direct intersegment addressing only).
Which of these possibilities is used is determined by the op code and addressing mode. The op code usually occupies the first byte, and only the first byte, of an instruction, but there are a few instructions in which a register designation is in the first byte and a few other instructions in which 3 bits of the op code are in the second byte. Within most of the op codes there are special 1-bit indicators.
42
W-bit If an instruction can operate on either a byte or a word, the op code includes a W-bit which indicates whether a byte (W = 0) or a word (W = 1) is being accessed. D-bit
For double-operand instructions (except for instructions with one operand being immediate and string instructions, which are discussed in Chap. 5).
One of the operands must be a register specified by a REG field. For these instructions the D-bit is used to indicate whether the register specified by REG is the source operand (D = 0) or the destination operand (D = 1).
43
44
V-bit Used by shift and rotate instructions to determine the number of shifts (see Chap. 3). Z-bit Used by the REP instruction (which is discussed in Chap. 5). Register designation is 2 bits long if it is for a segment register and 3 bits long if it is for any other type of register.
REG Register MOD Mode R/M Register to memory DISP Displacement DATA Immediate data
(if 16-bit data are used) Immediate operand to memory with 16-bit displacement
REG Register MOD Mode R/M Register to memory DISP Displacement DATA Immediate data
If there are two op code/addressing mode bytes, then the second byte is of one of the following two forms:
or
The first of these forms is for single-operand instructions (or instructions involving two operands with one of them being implied by the op code), and the second is for double-operand instructions, in which case REG specifies a register that is the source operand or destination operand depending on the value of the D-bit.
To permit exceptions to the segment register usage given in Fig. 2-15. a special 1-byte instructions called a segment override prefix is available. A segment override prefix has the form:
If an instruction is preceded by a segment override prefix, the segment register REG is used for data reference during the execution of that instruction. For the addressing modes given in the figure, DS may be overridden by CS, SS, or ES; and when BP is used, SS may be overridden by DS, CS, or ES.
50 Address modes and default segment registers for various MOD and R/M field combinations
000000DW
MOD
REG
R/M
Low-order DISP
High-order DISP
(a) Add register to register or memory and store results in register or memory
Optional, depending on MOD field Optional, present if S:W=01 Low-order DATA High-order ATA
000000SW
MOD
REG
R/M
Low-order DISP
High-order DISP
(b) Add immediate to register( memory) and put results in register (memory)
Optional, present if S:W=01 0000010W Low-order DATA High-order DATA
(c) Add immediate with AX(AL) and store results in AX(AL) special case of accumulator
Figure 2-17 shows the machine language code for two ADD instructions, both of which add the contents of register BH to the contents of CL and put the result in CL.
Figure 2-17 Two equivalent instructions for adding the contents of the BH register to those of the CL register.
00000010
Byte operands (8-bit addition)
11001111
00000000
Byte operands
11111001
53 Example: ADD instruction (3) Figure 2-18(a) shows an instruction that uses the relative based indexed addressing mode to add a memory location with a register. From D=0 it is seen that the sum is put in the memory location and W=1 indicates a 16-bit addition. The effective address is found by adding the contents of BX and DI to the 16-bit displacement, which is 2345. If (BX) = 0892 and (DI) = 59A3, then
00000001
Word operands
10010001
01000101
10100011
01914523
Displacement
Figure 2-18(a) Register to memory addition.
Figure 2-18(b) shows a similar instruction except that the source operand is immediate. In this instruction S=1 and W=1, which indicate that the 8-bit immediate operand is sign extended to FF97 before it is added. An equivalent instruction could be constructed of 6 bytes by letting S:W = 01 and by including a 16-bit immediate operand containing 97FF. Sign extend 8-bit operand Part of op code
10000011
Word operands
10000001
01000101
10100011
10010111 = 8381452397
Displacement
The long and short forms of an instruction for adding an immediate operand to the AX register are given in Fig. 2-19.
Because S:W = 01 there are 2 bytes of data in the instruction. In the long form AX is explicitly designated by the R/M field and in the short form AX is implied by the op code. Short forms will be discusses more thoroughly in Sec. 3-12.
16-bit data Part of op code
00000001
Word operands
10010001
01000101
10100011
81C02301
16-bit data
00000101
Word operands
00100011
00000001
052310
Data = 0123h
Figure 2-19 Two forms for adding an immediate operand to the AX register.
Instruction
No. of Transfers
MUL (unsigned multiply) 8-bit register multiplier 16-bit register multiplier 8-bit memory multiplier 16-bit memory multiplier IMUL (signed multiply) 8-bit register multiplier 16-bit register multiplier 8-bit memory multiplier 16-bit memory multiplier 80-98 128-154 (86-104)+EA (134-160)+EA 0 0 1 1 70-77 118-133 (76-83)+EA (124-139)+EA 0 0 1 1
Instruction DIV (unsigned divide) 8-bit register divisor 16-bit register divisor 8-bit memory divisor 16-bit memory divisor IDIV (signed divide) 8-bit register divisor 16-bit register divisor 8-bit memory divisor 16-bit memory divisor Shift and rotate instructions Single-bit register Variable-bit register Single-bit memory Variable-bit memory
No. of Transfers
80-90 144-162 (86-96)+EA (150-168)+EA 101-112 165-184 (107-118)+EA (171-190)+EA 2 8+4/bit 15+EA 20+EA+4/bit
0 0 1 1 0 0 1 1 0 0 2 2
Instruction
No. of Transfers
JMP (unconditional branch) Short Intrasegment direct Intersegment direct Intrasegment indirect using register mode Intrasegment indirect Intersegment indirect Conditional branch instructions JCXZ All other conditional branch instructions
0 0 0 0 1 2
0 0
EA
Direct
Register indirect Register relative Based indexed (BP)+(DI) or (BX)+(SI)
6
5 9
(BP)+(SI) or (BX)+(DI)
Based indexed relative (BP)+(DI)+DISP or (BX)+(SI)+DISP (BP)+(SI)+DISP or (BX)+(DI)+DISP
11 12
For example, if the clock has a frequency of 5 MHz (its period is 0.2 ms), then execution times for various forms of the ADD instruction can be computed as follows: Add register to register (result put in register) requires: Three clock cycles for either a byte or word operand Time = 0.6 ms Add memory to register using based indexed relative addressing (result put in register) requires: 9 + 12 = 21 cycles for byte or word operation with word at an even address Time = 4.2 ms
The tokens may be variable identifiers or: Constant A number whose base is indicated by a suffix as follows: B - binary D - decimal O - octal H - hexadecimal The default is decimal. The first digit in a hexadecimal number must be 0 through 9; therefore, if the most significant digit is a letter (A-F), then it must be prefixed with a 0. Examples are: 10112 = 1011B 22310 = 223D = 223 B25A16 = 0B25AH
String Constant
A character string enclosed in single quotes ('). Arithmetic operators
66
67
Also, if appropriate directives are used to define COST to be a word variable and COUNT to be a byte variable, then INC COST will be a word operation, and INC COUNT will be a byte operation.
69 Typical assembler language instruction (5) For some situations, however, it is impossible for the assembler to deduce the operand type. The instruction INC [BX] increments the quantity whose address is in BX, but should it increment a byte or a word? One of the purposes of the PTR operator is to specify the length of a quantity in this and other ambiguous situations. It is applied by writing the desired type followed by PTR. For the above INC instruction the PTR operator would be used to modify the operand as follows: INC BYTE PTR [BX] if a byte is to be incremented, or INC WORD PTR [BX] if a word is to be incremented.
70
Figure 3-5 Glossary of symbols and abbreviations
71
DATA INSTRUCTIONS
There are four basic 8086 instructions for transferring quantities to and/or from the registers and memory; they are the MOV, LEA, LDS, and LES instructions. Flags: None of the flags are affected. Addressing modes:
The destination cannot be immediate and cannot be CS. For the LEA, LDS and LES instruction REG cannot be a segment register and the source mode cannot be the immediate or register modes. For MOV, unless the source operand is immediate, one of the operands must be a register. For XCHG, at least one of the operands must be a register, but neither operand can be a segment register.
Name
Description
Move
Load effective address Load DS with pointer Load ES with pointer Exchange
MOV
LEA LDS LES
DST, SRC
REG, SRC REG, SRC REG, SRC
(DST)
(REG) (REG) (DS) (REG) (ES) (OPR1)
(SRC)
(SRC) (SRC) (SRC + 2) (SRC) (SRC + 2) (OPR2)
72
The MOV instruction is for moving a byte or word within the CPU or between the CPU and memory.
Depending on the addressing modes it can transfer information from a:
Register to a register. Immediate operand to a register. Immediate operand to a memory location. Memory location to a register. Register to a memory location. Register/memory location to a segment register (except CS). Segment register to a register/memory location.
73
74
75
76
LEA instruction
77
The LDS and LES instructions are the same except that the former loads the DS register from memory and the latter loads ES from memory. Both instructions also load a second nonsegment register from memory and neither instruction affects the flags. Typical LDS and LES instructions are:
78
Arithmetic operations
Figure 3-11 Summary of the arithmetic operations that are directly implemented by 8086 instructions
79
80
Addressing modes:
Unless the source operand is immediate, one of the operands must be in a register. The other may have any addressing mode.
Mnemonic and Format ADD ADC SUB SBB DST, SRC DST, SRC DST, SRC DST, SRC
Description
(DST) (DST) (DST) (DST) (SRC) + (DST) (SRC) + (DST) + (CF) (SRC) - (DST) (SRC) - (DST) - (CF)
81
82
83
Flags: None of the flags are affected. Addressing modes: Operand must be in AL or AX.
Description
Extend sign of AL to AH
CWD
Extend sign of AX to DX
84
Addressing modes:
Mnemonic and Format INC DEC NEG CMP OPR OPR OPR OPR1, OPR2
(OPR1) - (OPR2)
85
Name
Description
Byte operands: (AX) Word operands: (DX : AX) Product is signed. (AL) * (SRC) (AX) * (SRC)
IMUL MUL
SRC SRC
Same as IMUL except that the operands and product are unsigned Byte divisor: (AL) Quotient of (AX) / (SRC) (AH) Remainder of (AX) / (SRC) Word divisor: (AX) Quotient of (DX : AX) / (SRC) (DX) Remainder of (DX : AX) / (SRC) Quotient and remainder are signed with the sign of the remainder being the sign of the dividend. Same as IDIV except that the operands, quotient, and remainder are unsigned.
Signed divide
IDIV
SRC
Unsigned divide
DIV
SRC
86
87
Noting that ad216 and bc216 are equivalent to ad and bc followed by sixteen 0 bits, and ac232 is ac followed by thirty-two 0 bits, the product can be found by: 1. Computing bd and storing the low-order word as the low-order word of the product. 2. Computing ad, adding the high-order word of bd to the low-order word of ad, and adding the carry to the high-order word of ad. 3. Computing bc and adding it to the result of step 2 using double-precision addition. The carry is stored for use in step 5. 4. Storing the low-order word of the result of step 3 as the next-to-low-order word of the product. 5. Computing ac, adding the high-order word of the result of step 3 to the low-order word of ac, and adding the carries, including the carry from step 3, to the high-order word of ac. 6. Storing the double word resulting from step 5 as the two high-order words of the product.
88
89
For example:
Essentially, the rule is needed to "skip over" the six bit combinations that are unused by the BCD format whenever such a skip is warranted.
90
91
92
93
94
95
where the second byte gives an 8-bit signed (2's complement) displacement relative to the address of the next instruction in sequence.
Figure 3-29 Correspondence between branch distances, values of D8, and branch addresses
96
As an example of how the assembler determines the value of D8, consider the following sequence: 0050 AGAIN: INC CX 0052 ADD AX,(BX) 0054 JNS AGAIN 0056 NEXT: MOV RESULT,CX where the column on the left gives the effective address of the first byte of each instruction. Because 0050 -0056 -6 Effective branch address (IP) when JNS branch decision is made
97
98
99
100
101
Name Intrasegment direct short branch Intrasegment direct near branch Intrasegment indirect branch Intersegment direct (far) branch Intersegment indirect branch
Mnemonic and Format JMP SHORT OPR JMP NEAR PTR OPR JMP OPR* JMP FAR PTR OPR JMP OPR*
Description (IP) (IP) (IP) (IP) (CS) (IP) (CS) (IP)+sign extended D8 determined by OPR (IP)+16-bit displacement determined by OPR (EA) where EA is determined by OPR Offset of OPR within segment Segment address of segment containing OPR (EA) where EA is determined by OPR (EA+2) where EA is determined by OPR
Addressing modes: For intrasegment direct branches the mode is relative and for intersegment direct branches the mode is direct. Indirect branches cannot involve immediate modes and a memory addressing mode must be used in intersegment indirect branches.
102
103
LOOP INSTRUCTIONS
Post-test loops are most often constructed as shown in Fig. 3-38.
104
The loop instructions are designed to simplify the decrementing, testing, and branching portion of the loop. From the definition of the LOOP instruction it is seen that the above post-test loop implementation could be simplified to:
The loop instructions for the 8086 all have the form:
105
*Except for JCXZ which leaves (CX) unchanged, (CX) (CX)-1. Then if test condition is met, (IP) (IP) + sign extended D8; otherwise IP are unchanged and the program continues in sequence
106
107
108
109
110
is relatively inflexible because, in order to insert new instructions at the point labeled EXIT, the move instruction must be retyped. If the sequence
were used, insertions could be made without disturbing the present code. This is important during the debugging phase when message printout code may need to be temporarily included at key points (which are often "branch to" points) within.
111
2,4,6 and 7 are transferred according to Fig. 2-8. Bits 1,3 and 5 are indeterminate.
Flags: Only the indicated flags are affected. Addressing modes: None.
112
LOGICAL INSTRUCTIONS
The 8086 instructions for performing logical operations are defined in Fig. 3-47. All of the instructions operate bitwise on their operands, which may be one byte or one word in length.
Flags: NOT does not affect flags. The other four instructions clear CF and OF, leave AF undetermined, and set SF,ZF and PF according to usual rules. Addressing modes: The NOT operand cannot be immediate. For the remaining instructions, unless the source operand is immediate, at least one of the operands must be a register. The other operand may have any addressing modes.
113
114
Figure 3-48 Example of selectively setting, changing, clearing, and testing bits
115
116
117
The w-bit serves the usual purpose of identifying whether a byte or word is to be operated on by the instruction. The v-bit is set to 0 if the shift count is to be 1 and is set to 1 if the CL register contains the shift count. The three center bits in the second byte identify one of the seven possible shift or rotate instructions.
The shift instructions affect all of the condition flags and the rotate instructions affect only the CF and OF flags. The destination operand, OPR, can have any of the 8086 addressing modes except the immediate mode. CNT can be a 1, a constant expression that evaluates to a 1, or the register designation CL. If it is CL, then the number of positions to be shifted is determined by the contents of CL.
118
119
Flags: CF flag set as indicated. PF, SF and ZF flags are left unchanged by rotate instructions. OF flag is meaningful only if count is 1. AF flag is affected by shift instruction, but has no meaning. Addressing modes: OPR can have any mode except immediate; CNT must be 1 or CL.
120
Assembler instructions are translated into machine language instructions and correspond to executable statements in high-level language programs.
Just as high-level language programs must have nonexecutable statements to preassign values, reserve storage, assign names to constants, form data structures, and terminate a compilation, assembler language programs must contain directives to perform similar tasks.
where the variable is optional, but if it is present it is assigned the offset of the first byte that is reserved by the directive.
Note that unlike the label field, a variable must be terminated by a blank, not a colon.
121
The mnemonic determines the length of each operand and is one of the following: DB (Define Byte) - Each operand datum occupies one byte. DW (Define Word) - Each operand datum occupies one word, with its low-order part being in the first byte and its high-order byte being in the second byte. DD (Define Double Word) - Each operand datum is two words long with the low-order word followed by the high-order word.
To preassign data the operand must be a constant, an expression that evaluates to a constant, or a string constant. For example,
122
Figure 3-55 Typical preassignment of data using the DB, DW, and DD directives
123
An ASCII character string can be preassigned by using a string constant as an operand. The statement MESSAGE DB 'H','E','L','L','O
puts the ASCII codes for H(48), E(45), L(4C), and O(4F) in consecutive bytes beginning with the byte whose address is associated with the variable MESSAGE.
This statement is equivalent to MESSAGE DB 'HELLO
Note that the first character in the string goes in the first byte, the second in the second byte, and so on.
124
125
Statements such as
INTERSEG_DATA DD DATA1 DD DATA2 could be used to store both the offsets and segment addresses, as shown in Fig. 3-58(b).
126
127
Structures (1)
All elements allocated by a single storage definition statement must be of the same type (bytes, words, or doublewords). It is desirable, especially in business data processing applications, for a variable to have several fields, with each field having its own type.
Figure 3-59 Fields in a typical personnel record data structure
A structure definition gives the pattern of the structure and may have the simplified form
Structure name STRUC . . Sequence of DB, DW, and DD directives . Structure name ENDS
If a DB, DW, or DD statement includes a variable identifier, it denotes the beginning of a field and is referred to as a field identifier.
128
Structures (2)
The structure for the personnel record shown in Fig. 3-60 could be defined
PERSONNEL_DATA STRUC INITIALS DB 'XX' LAST_NAME DB 5 DUP(?) ID DB 0,0 AGE DB ? WEIGHT DW ? PERSONNEL_DATA ENDS
The structure definition does not reserve storage or directly preassign values: it merely defines a pattern. Therefore, to reserve the necessary space it must be accompanied by a statement for invoking the structure.
129
Records (1)
The RECORD directive is for defining a bit pattern within a word or byte. It has the form
Record name RECORD Field specification, . . . , Field specification where each field specification is of the form Field name: Length = Preassignment with the preassignment being optional.
For example, PATTERN RECORD OPCODE:5,MODE:3,OPR1:4=8,OPR2:4 would break a word into four fields and give them the names OPCODE, MODE, OPR1, and OPR2. The lengths of the fields in bits would be 5, 3, 4, and 4, respectively.
130
Records (2)
The statement
INSTRUCTION PATTERN (,,,5) would actually reserve the word, associate it with the variable INSTRUCTION, and preassign OPR1 to 8 and OPR2 to 5 as shown in Fig. 3-61.
131
where the expression name may be any valid identifier and the expression may have the format of any valid operand, be any expression that evaluates to a constant (the expression name is then a constant name), or be any valid mnemonic. The MOV instruction in the sequence
132
133
As described earlier, a physical memory address is obtained by adding an offset to 16 times a segment address that is contained in a segment register.
One of the tasks an assembler must perform is to assign the offsets of the labels and variables as it translates the instructions into machine language.
The assembler must also pass to the linker (via the object modules) all of the information that the linker will need in putting the various segments and modules together to form a program.
Several directives are designed to instruct the assembler how to perform these functions.
134
135
would inform the assembler that it is to assume that the segment address of CODE_SEG is in CS, of SEG1 is in DS, and of SEG2 is in ES. An assignment is not made for SS, presumably because either the stack is not used or the assignment for SS is in a separate ASSUME statement.
136
Referring to the structure given in Fig. 3-63, the code segment might typically begin as follows:
It is important to note that the ASSUME directive does not load the segment addresses into the corresponding segment registers.
Figure 3-63 Representative program structure
137
Program Termination
Just as an END statement is needed to signal the end of a high-level language program, an END directive of the form: END Label
138
Alignment Directives
There are two directives that are used for alignment purposes. The directive:
139
140
For the above segments, the first pass of the assembler uses the location counter to enter.
141
the assembler would check if the source type matches with the destination type and if COUNT is accessible through DS. Then, the assembler would note that the offset for COUNT is 006F (=11110) and would produce the machine instruction
The assembler also includes two tables, known as permanent symbol tables. In addition to assembling the machine instructions, the second pass must insert the preassigned constants that occur in the data definition statements and prepare the other information that will be required by the linker.
142
143
144
A typical listing is shown in Fig. 3-70. The first column in the program portion of the listing gives the value of the location counter immediately before the corresponding statement is assembled. The second column shows the machine code that the statement is assembled into.
The third column is simply the line number in the source code, and the remainder of each line is the source code just as it is presented to the assembler. If an error is found, an error identifying number and message are output on the line following the line containing the error.
145
The translation from assembler instructions to machine instructions is, in most cases, quite straightforward. All of the 8086 machine instructions consist of 1 or 2 bytes of op code and addressing mode designations with from 0 to 4 bytes of immediate, displacement, or segment address information appended to them. The number of appended bytes depends on the addressing modes. If the addressing modes call for both an immediate operand and a displacement the displacement will appear first, and if both an offset and a segment address are present, the offset will appear first.
146
Figure 3-71 Machine code for the 8086/8088 instructions. (Reprinted by permission of Intel Corporation. Copyright 1979.)
147
Move
148
149
150
151
152
153
154
155
156
157
158
159
OR and XOR
160
161
CALL
162
JMP
163
RET
164
165
166
167
168
169
170
Stack instructions
Flags: The flags are affected only by the POPF instruction. Addressing mode: For the PUSH and POP instructions the operand must be a word and may not be immediate. segment register can be specified as the operand in a PUSH or POP instruction. However, CS cannot be used in a POP instruction.
Mnemonic and Format Description
(SP) ((SP) + 1 : (SP)) (DST) (SP) (SP) ((SP) + 1 : (SP)) (PSW) (SP) (SP) 2 (SRC) ((SP) + 1 : (SP)) (SP) + 2 (SP) 2 (PSW) ((SP) + 1 : (SP)) (SP) + 2
Name
Push onto the stack Pop from the stack Push the flags Pop the flags
SRC SRC
171
172
Description
(SP) 2 (IP) (IP) + D16 *
Intrasegment CALL DST direct call Intrasegment CALL DST indirect call
Addressing modes: May be any branch addressing mode except a short CALL.
* Displacement between the destination and the instruction following the CALL instruction.
173
Name
Description
(IP) (SP) ((SP) + 1 : (SP)) (SP) + 2
Intrasegment return
RET
RET
EXP**
Intersegment return
RET
RET
EXP**
** EXP is an expression that evaluates to a constant and becomes the D16 portion of the instruction
174
175
Move string
Move byte string Move word string
MOVS DST,SRC
MOVSB MOVSW
(DI) 1
Compare string
((SI)) ((DI)) Byte operands (SI) (SI) 1, (DI) Word operands (SI) (SI) 2, (DI)
(DI) 1 (DI) 2
CMPSB CMPSW
176
Description **
Byte operand ((AL)) ((DI)), (DI) Word operand ((AX)) ((DI)), (DI)
Scan string Scan byte string Scan word string Load string Load byte string Load word string Store string Store byte string Store word string
SCAS DST SCASB SCASW LODS SRC LODSB LODSW STOS DST STOSB STOSW
(DI) 1 (DI) 2
Byte operand (AL) (SI), (SI) Word operand (AX) (SI), (SI)
(SI) 1 (SI) 2
Byte operand ((DI)) ((AL)), (DI) Word operand ((DI)) ((AX)), (DI)
(DI) 1 (DI) 2
177
Note that the second program sequence may move either bytes or words, depending on the type of STRING1 and STRING2. In Sec.5-2 it will be seen that this task can be performed even more efficiently by applying the REP prefix to eliminate the explicit loop.
178
The program sequence given in Fig.5-3 demonstrates the use of the DF flag by showing how data can be moved from an area to an overlapping area.
179
The other three string primitives SCAS,LODS and STOS, have single memory operands. Of these primitives only SCAS affects the condition flags.
Figure 5-6 Example of the use of the SCAS,STOS and LODS primitives
180
REP Prefix
As an example of the use of the REP prefix let us reconsider the program sequence for moving a string within memory given in Fig. 5-2(b).
By replacing the explicit loop MOVE: MOVS STRING2, STRING1 LOOP MOVE with REP MOVS STRING2, STRING1 not only is the code simplified, but the execution time is reduced from 18 + 17 = 35 clock cycles per iteration to 9 + 17 = 26 clock cycles
for the first iteration and 17 clock cycles for each subsequent iteration.
181
Figure 5-8 Search a table for a given name with eight characters
182
183
184
Unlike memory addressing, the contents of DX are not modified by any segment register.
This allows variable access to I/O ports in the range 0 to 64K. The machine language code for the IN instruction is:
Although AL or AX is implied as the first operand in an IN instruction, either AL or AX must be specified so that the assembler can determine the W-bit.
185
Note that if the long form of the IN or OUT instruction is used the port address must be in the range 0000 to 00FF, but for the short form it can be any address in the range 0000 to FFFF (i.e. any address in the I/O address space). Neither IN nor OUT affects the flags. The IN instruction may be used to input data from a data buffer register or the status from a status register. The instructions IN AX, 28H MOV DATA_WORD, AX would move the word in the ports whose address are 0028 and 0029 to the memory location DATA_WORD.
186
PROGRAMMED I/O
Programmed I/O consists of continually examining the status of an interface and performing an I/O operation with the interface when its status indicates that it has data to be input or its data-out buffer register is ready to receive data from the CPU.
187
188
189
190
191
If there is more than one device using programmed I/O, it is necessary to poll the ready bits of all of the devices.
192
Figure 6-8 shows how the devices could be serviced in turn. This is referred to as a round-robin arrangement. Such an arrangement essentially gives all three devices the same priority.
193
INTERRUPT I/O
Even though programmed I/O is conceptually simple, it can waste a considerable amount of time while waiting for ready bits to become active.
In the above example, if the person typing on the terminal could type 10 characters per second and only 10 s is required for the computer to input each character, then approximately 99,990 100,000 X 100% = 99.99% of the time is not being utilized.
194
195
Thus, an interrupt causes the normal program sequence to be suspended and a branch to be made to the location indicated by the double word beginning at four times the type (i.e. the interrupt pointer). Control can be returned to the point at which the interrupt occurred by placing an IRET instruction at the end of the interrupt routine. It was mentioned that there are two classes of interrupts, internal and external interrupts, with external interrupts being caused by a signal being sent to the CPU through one of its pins, which for the 8086 is either the NMI pin or the INTR pin. An interrupt initiated by a signal on the NMI pin is called a non-maskable interrupt and will cause a type 2 interrupt regardless of the setting of the IF flag. Non-maskable interrupt signals are normally caused by circuits for detecting catastrophic events.
196
197
Figure 6-10 Processing component relationships for the line input example involving interrupt I/O.
198
199
200
If the interrupt pointers are to be set by the user's program, they could alternatively be set when the program is loaded by inserting the following directives at the beginning of the program:
201
On the other hand, for a program that continually receives new data and cannot suspend the input while processing a buffer at least two buffers are needed. Figure 6-13 gives a flowchart of how LINE_PROC could be structured when double buffering is required.
202
203
204
205
Typical, such a circuit would be designed and inserted in the system as shown in Fig.6-15. The INTR and /INTA pins would not be connected to the interface but would be connected only to the management circuit.
Many microprocessor manufacturers produce interrupt priority management IC devices to supplement their CPU devices. The Intel 8259A programmable interrupt controller is designed to work with the 8086 and 8088 CPUs. It is similar to the management circuit shown in Fig.6-15, but has many features not considered above.
206
207
RD - To signal the 8259A that it is to place the contents of the IMR, 1SR, or IRR register or a priority level on the data bus. Which of these possibilities is placed on the bus depends on the state of the 8259A and is discussed below.
WR - To signal the 8259A that it is to accept data from the data bus and use the data to set the bits in the command words. How the received data are distributed is discussed later.
208
209
210
Bits 7-5 - Not used in an 8086/8088 system, only in an 8080 or 8085 system.
Bit 4 - Always set to 1. It directs the received byte to ICW1 as opposed to OCW2 or OCW3, which also use the even address (A0 = 0).
Bit 3(LTIM) - Determines whether the edge-triggered mode (LTIM = 0) or the level-triggered mode (LTIM = 1) is to be used. The edge-triggered mode causes the IRR bit to be cleared when the corresponding ISR bit is set.
211
Bit 2 (ADI) - Not used in an 8086/8088 system, only in an 8080 or 8085 system.
Bit 1 (SNGL) - Indicates whether or not the 8259A is cascaded with other 8259As. SNGL = 1 when only one 8259A is in the interrupt system.
Bit 0 (IC4) - Is set to 1 if an ICW4 is to be output to during the initialization sequence. For an 8086/8088 system this bit must always be set to 1 because bit 0 in ICW4 must be set to 1.
212
ICW3 is significant only in systems including more than one 8259A and is output to only if SNGL = 0. This case is discussed in Sec. 8-3-2. ICW4 is output to only if IC4 (ICWI) is set to 1; otherwise, the contents of ICW4 is cleared. The bits in ICW4 are defined as follows:
Bits 7-5 - Always set to 0. Bit 4 (SFNM) - If set to 1, the special fully nested mode is used. This mode is utilized in systems having more than one 8259A and is discussed below. Bit 3 (BUF) - BUF = 1 indicates that the SP/EN is to be used as an output to disable the system's 8286 transceivers while the CPU inputs data from the 8259A. If no transceivers are present, BUF should be set to 0 and, in systems involving only one 8259A, a 1 should be applied to the SP/EN pin.
213
Bit 2 (M/S) - This bit is ignored if BUF = 0. For a system having only one 8259A, this bit should be 1; otherwise, it should be 1 for the master and 0 for the slaves.
Bit 1 (AEOI) - If AEOI = 1, then the ISR bit that caused the interrupt is cleared at the end of the second INTA pulse.
Bit 0 (mPM) - mPM = 1 indicates the 8259A is in an 8086/8088 system. This bit being 0 implies an 8080 or 8085 system.
214
The first two instructions cause the requests to be edge triggered, denote that only one 8259A is used, and inform the 8259A that an ICW4 will be output. The next two instructions cause the 5 most significant bits of the interrupt type to be set to 00011. ICW3 is not output to because SNGL = 1; therefore, the last two instructions set ICW4 to 0D, which informs the 8259A that the special fully nested mode is not to be used, the SP/EN is used to disable transceivers, the 8259A is a master, EOI commands must be used to clear the ISR bit, and the 8259A is part of an 8086/8088 system. There are three OCWs. The command word OCW1 is used for masking interrupt requests; when the mask bit corresponding to an interrupt request is 1, then the request is blocked. OCW2 and OCW3 are for controlling the mode of the 8259A and receiving EOI commands.
215
Nonspecific, normal priority mode Specifically clears the ISR bit indicated by L2-LO Rotate priority so that a device after being serviced has the lowest priority Rotate priority until position specified by L2-LO is lowest
216
Under the normal priority mode, if ISRn is set, the priority resolver will not recognize any requests on IR7 through IR(n + l), but will recognize unmasked requests on IR(n-l) through IRQ.
As an example of the normal priority mode, suppose that initially AEOI = 0 and all ISR and IMR bits are clear.
Also suppose that, as shown in Fig. 8-18, requests occur simultaneously on IR2 and IR4, then a request arrives at IR1, and last a request arrives at IRS, and that these are the only requests.
217 Figure 8-18 Actions taken in the normal operating mode when a typical sequence of interrupts occurs.
218
Any ISR bit can be explicitly cleared by sending an OCW2 with the R, SL, and EOI bits set to 011 and putting the number of the bit to be cleared in L2-L0. If 0 1 1 0 0 0 1 1 is sent to OCW2, then ISR3 will be cleared.
In addition to the normal priority mode discussed above, OCW2 can rotate the priority by assigning bottom priority to any one of the IR levels. In this case the other priorities will follow as if the normal ordering had been rotated.
219
1 0 1 0 0 0 0 0
220
221
Master: IR0 Slave 1: IR0, IR1, IR2, IR3, IR4, IR5, IR6, IR7 Slave 2: IR0, IR1, IR2, IR3, IR4, IR5, IR6, IR7 Master: IR3, IR4, IR5, IR6, IR7
Lowest priority
The masks in the master and slaves may, of course, be used to block out some of the requests.
222
During any given bus cycle, one of the system components connected to the system bus is given control of the bus. This component is said to be the master during that cycle and the component it is communicating with is said to be the slave.
The 8086 receives bus requests through its HOLD pin and issues grants from its hold acknowledge (HLDA) pin. A request is made when a potential master sends a 1 to the HOLD pin. Normally, after the current bus cycle is complete the 8086 will respond by putting a 1 on the HLDA pin.
When the requesting device receives this grant signal it becomes the master. It will remain master until it drops the signal to the HOLD pin, at which time the 8086 will drop the grant on the HLDA pin.
223
224
Although DMA controllers could be designed around a variety of configurations all of these configurations must satisfy certain requirements.
225
5. 6. 7. 8. 9.
The controller/interface design shows bidirectional address lines connected to the controller and only unidirectional address lines going to the interface.
226
A typical sequence for starting a block input transfer is given in Fig.6-18. This sequence assumes the following bit definitions:
Bit 2 of INTSTAT - Busy bit for the I/O device Bit 1 of DMACON - Informs the controller of the transfer direction; 1 is for input Bit 3 of DMACON - Enables the controller so it will accept DMA requests Bit 6 of DMACON - Clear when bus is to be relinquished between transfers Bit 0 of INTCON - Informs the interface of the transfer direction; 1 is for input Bit 2 of INTCON - Do bit which starts the I/O activity
227
After the sequence in Fig.6-18 is executed the I/O device will begin inputting data and the DMA controller will steal a bus cycle and transfer a byte from the interface to memory each time a byte is placed in the interface's data-in buffer register.
228
If an interface is connected to a nonstorage device then the minimal configuration shown in Fig.6-17 may be adequate but for a storage device the interface needs to communicate, search and address information to the device.
The interface for a single-channel A/D conversion subsystem does not need to contain more than 2 or 3 bytes of control and status information but it would need to contain bits for:
1. 2. 3. 4. 5.
Enabling the interrupt capability Indicating errors Specifying the sample rate Enabling the DMA capability Initiating the input (i.e. setting the do bit)
229
In order to adapt to as many situations as possible both the 8086 and 8088 have been given two modes of operation, the minimum mode and the maximum mode. The minimum mode is used for a small system with a single processor, a system in which the 8086/8088 generates all the necessary bus control signals directly (thereby minimizing the required bus control logic).
The maximum mode is for medium-size to large systems, which often include two or more processors.
230
231
Figure 8-2 Pin assignment summary [Parts (a) and (b) reprinted by permission of Intel Corporation. Copyright 1981.]
232
233
234
Except for pins 28 and 34 the two processors have the same control pin definitions.
Pin 28 differs in the minimum mode. For the 8088 this minimum mode signal is inverted from that of the 8086, so that the 8088 is compatible with the Intel 8085 microcomputer chip.
On the 8086, pin 34(/BHE) designates whether or not at least 1 byte of a transfer is to be made on AD15 through AD8.
A 0 on this pin indicates that the more significant data lines are to be used; otherwise, only AD7 through AD0 are used.
235
Output during the latter portion of the bus cycle and is to inform the transceivers that the CPU is ready to send or receive data.
Indicates to the set of transceivers whether they are to transmit (1) or receive (0) data. Distinguishes a memory transfer from an I/O transfer. For a memory transfer it is 1. (For the 8088, the symbol is IO/ /M and a 1 indicates an I/O transfer. When 0, it indicates a write operation is being performed. It is used in conjunction with pins 28 (M/ /IO) and 32 (/RD) to specify the type of transfer. Outputs a bus grant to a requesting master. Pins with tristate gates are put in high impedance state while HLDA=1. Receive bus requests from bus masters. The 8086/8088 will not gain control of the bus until this signal is dropped.
236
237
To signal that the address is ready to be latched a 1 is put on pin 25, the address latch enable (ALE) pin.
Typically, the latching is accomplished using Intel 8282s, as shown in Fig.8-5. Because 8282 is an 8-bit latch, two of them are needed for 16-bit address and three are needed if a full 20-bit address is used.
238
The Intel IC device for implementing the transceiver (driver/receiver) block shown in Fig.8-4 is the 8286 transceiver device.
The 8286 contains 16 tristate elements, eight receivers and eight drivers. Therefore, only one 8286 is needed to service all of the data lines for an 8088, but two are required in an 8086 system.
Sometimes a system bus is designed so that the address and/or data signals are inverted. Therefore, the 8282 and 8286 both have companion chips that are the same as the 8282 and 8286 except that they cause an inversion between their inputs and outputs.
The companion for the 8282 is the 8283 and the companion for the 8286 is the 8287.
Figure 8-5 Application and internal logic of an 8286
239
240
241
(a) Input
Note: For an 8088, M / /IO is IO / /M and /BHE / S7 becomes /SSO which is present throughout the bus cycle (i.e. it changes at the same time as IO / /M). Also, only AD7-AD0 carry data.
242
(b) Output
243
Upon receiving the second pulse, the interface accepting the acknowledgment will put the interrupt type on AD7-AD0, which are floated the rest of the time during the two bus cycles.
The type will be available from T2 to T4.
244
Figure 8-14 Bus request and bus grant timing on a minimum mode system
245
BUS STANDARDS
The Intel MULTIBUS has gained wide industrial acceptance and several manufacturers offer MULTIBUS-compatible modules. This bus is designed to support both 8-bit and 16-bit devices and can be used in multiprocessor systems in which several processors can be masters. At any point in time, only two devices may communicate with each other over the bus, one being the master and the other slave. The master/slave relationship is dynamic with bus allocation being accomplished through the bus allocation (i.e. request/grant) control signals. The MULTIBUS has been physically implemented on an etched back plane board which is connected to each module using two edge connectors, denoted P1 and P2, as shown in Fig.8-20. The connector P1 consists of 86 pins which provide the major bus signals, and P2 is an optional connector consisting of 60 auxiliary lines. The P1 lines can be divided into the following groups according to their functions: 1.Address lines. 2.Data lines. 3.Command and handshaking lines. 4.Bus access control lines. 5.Utility lines.
246
2.
3.
Input data from or output data to the associated I/O device and convert the data from parallel to the format acceptable to the I/O device, or vice versa.
Send a ready signal when data have been accepted from or placed on the data bus, thus informing the processor that a transfer has been completed. Send interrupt requests and, if there is no interrupt priority management in the bus control logic, receive interrupt acknowledgments and send an interrupt type. Receive a reset signal and reinitialize itself and perhaps, its associated device.
4.
5.
6.
247
248
249
250
ASYNCHRONOUS COMMUNICATION
251
252
253
254
255
Vo<25 V Maximum short circuit current to any wire in cable - 0.5 A MARK signal at load < -3 V SPACE signal at load < +3 V MARK signal out of driver < -5 V and > -15 V SPACE signal out of driver > +5 V and < +15 V Rl < 7000 ohms when measured with a voltage from 3 to 25 V, but > 3000 ohms Cl including line capacitance < 2500 pF When El=0.5 V < Vi < 15 V , Ro > 300 ohms under power off conditions Co is such that slew rate of the driver's output voltage is < 30 V/microsecond, but the transition between -3 V and +3 V does not exceed the smaller of 1 ms or 4% of the bit time
256
257
258
259
260
Whether the mode, control or sync character register is selected depends on the accessing sequence. A flowchart of the sequencing is given in Fig. 9-14.
261
The relationship between the frequencies of the TxC and RxC clock inputs and the baud rate of the transmitter and receiver is: Clock frequency = Baud rate factor x Baud rate
If 10 is in the LSBs of the mode register and the transmitter and receiver baud rates are to be 300 and 1200, respectively, then the frequency applied to: ___ TxC should be 4800 Hz, and the frequency at ___ RxC should be 19.2 kHz.
262
263
264
NOTE: With regard to the synchronous connections it is assumed that the timing is controlled by the modem and its related communications equipment.
265
The sequence:
MOV AL,00111000B OUT 51H,AL MOV AL,16H OUT 51H,AL OUT 51H,AL MOV AL,10010100B OUT 51H,AL would cause the same 8251A to be put in synchronous mode and to begin searching for two successive ASCII sync characters.
266
267
268
PARALLEL COMMUNICATION
269
270
271
272
If a bit is 0, then the corresponding set is used for output; if it is 1, the set is for input.
273
For outputting:
274
275
While group A is in mode 2, group B may be in either mode 0 or mode 1. However, if group B is in mode 0, only PC2-PC0 can be used for input or output because group A has borrowed PC3 to use as an interrupt request line. Normally, if group A is in mode 2, PC2-PC0 would be connected to control and status pins on the device attached to the port A lines. Port B may also be used for this purpose.
276
Figure 9-23 Interfacing an A/D subsystem and D/A subsystem using an 8255A
277
MOV DX,0FFFBH MOV AL,00001111B OUT DX,AL MOV AL,00001110B OUT DX,AL
would output a pulse to the convert pin of the A/D converter. The first instruction of the latter sequence puts the address associated with Set/Reset instruction, which is the same as the address of the control register, in the DX register. The next two instructions cause PC7 to be set and the last two cause it to be cleared.
278
For outputting a byte from AL to the D/A converter, only the instructions MOV DX,0FFF9H OUT DX,AL are needed. As soon as the byte arrives at port B its bits are immediately applied to the input pins of the D/A converter, which, in turn, immediately converts it to an analog signal. The sample time could be adjusted by including a "do nothing" loop, such as: MOV CX,N IDLE: NOP LOOP IDLE between the inputs or outputs.
279
A flowchart for inputting a block of A/D samples using programmed timing is given in Fig.9-24. Only 8-bit A/D and D/A converters are included in the design shown in Fig.9-23. This limits the resolution to only 1 part in 256. If the voltage range of the input or output were -10 V to +10 V, the resolution would be: 20/256=0.078 V For higher resolutions, 10-, 12- or 14-bit converters are required.
280
1. Interrupt a time-sharing operating system at evenly spaced intervals so that it can switch programs. 2. Output precisely timed signals with programmed periods to an I/O device (e.g. an A/D converter). 3. Serve as a programmable baud rate generator. 4. Measure time delays between external events. 5. Count the number of times an event occurs in an external experiment and provide a means of inputting the count to the computer. 6. Cause the processor to be interrupted after a programmed number of external events have occurred.
281
282
The mode determines exactly what happens when the count becomes 0 and/or a signal is applied to the gate input.
Some possible actions are:
1. The GATE input is used for enabling and disabling the CLK input.
2. The GATE input may cause the counter to be reinitialized. 3. The GATE input may stop the count and force OUT high.
4. The count will give an OUT signal and stop when it reaches 0.
5. The count will give an OUT signal and automatically be reinitialized from the Initial Count Register when the count reaches 0.
283
284
285
286
All other combinations result in the data pins being put into their high-impedance state.
When A1=A0=1, whether a control register is being written or a command is being given depends on the MSBs of the byte being output.
There are two types of commands, the counter latch command, which causes the CE in the counter specified by the two MSBs of the command to be latched into the corresponding OL, and the read back command, which may cause a combination of the CEs to be latched or "prepare" a combination of status registers to be read.
287
If the \COUNT bit is 0, then the CEs for all of the counters whose CNT bits are 1 are latched.
If CNT0=CNT2=1 but CNT1=0, then the CEs in counters and 2 are latched but the CE in counter 1 is not latched.
Similarly, \STAT=0 causes the counters' status registers to be prepared for input.
288
289
Mode 0 (Interrupt on Terminal Count) GATE=1 enables counting and GATE=0 disables counting, and GATE has no effect on OUT. The contents of CR are transferred to CE on the first CLK pulse after CR is written into by the processor, regardless of the signal on the GATE pin. The pulse that loads CE is not included in the count. OUT goes low when there is an output to the control register and remains low until the count goes to 0. Mode 0 is primarily for event counting. Mode 1 (Hardware Retriggerable One-Shot) After CR has been loaded with N, a 0-to-1 transition on GATE will cause CE to be loaded, a 1-to-0 transition at OUT, and the count to begin. When the count reaches 0 OUT will go high, thus producing a negative-going OUT pulse N clock periods long.
290
291
Mode 4 (Software-Triggered Strobe) It is similar to mode 0 except that OUT is high while the counting is taking place and produces a one-clock period negative pulse when the count reaches 0.
Mode 5 (Hardware-Triggered Strobe-Retriggerable) After CR is loaded, a 0-to-1 transition on GATE will cause a transfer from CR to CE during the next CLK pulse. OUT will be high during the counting but will go low for one CLK period when the count becomes 0. GATE can reinitialize counting at any time.
292
Figure 9-28 shows how an 8254 could be used to provide a programmable sample rate generator for an A/D subsystem.
293
294
For low-cost small systems, especially single-board microcomputers and microprocessor-based instruments, the front panel (or console) is often implemented by using simple keyboard and display units as input and output devices.
Keyboard Design Unlike a terminal, mechanical contact keyboard, for which the key switches are organized in a matrix form, does not include any electronics. Figure 9-30 illustrates how a 64-key keyboard can be interfaced to a microcomputer through two parallel I/O ports such as those provided by an 8255A.
295
Keyboard Design
296
Display Design
Various types of devices are available for numeric and alphanumeric displays. Seven-segment LED displays such as the one shown in Fig.9-31 are typically used for hexadecimal digit display.
297
Figure 9-32 shows a multiple-digit display that is configured from eight seven-segment display units.
In order to reduce the device count by eliminating external data latches from the display units, they can be connected to two 8-bit parallel output ports and operated in a multiplexed mode.
298
299
300
Keyboard/Display Controller
The Intel 8279 keyboard/display controller is an LSI device designed to release the processor from performing the time-consuming scan and refresh operations.
301
For keyboard control, the 8279 constantly scans each row of the keyboard by sending out row addresses on SL2-SL0 and inputting signals on the return lines RL7-RL0, which represent the column addresses.
302
303
The SHIFT and CNTL pins are used primarily to support typewriter-like keyboards which have shift and control keys.
The key position is then entered into the 8x8 first-in/first-out (FIFO) sensor memory and the IRQ (interrupt request) line is activated if the sensor memory was previously empty.
The three MSBs of a command determine its type and the meaning of the remaining 5 bits depends on the type. Although there are eight types, only three of them are considered here.
304
Keyboard Display Mode Set Specifies the input and display modes and is used to initialize the 8279.
305
Read FIFO Sensor Memory Specifies that a read from the data buffer register will input a byte from the FIFO memory and, if the 8279 is in the sensor mode, it indicates which row is to be read. This command is required before inputting data from the FIFO memory. Its format is:
Note that if the input mode is a keyboard scan mode, a read is always from the byte which first entered the FIFO, hence the I and AAA bits are ignored.
306
Write to Display Memory Indicates that a write to the data buffer register will put data in the display memory. This command must be given before the CPU can send the characters to be displayed to the 8279. Its format is:
The 8279 provides two options for handling the situation in which more that one key is depressed at about the same time.
307
308
To demonstrate how to program an 8279, let us assume that the device is connected to a keyboard and multiple-digit display as shown in Fig.9-35, the 8279's addresses are FFE8 and FFE9, and the interrupt request pin IRQ is not used.
First, the device must be initialized by sending a mode set command to the control register. The following instructions set the keyboard/display controller to its encoded keyboard scan mode, with two-key lockout, and its left entry eight 8-bit displays mode:
309
Then, characters generated by the depressed keys can be read through the FIFO memory. ] A program segment that uses programmed I/O to input eight keywords and store them in an 8-byte array KEYS with the first byte at the highest address is:
310
To display characters, the CPU must first give a write display memory command and then output to the display memory.
The following instruction sequence displays eight seven-segment digits which are stored beginning at DIGITS with the least significant digit being stored at the low address:
311
DMA CONTROLLERS
As discussed in Chap. 6, a DMA controller is capable of becoming the bus master and supervising a transfer between an I/O or mass storage interface and memory.
312
313
314
When data are being put in or taken out of 8237's registers, the 8237 is a slave just like the system's I/O interfaces.
When both HRQ and \CS are low, the 8237 becomes a slave with the \IOR and \IOW being the input control pins. The CPU can read from or write to the internal registers of the controller by activating \IOR or \IOW. The AEN, which is active when the controller is a master and is outputting an address, is 0 while the system is communicating with the controller's registers. If the controller is the master, then it must supply the bus address. When it is master it puts the low-order byte of the address on the pins A7-A0 and the high-order byte on DB7-DB0, and sets AEN to 1.
315
316
Single Transfer Mode(01) After each transfer the controller will release the bus to the processor for at least one bus cycle, but will immediately begin testing for DREQ inputs and proceed to steal another cycle as soon as a DREQ line becomes active. Block Transfer Mode(10) DREQ need only be active until DACK becomes active, after which the bus is not released until the entire block of data has been transferred.
317
Demand Transfer Mode(00) This mode is similar to the block mode except that DREQ is tested after each transfer. If DREQ is inactive, transfers are suspended until DREQ once again becomes active, at which time the block transfer continues from the point at which it was suspended. This allows the interface to stop the transfer in the event that its device cannot keep up. Cascade Mode(11) In this mode 8237s may be cascaded so that more than four channels can be included in the DMA subsystem. In cascading the controllers, those in the second level are connected to those in the first level by joining HRQ to DREQ and HLDA to DACK. To converse space, this mode will not be considered further.
In all cases the count going to zero will cause \EOP to become active and the transfer process to cease.
318
319
The two LSBs of an output to a mode register direct the output to the indicated channel, i.e., select the mode register that is to receive the output. In addition to block transfers between I/O or mass storage devices and memory, the 8237 can supervise memory-to-memory transfers. Such transfers are conducted by bringing bytes from the source memory area into 8-bit temporary register in the 8237 and then outputting them to the destination memory area. Therefore, two bus cycles are required for each memory-to-memory transfer. The channel 0 current address register is used for source addressing. The channel 1 current address and current byte count registers provide the destination addressing and counting.
320
321
322
323
The 8237 timing can be divided into the SI,S0,S1,S2,S3,S4 and SW states. A flowchart of the timing as seen by the 8237 is given in Fig.9-40.
324
325
9-6 DISKETTE CONTROLLERS Magnetic tapes and disks have two major advantages over MBMs:portability between systems and capacity.Magnetic tapes tend to cost less per byte of stored information and be the most durable, but disks have much lower access times.
The data are bit serially stored (i.e., as a succession of bits) in concentric circles called tracks and are grouped into arcs known as sectors. Some diskette drivers have only one read/write head and can only store and retrieve data from one surface of the diskette, while others have two read /write heads and can utilize both surfaces.If both surfaces can be accessed , then the pairs of tracks that are the same distance from the center of the diskette are referred to as cylinders. Some have only one index hole and are said to be soft sectored, while others have an index hole for each sector and are said to be hard sectored. The tracks (and cylinders) are numbered, with the outermost track being given the number 0.The sectors are also numbered and on a soft-sectored diskette, the first sector after the index hole is assigned the number 1. The time needed to access a sector is subdivided into: Load time -For bringing the head in contact with the diskette. Position time -For positioning the head over the track. Rotational time -For rotating the diskette until it is over the desired sector. Typical average load, position and rotational times are 16,225 and 80 ms, respectively.Once a sector is found the average information transfer rate in bytes per second is approximately: Bytes per sector x Sectors per track x Speed in rpm/60 (This includes the time wasted in traversing gaps in the data)
326
In order to make our discussion more specific, let us now limit it to a single type of diskette, the IBM 3740-compatible, soft-sectored, 8-in. diskette.These diskettes contain 77 tracks and either 15 sectors (single density) or 26 sectors (double density), although our examples will permit from 8 to 26 sectors.Normally, only 75 of the tracks are used, thus allowing for two bad tracks.The good tracks are numbered 0 through 74.
The bit pattern of the serially stored information is shown in Fig.9-44. The information is grouped into cells, each of which is divided into four intervals, with the first interval containing a clock pulse.The third interval is for indicating a data bit and will contain a pulse if the data bit is 1. A representative value for the cell period for a 360-rpm drive is 4 s.
327
If a diskette having 26 sectors and 128 data bytes per sector is rotated at 360 rpm. the average transfer rate is approximately 26 x 128 x 360/60 =19,968 bytes/second which gives an average period of about 50 s.Taking into account the gaps, the actual period for transferring a byte is normally 32 s (or 4 s per bit).Although it would be possible to perform transfers at this rate without a DMA controller, it is much more efficient to use one.
328
329
Operations executed by the controller are divided into a command phase, an execution phase and a result phase. During the command phase bytes are sent to the control registers and flags via the data in/out register. Then the requested operation is performed during the execution phase and upon completion of the operation, the result phase is entered and status information is read by the processor. The outputs during the command phase and inputs during the result phase are performed using single-byte transfers, even though any data transfer that takes place in the execution phase is normally supervised by a DMA controller.
330
The sequence needed to complete four of these commands is given in Fig.9-49 as an example.The read data command includes all three phases, the seek command includes only the command and execution phases, the sense drive status command includes only the command and result phases and the specify command consists only of the command phase.
In all cases, the commands must be performed in their entirety (including the result phase, if applicable) or they will be considered incomplete. If an invalid command is given, the 8272 will return the status byte STO in response to the next input from the data in/out register.
331
As an example, consider an 8272 whose even address is 002A.The 8272 could be initialized to a step rate of 6 ms per track, a head unload time of 48 ms, a head load time of 16 ms and the DMA mode by the specify command sequence
The reason the %CHECK macro is needed before each output is that the two MSBs of the status register must be 10 before each command byte is written into the 8272.Also during the result phase, these 2 bits must be 11 before each byte is read from the 8272's data register.
would cause the head on drive 2 to be moved to cylinder 30 and head 0 to be selected.
332
Figure 9-50 defines two macros for executing the read data command.It is assumed that a sequence such as
would appear just prior to a READ_COM macro call to make certain that the desired drive (which in this example is drive 1) is available. Also, the associated DMA must be initialized before the READ_COM macro call is made so that DMA transfers may begin as soon as the read operation is performed.It is assumed that a call to the READ_STAT macro would be made only after the execution phase and the read is known to be complete (e.g., after an interrupt on completion interrupt has occurred).
333
9-7 MAXIMUM MODE AND 16-BIT BUS INTERFACE DESIGNS As noted in the chapter's introduction, all of the Intel examples in the first six sections of this chapter are based on a minimum mode 8088 processor.To convert the designs to a maximum mode system two primary changes are necessary.First of all, an 8288 bus controller must be connected into the system as shown in Figs.8-9 and 8-10. For a system that contains an 8237 DMA controller, the 8288 would replace the circuit for encoding the \RD,\WR and IO/ \M signals shown in Fig.9-38 and detailed in Fig.9-39.In any event, with the inclusion of an 8288 the \RD and \WR pins on the interfaces would be attached to the \IORC and \IOWC outputs from the 8288 and the IO/ \M lines shown entering the address decoders would no longer be required. The other major change concerns the HRQ and HLDA signals used to make bus requests and grants.The 8237 is designed to output a continuous request HRQ signal until it is ready to relinquish the bus, at which time it drops the signal.Also, the processor outputs a continuous HLDA signal. This is compatible with n 8086/8088 processor in minimum mode, but in maximum mode the processor uses a single \RQ / \GT line to both receive requests and issue grants and expects to see only a pulse at the time the request is made.The request is acknowledged by outputting a pulse and a second pulse must be sent to the processor from the DMA controller at the conclusion of the DMA activity. A circuit for converting between the two-line continuous signals associated with the 8237 and the one-line pulses of a maximum mode processor is given in Fig.9-51.
334
The problems associated with connecting the 8-bit interface devices to a 16-bit bus of an 8086 are related to the need to transfer even-addressed bytes over the lower half of the data bus and odd-addressed bytes over the upper half.
For interfaces that communicate only with the processor (i.e., do not utilize DMA), the problem can be solved quite simply.Instead of connecting the address lines for selecting individual registers internal to the interface , say An-A0, to the interface's pins An-A0, attach lines A(n+1) -A1 to those pins as shown in Fig.9-52.
This means that the interface will be assigned only even addresses in the I/O address space beginning with an address divisible by 2^(n+1) and the interspersed odd addresses would not be used.
For example, if the A1 and A0 pins on the 8255A were connected to the A2 and A1 address lines and the beginning address of the 8255A ports were 08F8, then all transfers to and from the 8255A would be made over the low-order byte of the bus. The ports A,B and C would have the addresses 08F8,08FA and 08FC, respectively and the control register would be assigned the address 08FE.Likewise, consecutive odd addresses can be assigned to an interface if the interface is connected to the high-order byte of the bus.
335
To use successive address and both halves of a 16-bit data bus, the bus could be connected as shown in Fig.9-53.
where 0 is low and 1 is high.By using these signals and two transceivers data can be transferred between the interface and the data bus lines D7-D0 when \BHE = 1 and A0 = 0, and the bus lines D15-D8 when \BHE = 0 and A0 = 1.The \RD signal is used to determine the direction of the data flow.The ready logic is not shown.
336
For an 8237-based DMA system, the bus control logic shown in Fig.9-38 must be altered as indicated in Fig.9-54.
The \BHE signal coming out of the 8086 would be latched by the same 8282 as is used by the A19-A16 lines.The A0 line would be connected to the \BHE line through a tristate inverter which is controlled by the 8237's signal.
When AEN is active and a0 =0, \BHE is high, indicating that the transfer is to be made over the lower byte of the bus.If AEN is active and A0 = 1 \BHE is low and the transfer is made over the upper byte.In addition, both the interface and the 8237 must be connected to the bus through extra logic similar to that given in Fig.9-53.
Although the above paragraphs have been concerned with the communication between an 8-bit interface and a 16-bit data bus, some attention should be given to the design of a 16-bit interface.Such an interface would transfer entire words to and from the data bus and would tend to double the utilization of the available bus cycles.A 16-bit interface design based on two 8255As is given in Fig.9-55.
The A2 and A1 lines in the address bus are connected to the A1 and A0 pins of both 825As: thus the 16-bit ports are formed from the pairs of ports A,B,C and the control/status registers.
The lower 8255A occupies 4 consecutive even addresses and the upper 8255A occupies 4 consecutive odd addresses.If bits A15A3 match the address designed into the address decoder, then the decoder will emit a 0 chip select signal.
For the lower 8255A, if both the chip select and A0 signals are 0, then a 0 is applied to \CS.For the upper 8255A, both the chip select and \BHE signals must be 0 in order for a 0 to be sent to the \CS.(Therefore, it is possible to address the 8255As individually.).The read,write and reset control lines are connected to the \RD,\WR and RESET pins of both of the 8255As and a ready signal is returned if either \CS signal is active. One other alternative in interface design is to treat registers of an I/O device as memory locations.
338
10-1 GENERAL MEMORY ORGANIZATION The memory of a computer system normally consists of one or more PC boards that are conected to the system bus.On each board is a module that is addressed by the high-order bits on the address bus.As shown in Fig.10-1 most systems include both ROM and RAM modules.
339
The general design of a memory module is shown in Fig.10-2.It consists primarily of an interface and an array of memory IC devices.
340
The principal criteria involved in designing a memory are: 1.Cost. 2.Capacity. 3.Speed. 4.Power consumption. 5.Reliability. 6.Volatility and access capability. 10-2 STATIC RAM DEVICES For static memory devices, a cell is commonly implemented using six MOS transistors, as shown in Fig.10-4. The number of memory cells and their arrangement in a static memory device varies considerably.Common sizes range from 256 x 4 to 16K x 1.
341
A 4K x 8 memory device array which is constructed from 1K x 1 devices is shown in Fig.10-6.If a chip is enabled, a read or write operation will proceed as specified by the R/W control input.Otherwise, the read/write signal will not be recognized and the output is forced into a highimpedance state.This allows the data outputs of several memory chips to be directly tied together. When this is done, the bit being output not only depends on the signals on the address lines, but also depends on which chips receive the chip enable signal.Each row in the array is connected to a row enable line and the row enable lines are activated by higher-order address bits, which for this example are bits A11 and A10. In summary, if the address contains 16 bits, A15-A12 would select the module, A11 and A10 would select the row, and A9-A0 would select the bits in the devices which constitute the addressed byte.
342
Figure 10-7(a) illustrates the timing of a memory read cycle.The address is applied at point A, which is the beginning of the read cycle and must be held stable during the entire cycle.In order to reduce the access time, the chip enable input should be applied before point B.The data output becomes valid after point C and remains valid as long as the address and chip enable inputs hold. A typical write cycle is shown in Fig.10-7(b).In addition to the address and chip enable inputs, an active low write pulse on the R/W line and the data to be stored must be applied during the write cycle.
343
An example of the design of a 16K x 8 static RAM memory module for a maximum mode 8088 is shown in Fig.10-8.It is assumed that the \CE and \WE inputs and the D7-D0 pins of the 4K x 8 static RAM, have the following relationships:
344
The incoming address bus splits into two parts, with lines A19-A14 being used to select the module.A13 and A12 are input to the chip enable logic, which is detailed in Fig.10-9(a).The chip enable logic has four outputs, \CE0 through \CE3, only one of which may be active at any one time. \MWTC and the module select line are input to a write pulse generator which is constructed from two monostable multivibrators and is detailed in Fig.109(b).The output of this circuit is connected to the Write Enable (\WE) pins of all the memory devices and causes the data on D7-D0 to be put into the address byte.
345
In a memory refresh cycle, a row address is sent to the memory chips and a read operation is performed to refresh the selected row of cells.However, a refresh cycle differs fro a regular memory read cycle in the following respects: 1.The address input to the memory chips does not come from the address bus.Instead, the row address is supplied by a binary counter called the refresh address counter.This counter is incremented by one for each memory refresh cycle so that it sequences through all the row addresses. The column address is not involved because all elements in a row are refreshed simultaneously. 2.During a memory refresh cycle, all memory chips are enabled so that memory refresh is performed on every chip in the memory module simultaneously. This reduces the number of refresh cycles.In a regular read cycle, at most one row of memory chips is enabled. 3.In addition to the chip enable control input, normally a dynamic RAM has a data output enable control.These two control inputs are combined internally so that the data output is forced to its high-impedance mode unless both control inputs are activated.During a memory refresh cycle, the data output enable control is deactivated. 346
Consider a memory module of 16K bytes implemented by 4K x 1 dynamic RAMs. The memory device array consists of four rows and eight columns.Each chip has 64 rows and 64 columns of memory cells and has separate row address (6 bits) and column address (6 bits) pins. It is assumed that the chip enable and output enable pins are CE and \CS, respectively.The block-diagram in Fig.10-11 shows the logic needed to generate the chip enable and the refresh address signals during a memory refresh.
347
There are several reasons why dynamic RAMs are attractive to memory designers, especially when the memory is large.Three of the main reasons are: 1.High Density - For static RAM, a typical cell requires six MOS transistors.The structure of a dynamic cell is much simpler and can be implemented with three, or even one MOS transistor.As a result, more memory cells can be put into a single chip and the number of memory chips needed to implement a memory module is reduced.A common size for a dynamic RAM chip is 16K x 1, and 64K x 1 devices are also available. 2.Low Power Consumption - The power consumption per bit of a dynamic RAM is considerably lower than that of static RAM.The power dissipation is less than 0.05 mW per bit for dynamic RAM and typically 0.2 mW per bit for static RAM.This feature reduces the system power requirements and lowers the cost.In addition, the power consumption of dynamic RAM is extremely low in standby mode: this makes it very attractive in the design of memory that is made nonvolatile through the use of a backup power source. 3.Economy - Dynamic RAM is less expensive per bit than static RAM. However, dynamic RAM requires more supporting circuitry and, therefore, there is little or no economic advantage when building a small memory system. Intel has made available its 8203 dynamic RAM controller, which is specifically designed to support its 2117,2118 and 2164 dynamic RAM memory devices. Here we will concentrate on the 8203's use with the 2164, which is a 64K x 1 device.The block diagrams for the 2164 and 8203 are shown in Fig.10-12.
348
For a read cycle, \WE must be inactive before the \CAS pulse is applied and remain inactive until the \CAS pulse is over.After the column address is strobed, \RAS is raised and with \RAS high and \CAS low the data bit is made available on DOUT.
For a write cycle the DIN signal should be applied by the time \CAS goes low, but after the \WE pin goes low.The write is performed through the DIN pin while \RAS,\CAS and \WE are all low.The DOUT pin is held at its high-impedance state throughout the write cycle. For the refresh-only cycle only the row address is strobed and the \CAS pin is held inactive.The DOUT pin is kept in its high-impedance state.
349
Figure 10-14 illustrates how an 8203 and thirty-two 2164s could be used to construct a 256K-byte memory module for a maximum mode 8086 MULTIBUS system. This is an Intel design which assumes that the data and address buses are inverted and therefore uses 8283s and 8287s to interface to these buses instead of 8282s and 8286s.
A way of reducing the number of chips needed in the support circuitry for dynamic RAM is to put a set of refresh logic on each memory device, thus permitting the device to refresh itself.Such a device is called an integrated RAM and except for memory accesses sometimes being held up by refresh cycles, the device appears to the user to be a static RAM.An example of this approach is the Intel 2186/7, which is an 8K x 8 integrated RAM.
350
10-4 BACKUP POWER FOR SEMICONDUCTOR MEMORIES One major disadvantage of using MOS RAMs to construct main memory is that the stored information may be lost as a result of even very short power failures.The solution is to provide a backup power supply which will support the system if the main supply fails.
The type and numbers of batteries required in the backup supply are determined by the following factors: 1.The supply current required by the memory modules. 2.The battery discharge characteristics. 3.The size,weight and cost of the batteries. 4.The maximum length of time the memory must be supplied by backup batteries. Because a memory module consists of a memory chip array and supporting logic, the total discharge current requirement can be calculated by:
351
Universal PROM programmer, commands are available under the ISIS-II operating system for performing the following operations: 1.Loading the data to be programmed from a selected input device (disk file, paper tape or system console) into the MDS memory. 2.Displaying or changing data in the MDS memory. 3.Programming a segment of a PROM with the data which are stored beginning at a specified address in the MDS memory. 4.Transferring a block of data in a PROM into memory so that the contents of the PROM may be examined through the system console or used to produce a duplicate PROM. 5.Transferring a block of data from a PROM into a disc file. 6.Comparing a block of data in a PROM with the contents of a segment of memory (program verification).
352
353
11-1 QUEUE STATUS AND THE LOCK FACILITY Although the maximum mode and the 8288 bus controller were introduced in Chap. 8, their multiprocessing features were not considered at that point.
Because the 8086 has a 6-byte instruction queue and the 8088 has a 4-byte queue, the instruction that has just been fetched may not be executed immediately.In order to allow external logic to monitor the execution sequence, a maximum mode 8086/8088 outputs the queue status through its QS1 and QS0 pins.During each clock cycle the queue status is examined and the QS1 and QS0 bits are encoded as follows:
00 - No instruction was taken from the queue. 01 - The first byte of the current instruction was taken from the queue. 10 - The queue was flushed because of a transfer instruction. 11 - A byte other than the first byte of an instruction was taken from the queue. Normally, semaphores are used to ensure that at any given time only one process may enter its critical section of code in which a shared resource is accessed.Let us now reconsider the semaphore implementation.
This implementation works fine for a system in which all of the processes are executed by the same processor, because the processor cannot switch from one process to another in the middle of an instruction. Suppose that processor A is concurrently ready to update a record in memory while processor B is ready to sort the same record.Since the both processors are running independently, they might test the semaphore at the same time. Note that the XCHG instruction requires two bus cycles, one which inputs the old semaphore and one which outputs the new semaphore.It is possible that after processor A fetches the semaphore, processor B gains control of the next bus cycle and fetches the same semaphore. Suppose that the location SEMAPHORE contains a 1 and both processors A and B are executing TRYAGAIN: XCHG SEMAPHORE,AL and 1.Processor A uses the first available bus cycle to get the contents of SEMAPHORE. 2.Processor B uses the next bus cycle to get the contents of SEMAPHORE. 3.Processor A clears SEMAPHORE during the next bus cycle, thus completing its XCHG instruction. 4.Processor B clears SEMAPHORE during the next bus cycle, thus completing its XCHG instruction. After this sequence is through, the AL registers in both processors will contain 1 and the TEST AL,AL
354
instruction will cause the JZ instructions to fail.Therefore, both processors will enter their critical sections of code.
To avoid this problem, the processor that starts executing its XCHG instruction first (which in this example is processor A) must have exclusive use of the bus until the XCHG instruction is completed.On the 8086/8088 this exclusive use is guaranteed by a LOCK prefix:
which for a maximum mode CPU, activates the \LOCK output pin during the execution of the instruction that follows the prefix. The \LOCK signal indicates to the bus control logic that no other processors may gain control of the system bus until the locked instruction is completed.To get around the problem encountered in the above example the XCHG instructions could be replaced with:
TRYAGAIN:
This would ensure that each exchange will be completed in two consecutive bus cycles. Physically, in a loosely coupled system each processing module includes a bus arbiter and the bus arbiters are connected together by special control lines in the system bus.One of these lines is a busy line which is active whenever the bus is in use. If a \LOCK signal is sent to the arbiter controlling the bus, then that arbiter will retain control of the system by holding the busy line active until the \LOCK signal is dropped.Thus, if a processor applies a \LOCK signal throughout the execution of an entire instruction, its arbiter will not relinquish the system bus until the instruction is complete. Another possible application of the bus lock capability is to allow fast execution of an instruction which requires several bus cycles. For example, in a multiprocessor system a block of data can be transferred at a higher speed by using the LOCK prefix as follows: LOCK REP MOVS DEST,SRC During the execution of this instruction the system bus will be reserved for the sole use of the processor executing the instruction.
355
11-2 8086/8088-BASED MULTIPROCESSING SYSTEMS Let us now consider the three fundamental multiprocessor configurations that the 8086 and 8088 are designed to support.
356
357
358
359
360
11-2-3 Loosely Coupled Configurations A loosely coupled configuration provides the following advantages: 1.High system throughput can be achieved by having more than one CPU. 2.The system can be expanded in a modular form.Each bus master module is an independent unit and normally resides on a separate PC board. Therefore, a bus master module can be added or removed without affecting the other modules in the system. 3.A failure in one module normally does not cause a breakdown of the entire system and the faulty module can be easily detected and replaced. 4.Each bus master may have a local bus to access dedicated memory or I/O devices so that a greater degree of parallel processing can be achieved.
In a loosely coupled multiprocessor system, more than one bus master module may have access to the shared system bus.Since each master is running independently, extra bus control logic must be provided to resolve the bus arbitration problem.This extra logic is called bus access logic and it is its responsibility to make sure that only one bus master at a time has control of the bus.Simultaneous bus requests are resolved on a priority basis. There are three schemes for establishing priority: 1.Daisy chaining. 2.Polling. 3.Independent requesting.
361
362
363
364
365
366
367
368
In summary, processing modules of different configurations may be combined to form a complex, loosely coupled multiprocessor system.Each module in such a system may be: 1.A single 8086 or 8088 or an independent processor such as an 8089. 2.A cluster of processors consisting of an 8086 or 8088 and a coprocessor (such as an 8087) and/or independent processors. 3.A cluster of independent processors (such as two 8089s). In addition, each module may include a local bus or a dedicated I/O bus.
11-2-4 Microcomputer Networks The previous multiprocessor configurations have a common characteristics and that is that all processors share the same system bus.Thus, the interprocessor communications are through shared memory and processors must be physically located close to each other. By using serial links, many microcomputer systems can communicate with each other and share some of the same hardware and software resources.Large systems of this type are called computer networks.
369
11-3 THE 8087 NUMERIC DATA PROCESSOR The 8087 numeric data processor (NDP) is specially designed to perform arithmetic operations efficiently.It can operate on data of the integer, decimal, and real types, with lengths ranging from 2 to 10 bytes.The instruction set not only includes various forms of addition, subtraction, multiplication and division, but also provides many useful functions, such as taking the square root, exponentiation, taking the tangent and so on. As an example of its computing power, the 8087 can multiply two 64-bit real numbers in about 27 s and calculate a square root in about 36 s.If performed by the 8086 through emulation, the same operations would require approximately 2 ms and 20 ms, respectively.
370
371
The 8086/8088 can operate on integers of only 16 bits, with a range from - 2^15 to 2^15 - 1.In addition to the word integer format, the 8087 allows integer operands to be represented using 32 bits (short integer) and 64 bits (long integer).Negative integers are coded in 2's complement form.The greater number of bits significantly extends the ranges of integers to - 2^31 through 2^31 - 1 for the short integer format and - 2^63 through 2^63 - 1 for the long integer format.This is roughly 2 x 10^9 and 9 x 10^18, respectively. In the packed BCD format, a decimal number is stored in 10 bytes.The entire most significant byte is dedicated to the sign.The most significant bit of this byte indicates whether a decimal number is positive (0) or negative (1). The remaining 9 bytes represent the magnitude with two BCD digits packed into each byte.Therefore, the valid range of values in the packed BCD format is - 10^18 + 1 to 10^18 - 1. The real data types, also called the floating point types, can represent operands which may vary from extremely small to extremely large values and retain a constant number of significant digits during calculations.A real data format is divided into three fields: sign, exponent and mantissa, i.e.,
X = 2^exp x mantissa
The exponent adjusts the position of the binary point in the mantissa. Decreasing the exponent by 1 moves the binary point to the right by one position.Therefore, a very small value can be represented by using a negative exponent without losing any precision.However, except for numbers whose mantissa parts fall within the range of the format, a number may not be exactly representable, thus causing a roundoff error.If leading 0s are allowed, a given number may have more than one representation in a real number format.But since there are a fixed number of bits in the mantissa, leading 0s increase the roundoff error.Therefore, in order to minimize the roundoff errors, after each calculation the 8087 deletes the leading 0s by properly adjusting the exponent.A nonzero real number is said to be normalized when its mantissa is in the form of 1.F, where F represents a fraction. Normally, a bias value is added to the true exponent so that the true exponent is actually the number in the exponent field minus the bias value.Using biased exponents allows two normalized real numbers of the same sign to be compared by simply comparing the bytes from left to right as if they were integers.
372
The 8087 recognizes three real data types: short real, long real and temporary real.In the short real data format, the biased exponent E and the fraction F have 8 and 23 bits, respectively and the number is represented by the form: (- 1)^S x 2^(E-127) x 1.F where S is the sign bit.Because the 1 appearing before the fraction is always present, it is implied and is not physically stored.For example, suppose that a short real number is stored as follows:
Then the true exponent is 130 - 127 = 3 and the floating point number being represented is
+1.011011 x 2 = 1 x 2 + 1 x 2 + 1 x 2 + 1 x 2 + 1 x 2 = 11.375
To illustrate the conversion of a real number to its short real form, consider the number 20.59375.First, one should convert the integral and fractional parts to binary as follows: 10100 + .10011 = 10100.10011 After normalizing this number by moving the binary point until it is between the first and second bits, it is written: 1.010010011 x 2^4 From this form it is seen that S = 0, E = 127 + 4 = 131 = 10000011 and F = 010010011 and the short real format of the number is:
For the short real data type, the valid range of the biased exponent is 0 < E < 255.Consequently, the numbers that can be represented are from 2^-126 to 2^128, approximately 1 x 10^-38 to 3 x 10^38.
373
A biased exponent of all 1s is reserved to represent infinity or "not-a-number" (NAN).At the other extreme, a biased exponent of all 0s is used to represent +0 (all 0s with + sign), or a denormal.A denormal is a result that causes an underflow and has leading 0s in the mantissa even after the exponent is adjusted to its smallest possible value.NANs and denormals are normally used to indicate overflows and underflows, respectively, although they may be used for other purposes. The long real format has 11 exponent bits and 52 fraction bits.As with the short real format, the first nonzero bit in the mantissa is implied and not stored.The range of representable nonzero quantities is extended to approximately 10^-308 to 10^308.As a comparison the range of nonzero real numbers for the IBM 370 real format is 10^-78 to 10^76. The 8087 internally stores all numbers in the temporary real format which uses 15 bits for the exponent and 64 bits for the mantissa.Unlike the short and long real formats, the most significant bit in the mantissa is actually stored.Because of the extended precision (19 to 20 decimal digits), integer and packed BCD operands can be operated on internally using floating point arithmetic and still yield exact results.A primary reason for using the temporary real format for internal data storage is to reduce the chance for overflow and underflows during a series of calculations which produce a final result that is within the required range. For example, consider the calculation: D <- (A*B)/C where A, B, C and D are in the short real format. The multiply operation may yield a result which is too large to be represented in the short real format, but yet the final result, after the division by C, may still be within the valid range. The 15-bit exponent of the temporary real format extends the range of valid numbers; therefore for most applications the user need not worry about overflow and underflows in the intermediate calculations. In order to support the 8087's data formats in addition to the DB, DW and DD directives, the ASM-86 assembler provides the data declarations directives DQ (define quadword) and DT (define tenbyte) to allocate storage or define data. The directive DQ is used to define an 8-byte storage block for long integer and long real values in the packed decimal or temporary real format. Some examples are given in Fig.11-20. The PTR operator may be used with the DQ or DT type just as they are with DB, DW and DD types.
374
11-3-2 Processor Architecture A general block diagram of the 8087 is given in Fig.11-21. The monitor and control logic maintains a 6-byte instruction queue (only 4 bytes are used if operand in conjunction with the 8088) and tracks the instruction execution sequence of the host. If the instruction currently being executed by the host is an ESC instruction, the 8087 decodes the external op code to perform the specified operation and also captures the operand and operand address. Instructions other than ESC instructions are simply ignored by the 8087. There are eight data registers which can be accessed either as a stack or randomly relative to the top of the stack. An operand may be popped from this stack or pushed onto it. The top stack element is pointed to by the ST bits, which are bits 13, 12 and 11 of the status register. A push operation first decrements ST by 1 and then loads the operand into the new top of the stack element and a pop operation retrieves the top of the stack and then increments ST by 1.
As a conventional register file, each register may be referenced by using an index to the stack pointer. This is called relative stack addressing. For relative stack addressing the registers are considered to be in a circle with register 7 being next to register 0. For instance, if ST contains a 3, then ST(2) and ST(6) represent register 5 and register 1 respectively. Because all numbers are internally stored in the temporary real format, each register consists of 80 bits.
375
The status register is 16 bits wide. It reports various errors, stores the condition code for certain instructions, specifies which register is the top of the stack and indicates the busy status. The bit definitions are given below. A 1 in bit 0 through 5, 7 or 15 indicates that the given condition exists. Bit 0- I, an invalid operation such as stack overflow, stack underflow, invalid operand, square root of a negative number, etc. Bit 1- D, the operand is not normalized. Bit 2- Z, a divide-by-zero error. Bit 3- O, an exponent overflow error, i.e. the biased exponent is too large. Bit 4- U, an exponent underflow, i.e. the biased exponent is too small. Bit 5- P, a precision error, i.e. the result is not exactly representable in the destination format and roundoff has been performed. This indication is normally used only for applications where exact results are required. Bit 6- Reserved. Bit 7- IR, an interrupt request is being made. Bits 8, 9, 10 and 14- C0, C1, C2 and C3 indicate the condition code. The condition code is set by the compare and examine instructions, which are discussed later. Bits 13, 12 and 11- ST, indicates which element is the top of the stack. Bit15- B, the current operation is not complete. After the 8087 is reset or initialized, all status bits except the condition code are cleared. Although the 8087 recognizes six error types, each error type may be individually masked from causing an interrupt by setting the corresponding mask bits to 1 in the control register. These mask bits are denoted IM (invalid operation), DM (denormalized operand), ZM (divide-by-zero), OM (overflow), UM (underflow) and PM (Precision error). If masked, the error will not cause an interrupt but, instead, the 8087 will perform a standard response and then proceed with the next instruction in sequence (For description of the standard responses, see Reference 1). In particular, the precision error, for which the standard response is to "return the rounded result" should be masked for floating point arithmetic because for most applications precision errors will occur most of the time. Precision errors are of consequence only in special situations. Whether an interrupt request, including error-related requests, will be generated also depends on the interrupt enable bit (IEM) in the control register. When this bit is set to 1, all interrupts are disabled except when the CPU is executing a WAIT instruction. If IEM is 0, an unmasked error can cause an interrupt to be sent to the CPU so that the error can be handled by an error-handling-interrupt routine. In the error-handling routine, the current instruction pointer and operand pointer, which are stored in the 8087, can be examined by the 8086/8088 by first putting them into memory. This can be done by using the appropriate 8087 instructions (see Sec.11-3-3). The contents of these pointers identify the instruction and operand address when the error occurred. Note that only the least significant 11 bits of the instruction code are kept in the instruction pointer register because the most significant 5 bits are always 11011, the top code of an ESC instruction.
376
The remaining bits in the control register provide flexibility in controlling precision (PC), rounding (RC) and infinity representations (IC). These bits are defined as follows: PC bits 00 means 24-bit precision. 01 is reserved. 10 means 53-bit precision. 11 means 64-bit precision. RC bits 00 means round to nearest. 01 means round toward -x. 10 means round toward +x. 11 means truncation. IC bit 0 indicates that +x and -x treated as a single unsigned infinitive. 1 indicates that +x and -x are treated as two signed infinitives. During a reset or initialization of the 8087, it sets the PC bits to 11, RC bits to 00, IC bit to 0, IEM bit to 0 and all error mask bits to 1. The tag register holds status of the contents of the data registers. Each data register is associated with two tag bits, which indicate whether its contents are valid (00), zero (01), a special value- i.e. NAN, infinity or decimal- (10) or empty (11). 11-3-3 Instruction Set The 8087 has 68 instructions, which can be divided into six groups according to their functions. These six groups are referred to as the data transfer, arithmetic, comparison, transcendental, constant, and processor control groups. Because the 8087 and the host CPU share the same instruction stream, the ASM-86 assembler allows the user to write programs in a super instruction set which consist of the 8086's and the 8087's instructions. For each 8087 instruction, the assembler generates two machine instructions, a wait instruction followed by an ESC instruction whose external code indicates the 8087's instruction. For example, assuming that SHORT_REAL is defined by a DD directive, the instruction FST SHORT_REAL which converts the contents of the top of the ST stack to the short real format and stores the result into SHORT_REAL, will be assembled as
377
The WAIT instruction preceding the ESC instruction causes the 8086/8088 to enter a wait state until its /TEST pin becomes active and is necessary to prevent the ESC instruction from being decoded before the 8087 completes its current operation. If the /TEST pin is already active or when it becomes active the ESC instruction will be decoded by the 8087 and then both the 8086 and 8087 will proceed in parallel. However a WAIT must be explicitly included whenever the CPU wants to access a memory operand involved in the previous 8087's instruction. For example in the following sequence the CPU must wait until the 8087 has returned its result to SHORT_REAL before it can execute the CMP instruction:
Note that Intel has a software package that emulates all 8087 instructions. For a program to be executed by emulation the FWAIT instruction, which is an alternate mnemonic for the WAIT instruction, must be used for synchronization. Because there is no 8087 to activate the /TEST pin for emulated execution, the package will change any FWAIT or inserted WAIT to a NOP to avoid an endless wait. However, explicit WAIT instructions are not eliminated from the user's object code. Let us now consider the definitions of the 8087's assembler language instructions. Many of the assembler instructions allow the user to specify operands in more then one way, either explicitly or implicitly. For such instructions slashes will be employed to separate the alternate operand forms. For instance the operand field denoted //SRC/DST,SRC means that the instruction may be coded in any one of the following three forms:
1.Both the source and destination operands are implicit (this is indicated by having nothing between the first two slashes) 2.The source operand is specified and the destination operand is implicit 3.Both the source and destination operands are specified by the user.
378
An operand may be a register in the register stack or a memory operand. For a register operand ST represents the top stack element and ST(i) means the ith register below the top stack element (i.e. the register corresponding to ST+i). A memory operand may be addressed by any of the 8086's data addressing modes. The data type of a memory operand may be word integer, short integer, long integer, packed BCD, short real, long real or temporary real. The data transfer group includes the nine instructions which are summarized in Fig.11-22. A memory operand whose type is word integer, short integer, long integer, short real, long real, temporary real or packed BCD can be converted to temporary real and then pushed onto the register stack or a register can be pushed to onto the stack. Conversely the contents of the top stack element can be converted from temporary real to the destination's data format and then stored in memory. In addition instructions are available for popping the contents of a register from the stack and exchanging the contents of a register with the top of the stack. The tag register is updated following each data transfer instruction. The 8087 has a variety of instructions for performing the arithmetic operations: addition, substraction, multiplication and division. Results are always stored on the top of the register stack or in a specified register and one of the source operands must be the top of the stack. For real arithmetic instructions the other operand may be located in a specified register or in memory. Special forms are provided to pop the stack after the arithmetic operation is completed. Because data are internally represented in the temporary real format arithmetic involving operands of other types can always be accomplished by first loading the operands into the appropriate registers using the data transfer instructions and then performing the real arithmetic operation. However, arithmetic instructions are provided that will accept memory operands of the short real, long real, temporary real, word integer or short integer type and automatically convert these operands to temporary real before performing their operations. In addition to the basic arithmetic operations the 8087 provides instructions to calculate the square root, adjust scale values using the power of 2, perform modulo division, round real values to integer values, extract the exponent and fraction, take the absolute value and change the sign. These instructions operate on the top one or two stack elements and the result is left on the top of the stack. Figure 11-23 summarizes the arithmetic instructions. The comparison instructions compare the top of the register stack with the source operand, which may be in another register or in memory, and set the condition codes accordingly. The top stack element may also be compared to 0, or examined to determine its tag, sign or normalization. Condition code settings can be examined by the 8086 or 8088 by storing the status register of the 8087 in memory using a proper 8087 processor control instruction. There are seven instructions in the compare group and they are defined in Fig.11-24.
379
380
381
382
383
The five transcendental instructions calculate tan (0<</4), tan^(-1)(Y/X) (0<Y<X<), 2^X-1 (0<=X<=0.5), Y*log2(X) and Y*log2(X+1) and are defined in Fig.11-25. All transcendental instructions use ST or ST(1) as their operand(s) and the result are stored back on the stack. Other common trigonometric, inverse trigonometric, hyperbolic, inverse hyperbolic, logarithmic and exponential functions can be derived from these five functions through mathematical identities. For example calculation of the natural log of X is equivalent to
As an example involving the trigonometric functions, consider the partial tangent instruction, FPTAN, which computes tan, where in radians is the (ST) and is between 0 and /4. The result is a ratio Y/X with Y replacing and X being pushed onto the stack.The sine function is related to the tangent function as follows: sin=tan / sqrt(1+tan^2()) When 0<</4, sin can be calculated by Y/sqrt(X^2+Y^2) If is outside its acceptable range, then some procedure must be used to obtain a suitable angle that is inside the 0 to /4 range. For example, the instruction FPREM and the identity
can be used to reduce an angle in the range /4 to /2 to the valid range (see Exercise 10).
384
The 8087 provides seven instructions for generating constants. They are used to push +0.0, +1.0, , log2[10], log2[e], log10[2], or ln[2] onto the register stack. These constants are stored in the temporary real format and have an accuracy of up to approximately 19 decimal digits. The constant instructions are summarized in Fig.11-26. The last group of instructions are for manipulating the control and status registers and saving and restoring various portions of the processor's state. Normally, these instructions are required in subroutines and interrupt service routines which need to use the 8087 or for error-handling and process switching. The initialization instruction resets the 8087 to its initial state and is normally required before using the 8087. Several of the instructions load the control register with new contents, thus permitting the control bits to be dynamically changed as circumstances change. Different portions of the 8087's current state may be saved in or restored from memory for various reasons. Three actions that are often needed are: 1.To save status register only (FSTSW) - used when the CPU needs to examine the condition code settings after a comparison instruction. 2.To save the processor environment consisting of the control, status and tag registers and the instruction and operand pointers (FSTENV) used in an error handling routine to identify the cause of the error. 3.To save the entire processor state consisting of the processor environment plus the register stack (FSAVE) - used in subroutines, interrupt service routines and process switching.
In order to terminate the 8087's request, the FSTENV and FSAVE instructions set all error masks in the 8087 to 1 (via initialization) after the registers are stored in memory. (the interrupt enable mask (IEM) bit is also set to 1 by FSAVE so that all interrupts are disabled). The saved image can be restored with the FLDENV or FRSTOR instructions. However, in an error-handling routine it is necessary to set all saved error masks to one before the status is loaded into the 8087. Otherwise, an interrupt will occur immediately.