Workshop On 86
Workshop On 86
(INTegrated ELectronics)
By
S. KESHAV MURTHY
CS & E, BIT
Contents to be covered in Day - 1
• X 86 Family
• Features of 8086 – Architecture
• Pipelining in X86
• Addressing modes in 8086
• Instruction sets, Directives
• Interrupts
• Memory Interfacing (16-bit)
About X-86
X -> 80, 801,802,803,804,805
PENTIUM (PENTA+IUM)
P-SERIES (P3,P6 For Multimedia, Parallel
Processing in a Pipeline)
Itanium(i) – New brand name(64-bit) to
meet needs of Internet driven activities for
Servers & Work Stations. It can executes
many instructions simultaneously(ILP)
About X86 Processor Applications
8085 (8-bit) – small static Equipment's like for programming toys
signal light, display devices
Note: when memory is used in instruction 4 the size of the memory has
to be specified by using assembler directives byte ptr / word ptr /
dword ptr
Data transfer instructions
• PUSH instruction: Possible push instructions are :
1. PUSH reg 2. PUSH mem 3.PUSHF 4. PUSHA 5. PUSH sreg
Note: With PUSHF the flag register is pushed on the stack and with
PUSHA instruction AX, CX, DX, BX, SP, BP, SI and DI in the same order
will be pushed.
The sequence of operation on push instruction are :
(SS: ( SP)-1) (Src upper byte)
(SS: (SP)-2) (Src lower byte)
(SP) (SP) - 2
• POP instruction : Possible pop instructions are :
1. POP reg 2. POP mem 3. POPF 4. POPA 5. POP sreg
The sequence is : (dst lower byte) (SS : (SP) )
(dst upper byte) (SS : (SP) + 1)
( SP ) ( SP ) + 2
DATA transfer instructions
• What is the effect of following sequence: PUSH AX
PUSH BX
POP AX
POP BX
(same xchg ax,bx)
• Initialization of the stack:
sseg segment .model small
dw 100h dup (?) (OR) .stack 200h
sseg ends
• LEA instruction: It loads the effective address of the memory specified
into the register specified. LEA reg,mem
(reg) offset address of mem
It is used as a pointer initialization instruction.
example
• Consider swapping of 2 data :
.model small
.data
data1 dw 2000h
data2 dw 3000h
.code
lea si,data1 ; si is the pointer to data1
lea di,data2 ; di is the pointer to data2
mov bx,[si] ; move data1 into bx
mov cx,[di] ; move data2 into cx
mov [si],cx ; move data2 to memory pointed by si
mov [di],bx ; move data1 to memory pointed by di
.exit
end
Data tfr instructions contd….
• LDS and LES instruction:
instruction These load any 16-bit register with an offset
address and the DS, ES segment register with a segment address.
i.e. LDS reg, mem
(reg) = (mem)
( DS ) = (mem+2) LDS AX,DATA or LDS AX,X
• Exchange instructions:
a) XCHG ax, reg16 b) XCHG reg, reg/mem
(AX) (reg16) (reg) (reg/mem)
• LAHF SAHF
(AH) = ( flags lower byte) ( flags lower byte) = (AH)
• IN acc,portaddr8 IN acc,dx
(acc) (portaddr8) ex: IN DX,AL (acc) ((dx))
This is a fixed IO addressing (8-bit addr) This is a variable port addressing (16-bit
OUT DX,AX
addr)
Instructions contd….
• XLAT: This is an instruction most suited for lookup table technique.
The offset address of memory is (BX) + (AL). On execution the data
from DS:OA is brought to AL register
i.e. (AL) ( DS : (BX) + (AL))
When to be used for the lookup table the following steps are to be
followed: 1. Prepare the table as per the requirement.
2. Make BX as pointer to the Table.
3. The number for which conversion is required is loaded in AL
Therefore instruction is :
AND CL , 5bh
Logical instructions
OR instruction: The possible instructions in OR gate operations are:
1. OR reg2 ,reg1 2. OR reg/mem, immdata
3. OR mem,reg 4. OR reg ,mem.
OR instruction can be used for setting a bit of a register or memory.
XOR instructions: The possible instructions in XOR gate operations are:
1. XOR reg2 ,reg1 2. XOR reg/mem, immdata
3. XOR mem,reg 4. XOR reg ,mem.
XOR instructions can be used for complimenting a bit of a register or
memory.
NOT and NEG: NOT instruction is used to get 1’s compliment of the
operand. Instruction is NOT reg/mem.
NEG instruction is used to get 2’s compliment of the operand.
Instruction is NEG reg/mem
Logical instructions
• TEST and BIT test Instruction: TEST instruction performs AND operation
between the 2 operands and then flags are affected based on the result,
but the result is discarded. TEST instruction is followed by JZ ( Jump if
Zero) or JNZ(Jump if not Zero) instruction.
eg. Test whether bit 0 of al-reg. If the bit = 1 branch to RIGHT
TEST AL,1
JNZ RIGHT
• Write a program to clear D0,D1 ; set D2,D3; compliment D6,D7 of a 8-
bit memory data.
i/p: D7 D6 D5 D4 D3 D2 D1 D0
O/P: D7 D6 D5 D4 1 1 0 0
Setting is achieved by OR operation; Clearing by AND operation ;
Complimention by XOR operation.
Logical instructions
• .model small
.data
num db 24h
res db 0
.code
mov ax,@data
mov ds,ax
mov al,num
and al,0FCh; To clear D0,D1;FCh= (1111 1100)
or al,0Ch ; To set D2,D3
xor al,0C0h ; To compliment D6,D7
mov res,al
.exit
end
Shift and Rotate
• Shift instructions move bits in a register or memory left/right. A Left
shift performs a multiplication in power of 2n and a right shift gives
quotient of division by power of 2n . The instructions of shift are:
Cy Register or memory
SHL/ 0
SAL
SHR 0 Cy
SAR Cy
Shift and rotate instruction
• All rotate and shift instructions can be performed once or ‘n’ times. If to be
performed once the syntax is : SHL AX,1
If the operation is to be performed ‘n’ times then the value of ‘n’ is to be
loaded into CL-register. Eg. SHL AX,CL
• Rotate instrutions:
ROL
RCL ROR
RCR
Logical instructions
• Write a program segment to multiply ax by 10.
SHL AX,1 ; Multiplication by 2
MOV BX,AX ;
SHL AX,2 ; Multiplication by 8
ADD AX,BX
MOV AX,00
ADD AX,[DI]
ADD AX,[DI+2]
RET
SUMS ENDP
• Write a procedure to add 2 numbers at X and X+1(x,x+1 are bytes db)
SUMS PROC NEAR
LEA DI,X
MOV AL,00
ADD AL,[DI]
INC DI
ADD AL,[DI]
RET
SUMS ENDP
Arithmetic Instructions(2)
Addition with carry: It is used to add numbers wider than 16-bits.
ADC reg2,reg1;adc ax,bx ADC reg/mem,data
(reg2) = (reg2) + (reg1) + Cy (reg/mem) = (reg/mem)+ data+ Cy
ADC reg,mem;adc ax,z;(ax)+z+(cy)ax
(reg) = (reg) + (mem) + Cy
Write a procedure to add 2, 32-bit numbers
SUMS PROC
LEA DI,X MOV CX,AX
LEA SI,Y RET
MOV AX,[DI] SUMS ENDP
ADD AX,[SI]
MOV BX,AX
MOV AX,[DI+2]
ADC AX,[SI+2]
Subtraction instructions
• 1. SUB reg2 , reg1 2. SUB reg/mem,data
(reg2) = (reg1) - (reg2) (reg/mem)=(reg/mem)-data
(d) <-- (s) –(d)
3. SUB reg, mem 4. DEC reg/mem
(reg) = (mem) - (reg) (reg/mem)= (reg/mem) - 1
7. SBB reg,mem
(reg) = (reg) - (mem) - Cy
ex1. sub ax,bx ex2.sub ax,1234 ex3.sub ax,z ex4.dec ax
Comparision instruction
• CMP instruction is used to compare 2 operands and is used to determine
the relation between the 2 operands. It basically subtracts 2 numbers and
flags are affected and the result is not saved.
CMP op1,op2; cmp ax,bx
After CMP instruction
if CF=1 then op1 < op2 ; if ZF=1 then op1 = op2
CF=0 then op1 ≥ op2
The various instructions are CMP REG2,REG1
CMP REG,MEM
CMP REG/MEM,DATA
The conditional jumps after CMP are JA , JB, JAE, JBE
e.g. to check al<=10
cmp al,10
jbe lesseq
Multiplication and Division
• MUL src ;dest is accumulator by default
If src is 8-bits then (Ax) = (al) * (src)
If the upper part of the product = 00 then CF=OF=0
If the upper part of the product ≠ 00 then CF=OF=1
If src is 16-bits then (DX_AX) = (ax) * (src)
If the upper part of the product = 0000 then CF=OF=0
If the upper part of the product ≠ 0000 then CF=OF=1
• IMUL src
It is similar to MUL but is used for signed multiplication. In case of
signed multiplication the result ,if negative, will be stored in its
equivalent 2’s compliment form.
To perform 5*10 mov bl,5;move 05 to BL
mov al,10
mul bl; AL*BL->AL
Division
• In case of Division instruction (DIV or IDIV) the numerator should be
double the size of the denominator. i.e. it is a 16-bit by 8-bit division or
32-bit by 16-bit division.
• DIV src and IDIV src
If the src is 8-bits then (AH_AL) = (AX) / (src) with AH = rem & AL=quo
If the src is 16-bits then (DX_AX) =(DX_AX)/(src) with DX =rem & AX=quo
• There are 2 possible types of errors from a division operation: Divide by
zero and Divide overflow.
• Divide overflow occurs when the result exceeds the storage size of the
destination register . E.g. if 1500 is to be divided by 2 then the quotient
is 750 and can’t be stored I al-register as it is only a 8-bit register.
• Eg. If AX=0010h (+16) and BL=FDh(-3) then after IDIV AL=FBh (-5) and
AH = 1.
Division
• To make the numerator double size for unsigned numbers make (ah) =
00 and (DX) = 0000.
• To reliaze the same in signed number it is necessary to have a sign
extention. Instructions CBW and CDW are used.
CBW(Convert byte to word) sign extends AL to AH
CWD(Convert word to double word) sign extends AX to DX.
• write a program segment to divide a number at X by no. at X+1
A) UNSIGNED NUMBER B)SIGNED NUMBER
MOV AL,X MOV AL,X
MOV AH,0 CBW
DIV X+1 IDIV X+1
MOV ANSQ,AL MOV ANSQ,AL
MOV ANSR,AH MOV ANSR,AH
BCD & ASCII Arithmetic
DAA : After the addition of 2 bcd numbers the result will be in
hexadecimal. To get a packed decimal result DAA is used. It works on
AL-register only and can be used only after 8-bit addition.
e.g. addition of 2, 16-bit BCD numbers(sum in ch+cL)
mov dx,3099h
mov cx,1234h
mov al,cl
add al,dl ;al=cd h=33d and cy=1
daa ;al=33 ,cy=1
mov cl,al
mov al,ch
adc al,dh
daa
mov ch,al
BCD & ASCII instructions
• DAS : After the subtraction of 2 bcd numbers the result will be in
hexadecimal. To get a packed decimal result DAS is used. It works on
AL-register only and can be used only after 8-bit subtraction.
e.g. sub of 2, 16-bit BCD numbers
mov dx,3099h
mov cx,1234h
mov al,cl
sub al,dl
das
mov cl,al
mov al,ch
sbb al,dh
das
mov ch,al
ASCII arithmetic
There are 4 ascii instructions namely:AAA, AAS, AAM, AAD. These
instructions sets the result to the unpacked bcd which can be converted
to ascii easily by adding 30h which is ascii of 0.
AAA: It adds 2 decimal digit and gives unpacked result in AH and AL
registers. E.g. 7+8 = 0fh on using AAA ; AH = 01 and AL = 05 Now
by adding 30h to AL and AH the number can be converted to ASCII.
MOV AL,07
ADD AL,08
AAA
ADD AX,3030H
AAS: It acts similar to AAA but is used after subtraction of 2 BCD digits.
AAM: After multiplication the result in AL will be hexadecimal. AAM
instruction converts the hexadecimal result into unpaked BCD in AH and
AL.
e.g. 07*08 = 56d = 38h on executing AAM AH=05 and AL=06
ASCII instructions
AAD: This instruction converts the unpacked BCD digits in AH and AL into
a packed hexadecimal number in AL.
e.g. If (AH)=08 (AL)=05 using AAD (AL) =85h
Write a program to display the number in AL in decimal form.
DOS interrupt 21h function code 02 is used for display of a character.
I/p: (AH) = 02 and (DL) = ascii of the character to be displayed.
.model tiny |
.code |
mov al,48h | int 21h
aad | pop ax
div ax,3030h | mov ah,02
mov dl,ah | mov dl,al
push ax | int 21h
mov ah,02 | .exit
Review questions
• If (AX)=1001h and (DX)=20FFh what is the result and the flag values
0001 0000 0000 0001
+ 0010 0000 1111 1111
----------------------------------
0011 0001 0000 0000 = 3100h
Flags: CF=0, ZF=0, OF=0, AC=1, PF=0, SF=0
• Select an instruction that tests bit position 2(d3) of register ch flag z is
modified based on bit3.if d3 is 0,zf=0,else zf=1.
TEST CH,04 (same as AND, dest.is not altered)
• Select an instruction to move DH register right one place, making sure
the sign of the result is the same as the sign of the original number.
SAR DH,1
Processor control instructions
Flag control instructions:
CLD --- clear direction flag STD --- set direction flag,DF=1
CLI ---- Clear interrupt flag STI --- set interrupt flag
CLC ---- Clear Carry flag STC --- set carry flag
CMC --- Compliment carry flag.
Miscellaneous instructions:
WAIT: causes the processor to go to wait state and comes out of wait
state when TEST pin = 0
HLT : causes the processor to terminate all the tasks and go to halt
state. The processor can be restarted by interrupt or reset.
NOP: It is a no operation instruction which causes the processor to
pause for a short time only.
LOCK prefix: when used with an instruction causes LOCK pin to go low
until the instruction is completed. E.g. LOCK MOV AX,DX
ESC: When executed the opcode is given to co-processor for execution.
Program control instruction
• Branching instruction: The value of program pointers (CS and IP) have to
be changes to realise branching. There are 2 types of branching
operations namely : Conditional and unconditional branching.
FAR jump indirect: Indirect Program AM: In case of intra segment indirect the
register or memory specified in the instruction gives the offset value for the
branching. i.e. JMP reg/mem
on execution (IP) = (reg/mem).
In case of inter segment indirect a memory is specified which is a 32-bit
memory. The 1st 16-bit number is taken to IP as the offset and the next 16-bit
number to CS as the segment address.
i.e. JMP dword ptr mem
(IP) = ( mem)
(CS) = ( mem + 2)
Program control instructions
• Conditional jumps: These are always inter segment branching only.
Jcondition label
If the condition is true
(IP) = (IP) + DISPLACEMENT
else
(IP) = unchanged.
• The conditions are based on flags. The flags involved in the conditions are
ZF, CF, PF, OF and SF.
• The various instructions with the condition satisfied and condition unsatisfied
values are given in the table in the next slide:
• E.g. JNC label
If Cy=0
(IP) = (IP) + DISPLACEMENT
else
(IP) = unchanged.
Program control instructions
Instruction Condition safisfied Condition fails
JZ / JE LABEL ZF = 1 ZF = 0
JNZ / JNE LABEL ZF = 0 ZF = 1
JNC LABEL CF=0 CF=1
JC LABEL CF=1 CF=0
JS LABEL SF=1 SF=0
JNS LABEL SF=0 SF=1
JO LABEL OF=1 OF=0
JNO LABEL OF=0 OF=1
JP LABEL PF=1 PF=0
JNP LABEL PF=0 PF=1
JCXZ LABEL CX=0 CX ≠ 0
Program control instructions
Instruction Condition satisfied Condition fails
INSTRUCTIONS USED AFTER UNSIGNED OPERATION
JBE LABEL CF=1 OR ZF=1 CF = 0 AND ZF=0
JB LABEL CF = 1 CF = 0 OR ZF = 1
JA LABEL CF = 0 AND ZF = 0 CF = 1 OR ZF = 1
JAE LABEL CF = 0 CF = 1
INSTRUCTION USED AFTER SIGNED OPERATION
SF ⊕ OF = 1 AND ZF SF ⊕ OF = 0 OR ZF
=0 =1
JL LABEL
JLE LABEL SF ⊕ OF = 1 SF ⊕ OF = 0
SF ⊕ OF = 0 AND SF ⊕ OF = 1 OR
ZF=0 ZF=1
JG LABEL
JGE LABEL SF ⊕ OF = 0 SF ⊕ OF = 1
Program control instructions
• Write a procedure to search a character 0Ah in a string of 100
characters. If found return with CF = 0, if not found return CF=1
SCAN PROC NEAR
MOV CX,100;counter
MOV AL,0AH;char to search
LEA DI,STRING; destination string
CLD
REPNE SCASB
JCXZ NF
CLC;return with CF=0,found
RET
NF: STC;return with cf=1;not found.
RET
SCAN ENDP
Program control instructions
• LOOP instruction: It is a combination of DEC CX followed by JNZ.
It decrements the value in CX-register by 1 and if the value of CX ≠ 0
then it causes a branching to the label specified.
CODE 00001
4. IRET: It’s the last instruction of a interrupt subroutine. It pops from the
stack into IP, CS, and flag register in the same order.
BIOS Int 10h(Interrupt for Monitor)
Syntax:
Mov ah,function number in AH
Int 21h
Function number in AH is
01h read a char AL
02h to print a char <-DL
09h to print name DX
0Ah to accept name->DX
Memory interface
• The most common types of memory are: ROM, Flash ROM, SRAM and
DRAM
• Memory pin connection:
Address Connections: The number of pins required for addressing is
obtained by expressing the memory capacity as power of 2. The power
value is the number of lines required.
eg. Memory capacity = 16KBytes
Expressing as a power of 2 we get 16KBytes = 2 14 .Therefore 14 lines
are required for addressing, typically marked as A0 – A13
(Range of address is 00000h to 03FFFFh)
Data connections: These are the pins through the data is read or written
into the memory. It is labelled as D0 to D7.
Memory are expressed as M X N where M represents the no. of
locations and N represents the bits per location.
Memory interface
• Memory connections contd…….
Selection connections: Each memory device will have a enable signal
called chip select / chip enable / select. It has to be selected to enable
the memory IC. Generally the address lines are used for selection.
Control connections: For ROM there will be only one control signal
called Output enable(OE) where as the RAM will need 2 control signals,
one to read and other to write. These signals are controlled by the
processor through the memory interface.
• ROM memory:
It is a read only memory and permanently stores programs and data
that are resident to the system. It is a non-volatile memory The types of
ROM memory available are : PROM, EPROM, EEPROM, Flast ROM etc.
Memory interface
• The EPROM IC has a number a 27XXX where XXX represents the
number of Kilo bits storable in the IC
E.g, If the IC is 2732 It indicates that 32Kbits or 4KBytes of data can be
stored.
If the IC is 271024 the capacity is 1024kbits or 128KBytes.
• Similarly Static RAM has starting with 6XXX and DRAM with 4xxx
• In case of dynamic RAM a signal for refreshing the memory value is
required apart from the regular pins for address, data, selection and
control.
• ADDRESS DECODING: To interface memory to microprocessor, it is
necessary to decode the address sent from the microprocessor. It
makes the memory function at a unique section of memory. Without
decoding only one memory device can be connected to the processor.
Memory interface
• Simple NAND gate Decoder:
A NAND gate can be used as a decoder to enable the Chip selection of a
memory IC. Generally CE is a low output. Hence NAND is preferred.
NAND provides a low output when all its input are high.
Method of decoding using NAND:
1. Identify the lines of address required based on the memory size.
2. From A0 separate that many lines as identified in step 1.
3. The remaining address lines through NAND gate is used for Chip
selection.
0
1
0
1
Diagram interfacing 64K RAM &
64K ROM
Thank you
Any Question…….. ?