Practical 5 8
Practical 5 8
Practical 5 8
Compiler
High-Level Language
Machine Language
Assembly Language
Assembler
8086’s Instruction Set
▪ The instruction set of the 8086 microprocessor contains 117 basic instructions.
▪ Number of instruction have been extended due to the use of different operand and addressing modes.
▪ The instructions set of the 8086 have been divided into five categories according to their
functionality:
▪ Data Transfer instructions.
▪ Arithmetic instructions.
▪ Logic Instructions.
▪ Shift instructions.
▪ Rotate Instructions.
Data Transfer Instructions
MOV (MOVE)
• Format: MOV D, S.
• Used to transfer data (byte or word) from:
• Register to register (except CS register).
• Memory location to register (except CS register).
• Immediate operand to register (except CS register).
• Register to memory location.
• Immediate operand to memory location.
• Cannot transfer data between two memory locations (should use intermediate storage such as the
accumulator AX).
• Flag bits within the 8086 are not modified by execution of MOV instruction.
Data Transfer Instructions
MOV (MOVE)
▪ Program 1 ▪ Program 2
MOV AX,0200H MOV AX,0200H
MOV BX,AX MOV BX,1122H
MOV [1212H],BX MOV DS,AX
HLT MOV [1212H],BX
HLT
Data Transfer Instruction
XCHG (Exchange)
▪ Format: XCHG D, S.
▪ The XCHG instruction can be used to swap data between:
▪ Two general purpose registers.
▪ General purpose register and a memory location.
▪ Any general purpose register (including pointers and index registers) and the Accumulator AX.
▪ Flag bits within the 8086 are not modified by execution of XCHG instruction.
▪ Program 3:
MOV AX,1122H
MOV BX,3344H
XCHG AX,BX
HLT
Data Transfer Instruction
XLAT (Translate)
▪ Format: XLAT.
▪ Uses DS, BX, and AL.
▪ DS shows the beginning of the current data segment.
▪ The content of register BX represent the offset of the starting address of the lookup table.
▪ AL represents the offset of the element to be accessed from the beginning of the lookup table.
▪ Since the AL is 8-bit element address permit the table with up to 256 elements.
▪ The value of BX and AL should be initialized prior to the execution of the XLAT instruction.
▪ Execution of the XLAT replaces the content of the AL by the content of the accessed lookup table location
with the physical address:
▪ PA = (DS) 0 + (BX) + (AL).
▪ An example of the use of this instruction would be for software code conversion.
Data Transfer Instruction
LEA (Load Effective Address)
▪ LEA (Load register with effective address).
▪ Format: LEA Reg16, EA.
▪ Loads a specified register with a 16-bit offset address.
▪ Program 4:
VAR DW 'hi there’
LEA BX,var
MOV AX,02h
XLAT
HLT
Data Transfer Instructions
LDS and LES
▪ LDS (Load Register and Data Segment Register).
▪ Format: LDS Reg16, Mem32.
▪ Load a specified register as well as the DS.
▪ Example: LDS SI, [2000H].
▪ LES (Load Register and Extra Segment Register).
▪ Format: LES Reg16, Mem32.
▪ Loads a specified register with 16-bit offset address as well as the ES.
▪ Example: LES SI, EA.
▪ Note: The Flag register is not effective by those instructions.
𝑆 + 𝐷 + 𝐶𝐹 → (𝐷)
OF, SF, ZF, AF, PF and CF
ADC Add with carry ADC D,S 𝐶𝑎𝑟𝑟𝑦 → 𝐶𝐹
▪ AX + SUM to AX
123416 + 00𝐶𝐷16 = 130116 the carry flag remains reset.
▪ BL + 0516 + CF to BL
𝐴𝐵16 + 0516 + 016 = 𝐵016 The carry flag remains reset.
▪ SUM + 1 to SUM
00𝐶𝐷16 + 116 = 00𝐶𝐸16
Arithmetic Instruction
Addition
▪ Example 3:
ADD AL, BL
AAA AL=3216 (2 in ASCII) , BL=3416 (4 in ASCII)
▪ First the addition is performed between AL and BL: 3216 + 3416 = 6616
▪ AAA is an instruction that should be executed immediately after the instruction that adds ASCII data to
adjust the result to its equivalent decimal number by execution the AAA instruction so the result
equivalent decimal number is 6.
▪ Note: if the summation of any two ASCII data exceeds 9; AL contains LSDs and AH is incremented
by 1. Otherwise, AL contains the sum and AH is cleared.
▪ The final result is that (AL) =0616 and (AH) =0016 .
▪ Both AF and CF remain cleared.
▪ The DAA instruction perform a similar function as the AAA but with adjust operation done for the addition of
packed BCD numbers. Note that packed BCD numbers is two BCD digits packed into a byte and the result is a
packed number as well.
Arithmetic Instruction
Addition
▪ Program 6: ADD ▪ Program 7: ADC ▪ Program 8: INC
MOV AX, 1H MOV AX,12H MOV AX,1H
MOV BX, 2H MOV BX,25H MOV BX, 2H
ADD AX,BX ADC AX,BX ADC AX,BX
MOV CX,AX HLT
INC AX
ADD BX,CX
INC BX
HLT
ADD AX,BX
HLT
Arithmetic Instruction
Addition
▪ Program 9 : AAA ▪ Program 9: DAA
MOV AL,32H MOV AL,11
MOV BL, 34H MOV BL, 5
ADD AL,BL ADD AL,BL
AAA DAA
HLT HLT
Arithmetic Instruction
Subtraction
Mnemonics Meaning Format Operation Flag affected
𝐷 − (𝑆) → (𝐷)
SUB Subtraction SUB D,S OF, SF, ZF, AF, PF and CF
𝐶𝑎𝑟𝑟𝑦 → 𝐶𝐹
𝐷 − 𝑆 − 𝐶𝐹 → (𝐷)
SBB Subtract with Borrow SBB D,S OF, SF, ZF, AF, PF and CF
𝐶𝑎𝑟𝑟𝑦 → 𝐶𝐹
DEC Decrement by 1 DEC D 𝐷 − 1 → (𝐷) OF, SF, ZF, AF, and PF
0 − 𝐷 → (𝐷)
NEG Negate NEG D OF, SF, ZF, AF, PF and CF
1 → 𝐶𝐹
SF, ZF, AF, PF and CF
DAS Decimal Adjust for Subtraction DAS
OF is Undefined
AF and CF
AAS ASCII Adjust for Subtraction AAS
Other flags are undefined
Arithmetic Instruction
Subtraction
▪ Example 1: SBB BX, CX BX = 123416 , CX = 012316 , CF = 016
▪ (BX) - (CX) – (CF) = 123416 - 012316 - 016 = 111116 and the result ends up in destination reg. BX.
▪ The carry flag stays clear since that there was no need for borrow.
▪ The NEG (Negate) instruction which can operate on:
▪ General purpose registers.
▪ Storage location in memory.
▪ Example 2: NEG BX BX= 003𝐴16
▪ 0 – BX to BX
▪ 000016 − 003𝐴16 ≫ 000016 + 2’s complement of ( 003𝐴16 ) ≫ 000016 + 𝐹𝐹𝐶616 = 𝐹𝐹𝐶616
▪ Since no carry is generated in this add operation. The carry flag is complemented to give 1.
Arithmetic Instruction
Subtraction
▪ Program 10: SUB and SBB ▪ Program 11: DEC and NEG ▪ Program 12:DAS and AAS
MOV AX,000AH MOV AX,000FH MOV AX,000FH
MOV BX,0001H DEC AX SUB AX,02H
SUB AX,BX MOV AX,0FFFFH DAS
SBB AX,BX NEG AX ADD AL,30H
HLT HLT SUB AL,03H
AAS
HLT
Arithmetic Instruction
Multiplication and Division
Mnemonics Meaning Format Flag affected
OF and SF
MUL Multiply (Unsigned) MUL S
ZF, AF, PF and CF (Undefined)
DIV Divide (unsigned) DIV S OF, SF, ZF, AF, PF and CF (Undefined)
OF and CF
IMUL Integer Multiply (Signed) IMUL S
ZF, AF, PF and SF (Undefined)
IDIV Integer Divide (Signed) IDIV S OF, SF, ZF, AF, PF and CF (Undefined)
SF, ZF and PF
AAM Adjust AL for Multiplication AAM
CF, OF and AF (Undefined)
SF, ZF and PF
AAD Adjust AX for Division AAD
CF, OF and AF (Undefined)
CBW Convert Byte to Word CBW None
CWD Convert Word to Double Word CWD None
Arithmetic Instruction
Multiplication and Division
Mnemonic Operation
S8 S16
MUL
(AL).(S) to (AX) (AX).(S) to (DX).(AX)
S8 S16
Q: (AX)/(S) to (AL) Q: (DX.AX)/(S) to (AX)
DIV
R: (AX)/(S) to (AH) R: (DX.AX)/(S) to (DX)
If Q is 𝑭𝑭𝟏𝟔 type 0 interrupt occur If Q is 𝑭𝑭𝑭𝑭𝟏𝟔 type 0 interrupt occur
S8 S16
IMUL
(AL).(S) to (AX) (AX).(S) to (DX).(AX)
S8 S16
Q: (AX)/(S) to (AL) Q: (DX.AX)/(S) to (AX)
IDIV
R: (AX)/(S) to (AH) R: (DX.AX)/(S) to (DX)
If Q is positive and exceeds 𝟕𝑭𝑭𝑭𝟏𝟔 or if Q is negative and become less than 𝟖𝟎𝟎𝟏𝟏𝟔 type 0 interrupt occur
Q: (AL)/(10) to (AH)
AAM
R: (AL)/(10) to (AL)
(AH).(10)+(AL) to (AL)
AAD 00 to (AH)
CBW (MSB of AL) to All bits of (AH)
CWD (MSB of AX) to All bits of (DX)
Arithmetic Instruction
Multiplication and Division
▪ Example 1: if AL= -1 (2’s complement signed data) and CL= -2 (2’s complement signed data) what is the
result produced in AX by executing the following Instructions:
▪ MUL CL
▪ IMUL CL
▪ As binary data the content of AL and CL are:
▪ 𝐴𝐿 = −1 = 𝑎𝑠 2′ 𝑠 𝑐𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 = 111111112 = 𝐹𝐹16
𝐶𝐿 = −2 = 𝑎𝑠 2′ 𝑠𝑐𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡 = 111111102 = 𝐹𝐸16
▪ Executing the MUL instruction gives:
▪ 𝐴𝑋 = 111111112 × 111111102 = 11111101000000102 = 𝐹𝐷0216
▪ The second instruction multiplies the same two numbers as signed numbers to generate the signed
result. That is:
▪ 𝐴𝑋 = −116 × −216 = 216 = 000216
Arithmetic Instruction
Multiplication and Division
▪ The AAM instruction is for use after you have multiplied two decimal digits together and left the result in AL:
▪ it divides AL by ten and stores the quotient in AH.
▪ It leaves the remainder in AL.
▪ AAD instruction performs the inverse operation to AAM:
▪ It multiplies AH by ten.
▪ Adds it to AL.
▪ Sets AH to zero.
Arithmetic Instruction
Multiplication and Division
▪ MUL Instruction ▪ IMUL Instruction ▪ DIV Instruction
▪ 8 bit: MOV AL,0FFH ▪ 8 bit:
MOV AL,05H MOV CL,0FEH MOV AL,05H
MOV CL,02H MUL CL MOV CL,02H
MUL CL MOV AL,0FFH DIV CL
HLT MOV CL,0FEH HLT
▪ 16 bit: IMUL CL ▪ 16 bit:
MOV AX,0FFFFH HLT MOV AX,0005H
MOV CX,1111H MOV CX,0002H
MUL CX DIV CX
HLT HLT
Arithmetic Instruction
Multiplication and Division
▪ AAM and AAD Instructions ▪ CBW and CWD Instructions
MOV AL,06H MOV AL,80H
MOV CL,04H CBW
MUL CL CWD
AAM HLT
MOV BL,03H
AAD
DIV BL
HLT
Computer Architecture
8086’s Assembly Language: Logic and Shift Instructions
Logic Instructions
SHL AX,01H
SHL AX,CL
CL=02H
Shift Instructions
▪ ARITHMETIC SHIFT: ASL AND ASR ▪ LOGICAL SHIFT: SHL AND SHR
MOV AL,11110000B MOV AL,11110000B
SAL AL,03H SHL AL,03H
SAR AL,03H SHR AL,03H
HLT HLT
Rotate Instructions
ROL AX,01H
ROR AX,CL
CL=04H
Rotate Instructions
Note: The SAHF stores the eight bits of AH into the Flags as shown below, taking in considerations that the
second, fourth, and sixth bits have fixed values.
SF ZF 0 AF 0 PF 1 CF
Flags’ Control Instructions
▪ Program 1 ▪ Program 2
STC MOV AH, 03H
CLC SAHF
CMC MOV AH, 00H
CLI LAHF
STI HLT
HLT
Compare Instruction
▪ This instruction is used to compare two 8-bits or 16-bits numbers.
▪ It compares whether two numbers are equal or unequal.
▪ If not equal then which one is larger?
▪ The result is reflected by changes in six of the status flags of the 8086. It effects OF, SF, ZF, AF, CF,
and PF.
Mnemonics Meaning Format Operation Flags Affected
(D) – (S)
CMP Compare CMP D, S CF, AF, OF, PF, SF, ZF
is used in setting or resetting the flags
▪ The process of comparison performed by the CMP instruction basically by a subtraction operation.
▪ The source is subtracted from the destination operand.
▪ Based on the result of the subtraction, the appropriate flags are set or reset.
Compare Instructions
▪ Example: ▪ Program:
▪ If we compare 5 to 7: MOV AL,05H
▪ Subtract 5 from 7. MOV BL,07H
▪ ZF and CF both become logic 0, which indicate that a
CMP AL,BL
smaller number was compared to a larger one.
CMP BL,AL
▪ If we compare 7 to 5:
▪ Subtract 7 from 5. MOV BL,05H
▪ ZF = 0, and CF=1: which indicates that a larger number CMP AL,BL
have been compared with a smaller number.
HLT
▪ If we compare 5 to 5:
▪ Subtract 5 from 5.
▪ ZF=1, and CF=0: which indicates the equal condition.
Jump Instructions
▪ The purpose of jump instruction is to alter the execution path of the 8086.
▪ It changes the content of the CS and IP, so that instead of running the next instruction a new sequence will
be obtained.
▪ 8086 allows two types of jump instructions:
▪ Unconditional jump: jump to another part of the program without any condition. The left part will not
be executed.
▪ Conditional Jump: checks a condition and then decide whether to jump or not.
Operand Type
Mnemonics Meaning Format Operation Affected Flags Short-label Intrasegment
Jump is initiated Near-label Intrasegment
to the address Far-label Intersegment
JMP Unconditional Jump JMP Operand NONE Memptr16 Intrasegment
specified by the
operand Regptr16 Intrasegment
Memptr32 intersegment
Jump Instructions
▪ There are two basic types of conditional jump:
▪ Intrasegment jump: limited to addresses within the current code segment. It is achieved by just modifying IP
▪ Intersegment jump: permits jumps from one code segment to another. It requires modification to both CS and IP.
▪ Short-Label:
▪ An 8-bit number is coded as an immediate operand to specify the signed displacement of the next instruction to
be executed from the location of the jump instruction.
▪ When Jump instruction is executed, IP is to be Updated by the new value, which is:
▪ (IP) - 2 + the signed displacement
▪ Now; the new IP and the CS both gives the address of the next instruction to be executed.
▪ Allow a jump in the range of -126 and +129 bytes from the location of the jump instruction.
Jump Instructions
▪ Near-Label:
▪ Use 16-bit immediate operand.
▪ This size of offset corresponds to the complete range of the current CS.
▪ The value of the offset is automatically added to IP upon the execution of the instruction.
▪ Program control is passed to the new location identified by the new IP.
▪ Memptr16 and Regptr16:
▪ Just as for Near-Label operand they both permit a jump to any address in the current CS.
▪ Far-Label:
▪ Uses a 32-bit immediate operand to specify the jump to adders.
▪ The first 16 bits of the 32 bits pointer are loaded into IP and are offset address relative to the
contents of the CS register.
▪ The next 16 bits are loaded into CS register and define the new CS.
▪ Memptr32:
▪ Is an indirect way to specify the offset and CS address for an intersegment jump.
Jump Instructions
Mnemonics Meaning Condition Mnemonics Meaning Condition
JA above CF=0 and ZF=0 JNC Not carry CF=0
JAE Above or equal CF=0 JNE Not equal ZF=0
JB Below CF=1 JNG Not greater ((SF xor OF) or ZF) =1
JBE Below or equal CF=1 or ZF=1 JNGE Not greater nor equal (SF xor OF)=1
JNL Not less SF=OF
JC Carry CF=1
JNLE Not less nor equal ZF=0 and SF=OF
JCXZ CX register is zero (CF or ZF) =0
JNO Not overflow OF =0
JE Equal ZF=1
JNP Not party PF=0
JG Grater ZF=0 and SF=OF
JNS Not sign SF=0
JGE Greater or equal SF=OF
JNZ Not zero ZF=0
JL Less (SF xor OF) =1
JO Overflow OF=1
JLE Less or equal ((SF xor OF) or ZF)=1 JP Parity PF=1
JNA Not above CF=1 or ZF =1 JPE Parity even PF=1
JNAE Not above nor equal CF=1 JPO Parity odd PF=0
JNB Not below CF=0 JS Sign SF=1
JNBE Not below nor equal CF=0 and ZF=0 JZ zero ZF=1
Jump Instructions
▪ Program 1:
MOV AL,05H
MOV BL,05H
JMP SECOND
FIRST: ADD AL,BL
JNZ THIRD
SECOND: SUB AL,BL
JZ FIRST
THIRD: MOV AL,0FH
HLT
Subroutine Instructions
▪ Each time the function of a subroutine is needed:
▪ it could be called using the CALL instruction.
▪ The CS and IP of the main program to be stored in the stack once a subroutine is called.
▪ IP and CS are to be returned back from the stack after finishing the subroutine.
▪ RET instruction is used to get back from the subroutine and handling the control once again to the
main program.
▪ The RET instruction causes the stored IP or IP and CS of the main program that have been saved
into the stack to be popped back so that the control is to be returned to the main program.
▪ Program:
MOV AL,05H
MOV BL,06H
CALL JOP
ADD AL,BL
HLT
JOP: INC AL
DEC BL
RET
Push and Pop Instructions
Mnemonics Meaning Format Operation Flags Affected
((SP))← (S)
PUSH Push word into stack PUSH S NONE
(SP)← (SP)-2
(D) ←((SP))
POP POP word off stack POP D NONE
(SP) ←(Sp+2)
▪ Note: content of the flag register could be saved and restored into and from the stack by PUSHF and POPF
respectively. Table below shows those two instructions:
𝑪𝑿 ← 𝑪𝑿 − 𝟏
LOOPNE/LOOPN Loop while not equal Jump to location defined by short-label if 𝑪𝑿 ≠ 𝟎
LOOPNE/LOOPNZ Short-Label
Z Loop while not Zero and 𝒁𝑭 = 𝟎; otherwise execute next sequential
instruction.
Loop Instructions
▪ Program :
MOV AL,01H
MOV CX,0AH
SUM: INC AL
LOOP SUM
HLT