MP 2
MP 2
MODULE – II
Programming of x86 processor
LES − Used to load ES register and other provided register from the memory.
1. MOV instruction
It is a general purpose instruction to transfer byte or word from register to register, memory to register, register
to memory or with immediate addressing.
General Form: MOV destination, source
Here the source and destination needs to be of the same size, that is both 8 bit or both 16 bit.
MOV instruction does not affect any flags.
Example:-
MOV BX, 00F2H ; load the immediate number 00F2H in BX register
MOV CL, [2000H] ; Copy the 8 bit content of the memory location, at a displacement of
2000H from data segment base to the CL register
MOV [589H], BX ; Copy the 16 bit content of BX register on to the memory location, which
at a displacement of 589H from the data segment base.
MOV DS, CX ; Move the content of CX to DS
2. PUSH instruction
The PUSH instruction decrements the stack pointer by two and copies the word from source to the location
where stack pointer now points. Here the source must of word size data. Source can be a general purpose
register, segment register or a memory location.
The PUSH instruction first pushes the most significant byte to sp-1, then the least significant to the sp-2.
Push instruction does not affect any flags.
Example:-
PUSH CX ; Decrements SP by 2, copy content of CX to the stack (figure shows
execution of this instruction)
PUSH DS ; Decrement SP by 2 and copy DS to stack
3. POP instruction
The POP instruction copies a word from the stack location pointed by the stack pointer to the
destination. The destination can be a General purpose register, a segment register or a memory location. Here
after the content is copied the stack pointer is automatically incremented by two.
The execution pattern is similar to that of the PUSH instruction.
Example:
POP CX ; Copy a word from the top of the stack to CX and increment SP by 2
4. PUSHA instruction
Pushes all general purpose registers onto the stack in the following order: AX, CX, DX, BX, SP, BP, SI,
DI. The value of SP pushed into the stack is its value before the actual push of SP.
5. POPA instruction
This instruction pops the top 8 words off the stack into the 8 general purpose 16/32 bit registers.
Registers are popped in the following order: DI, SI, BP, SP, DX, CX and AX.
The SP value popped from the stack is actually discarded.
6. XCHG instruction
The XCHG instruction exchanges contents of the destination and source. Here destination and source can be
register and register or register and memory location, but XCHG cannot interchange the value of 2 memory
locations.
General Format
XCHG Destination, Source
XLAT
or
XLAT Table_Name
First, you’ll have to store the starting offset address of table into BX register which is done by:
MOV BX, OFFSET Table_Name
Example:
LEA BX, PRICES ;Load BX with offset of PRICE in DS
10. LDS –
LDS Register, Memory address of the first word
This instruction loads new values into the specified register and into the DS register from four
successive memory locations. The word from two memory locations is copied into the specified register and the
word from the next two memory locations is copied into the DS registers. LDS does not affect any flag.
Example:
LDS BX, [4326] ;Copy content of memory at displacement 4326H in DS to BL, content of
4327H to BH. Copy content at displacement of 4328H and 4329H in DS to DS register
11. LES –
LES Register, Memory address of the first word
This instruction loads new values into the specified register and into the ES register from four
successive memory locations. The word from the first two memory locations is copied into the specified
register, and the word from the next two memory locations is copied into the ES register. LES does not affect
any flag.
Example:
LES BX, [789AH] ;Copy content of memory at displacement 789AH in DS to BL, content of 789BH
to BH, content of memory at displacement 789CH and 789DH in DS is copied to ES register
Arithmetic Instructions
These instructions are used to perform arithmetic operations like addition, subtraction, multiplication, division,
etc.
Following is the list of instructions under this group –
Instructions To Perform Addition
ADD − Used to add the provided byte to byte/word to word.
ADC − Used to add with carry.
INC − Used to increment the provided byte/word by 1.
AAA − Used to adjust ASCII after addition.
DAA − Used to adjust the decimal after the addition/subtraction operation.
1. ADD instruction
Add instruction is used to add the current contents of destination with that of source and store the result in
destination. Here we can use register and/or memory locations. AF, CF, OF, PF, SF, and ZF flags are affected
General Format:
ADD Destination, Source
Example:
ADD AL, 0FH ; Add the immediate content, 0FH to the content of AL and store
the result in AL
ADD AX, BX ; AX <= AX+BX
ADD AX,0100H – IMMEDIATE
ADD AX,BX – REGISTER
ADD AX,[SI] – REGISTER INDIRECT OR INDEXED
ADD AX, [5000H] – DIRECT
ADD [5000H], 0100H – IMMEDIATE
ADD 0100H – DESTINATION AX (IMPLICT)
This instruction performs the same operation as ADD instruction, but adds the carry flag bit (which may be set
as a result of the previous calculation) to the result. All the condition code flags are affected by this instruction.
The examples of this instruction along with the modes are as follows:
Example:
ADC AX,BX – REGISTER
ADC AX,[SI] – REGISTER INDIRECT OR INDEXED
ADC AX, [5000H] – DIRECT
ADC [5000H], 0100H – IMMEDIATE
ADC 0100H – IMMEDIATE (AX IMPLICT)
3 . INC instruction
INC instruction adds 1 to any register or memory location, except segment register. AF, CF, OF, PF, SF, and ZF
flags are affected.
Format: INC dest
dest - mem/reg
Example:
INC BL ; BL=BL+1
INC BYTEPTR[BX] ;Adds 1 to the byte contents of data segment memory location addressed by BX
4. AAA instruction
ASCII adjust after addition. Addition of two one digit ASCII coded number will not be ASCII. To
convert the result to ASCII, we use AAA after ADD instruction. The result in AX, when added to
3030H, gives us ASCII coded sum.
Format: AAA
Affects FLAGS: AF CF (OF,PF,SF,ZF undefined)
Example:
MOV AL,31H ;mov ASCII 1 in AL
ADD AL,39H ;The ASCII result should be 10 but we get 6AH in AL
AAA ;Adjusts the result and AX now contains 0100H
ADD AX,3030H ;AX now contains ASCII result 10
5. DAA instruction
Decimal adjust after addition. This instruction follows ADD or ADC to adjust the result into BCD result.
This instruction works only with the AL registers and so must occur 8 bits at a time.
Format: DAA
Affects FLAGS: AF CF (OF,PF,SF,ZF undefined)
Example:
6. SUB instruction
SUB instruction is used to subtract the current contents of destination with that of source and store the result in
destination. Here we can use register and/or memory locations. AF, CF, OF, PF, SF, and ZF flags are affected.
General Format:
SUB Destination, Source
Example:
SUB AL, 0FH ; subtract the immediate content, 0FH from the content of AL and
store the result in AL
SUB AX, BX ; AX <= AX-BX
SUB AX,0100H – IMMEDIATE (DESTINATION AX)
SUB AX,BX – REGISTER
SUB AX,[5000H] – DIRECT
SUB [5000H], 0100H – IMMEDIATE
8. DEC instruction
DEC instruction subtracts 1 from any register or memory location, except segment register. AF, CF, OF, PF, SF,
and ZF flags are affected.
MGM Polytechnic College, Kilimanoor (CT Department) Page 31
MICROPROCESSORS AND INTERFACING (5131)
Example:
DEC BL ; BL=BL-1
DEC BYTEPTR[BX] ;Subtracts 1 from the byte contents of data segment memory location addressed by
BX
9. NEG instruction
Two complements' negation
Subtracts the destination from 0 and saves the 2s complement of "dest" back into "dest".
Affects FLAGS:AF CF OF PF SF ZF
Examples:
NEG AX ;AX is two's complemented
NEG BYTE PTR[BX] ; The byte contents of the data segment memory location addressed by BX
are two's complemented.
Format: DAS
Affects FLAGS: AF, CF (OF,PF,SF,ZF undefined)
Example:
Example
MUL CX ;AX times CX, result high word in DX and low word in AX
This insruction multilplies a signed byte (word) from a source with a signed byte (word) in AL (AX).All
details of IMUL are exactly the same as that of MUL, except that if all bits of the destination are not used by the
result, the result is sign extended. The OF and the CF are both set to 1 if the upper byte (word) of a 16-bit (32-
bit) prodct contains part of the product.
Example:
Multiply 9 and 5
Example
DIV BL ;Divide a word in AX with a byte in BL. Quotient in AL, remainder in AH
Example
IDIV BP ;Signed doubleword in DX and AX divided by signed word in BP
Format: AAD
Affects Flags: PF, SF, ZF
Example
Divide 67 by 9
DIV CH ;Divide AX by unpacked BCD in CH, result: AL = 07 unpacked BCD, AH = 04 unpacked BCD
19. CBW instruction − Used to fill the upper byte of the word with the copies of sign bit of the lower byte.
20. CWD instruction − Used to fill the upper word of the double word with the sign bit of the lower word
Logical Instructions
1. AND instruction
This instruction logically ANDs each bit of the source byte/word with the corresponding bit in the destination
and stores the result in destination. The source can be an immediate number, register or memory location,
register can be a register or memory location.
The CF and OF flags are both made zero, PF, ZF, SF are affected by the operation and AF is undefined.
General Format:
AND Destination, Source
Example:
AND BL, AL ; suppose BL=1000 0110 and AL = 1100 1010 then after the
operation BL would be BL= 1000 0010.
AND CX, AX ; CX <= CX AND AX
AND CL, 08 ; CL<= CL AND (0000 1000)
2. OR instruction
This instruction logically ORs each bit of the source byte/word with the corresponding bit in the destination and
stores the result in destination. The source can be an immediate number, register or memory location, register
can be a register or memory location.
The CF and OF flags are both made zero, PF, ZF, SF are affected by the operation and AF is undefined.
General Format:
OR Destination, Source
Example:
3. NOT instruction
The NOT instruction complements (inverts) the contents of an operand register or a memory location, bit by bit.
The examples are as follows:
Example:
NOT AX ;(BEFORE AX= 1011= B= 16 AFTER EXECUTION AX = 0100 = 4 = 16.
NOT [5000H]
4. XOR instruction
The XOR operation is again carried out in a similar way to the AND and OR operation. The constraints on the
operands are also similar. The XOR operation gives a high output, when the 2 input bits are dissimilar.
Otherwise, the output is zero. The example instructions are as follows:
Example:
XOR AX,0098H
XOR AX,BX
XOR AX,[5000H]
5. TEST instruction
Performs a logical AND of the two operands updating the flags
register without saving the result.
Shift Instructions
Shift instructions move the binary data to the left or right by shifting them within the register or memory
location. They also can perform multiplication of powers of 2^+n and division of powers of 2^-n.
There are two type of shifts
logical shifting
arithmetic shifting
later is used with signed numbers while former with unsigned.
1. SHL/SAL instruction
Both the instruction shifts each bit to left, and places the MSB in CF and LSB is made 0. The destination
can be of byte size or of word size, also it can be a register or a memory location. Number of shifts is indicated
by the count.
All flags are affected
General Format:
SAL/SHL destination, count
2. SHR instruction
This instruction shifts each bit in the specified destination to the right and 0 is stored in the MSB
position. The LSB is shifted into the carry flag. The destination can be of byte size or of word size, also it can be
a register or a memory location. Number of shifts is indicated by the count.
All flags are affected
General Format:
SHR destination, count
Example:
MOV BL, B7H ; BL is made B7H
SHR BL, 1 ; shift the content of BL register one place to the right. Before execution,
B7 B6 B5 B4 B3 B2 B1 B0 CY
1 0 1 1 0 1 1 1 0
After execution,
B7 B6 B5 B4 B3 B2 B1 B0 CY
0 1 0 1 1 0 1 1 1
3. SAR instruction
Shifts the destination right by "count" bits with the current sign
bit replicated in the leftmost bit. The Carry Flag contains the last bit shifted out.
Format:SAR dest,count
dest-mem/reg
Affects FLAGS:CF OF PF SF ZF (AF undefined)
Examples:
SAR SI,2 ;SI is arithmetically shifted 2 places to the right
SAR DATA1,CL ;The contents of data segment addressed by DATA1 is right shifted arithmetically by the
number specified by CL.
Rotate Instructions
Rotate on the other hand rotates the information in a register or memory either from one end to another
or through the carry flag.
1. ROL instruction
This instruction rotates all the bits in a specified byte or word to the left some number of bit positions.
MSB is placed as a new LSB and a new CF. The destination can be of byte size or of word size, also it can be a
register or a memory location. Number of shifts is indicated by the count.
MGM Polytechnic College, Kilimanoor (CT Department) Page 39
MICROPROCESSORS AND INTERFACING (5131)
2. ROR instruction
This instruction rotates all the bits in a specified byte or word to the right some number of bit positions.
LSB is placed as a new MSB and a new CF. The destination can be of byte size or of word size, also it can be a
register or a memory location. Number of shifts is indicated by the count.
All flags are affected
General Format:
ROR destination, count
Example:
MOV BL, B7H ; BL is made B7H
ROR BL, 1 ; shift the content of BL register one place to the right.
Before execution,
B7 B6 B5 B4 B3 B2 B1 B0 CY
1 0 1 1 0 1 1 1 0
After execution,
B7 B6 B5 B4 B3 B2 B1 B0 CY
1 1 0 1 1 0 1 1 1
3. RCR instruction
This instruction rotates all the bits in a specified byte or word to the right some number of bit positions
along with the carry flag. LSB is placed in a new CF and previous carry is placed in the new MSB. The
destination can be of byte size or of word size, also it can be a register or a memory location. Number of shifts is
indicated by the count.
All flags are affected
MGM Polytechnic College, Kilimanoor (CT Department) Page 40
MICROPROCESSORS AND INTERFACING (5131)
General Format:
RCR destination, count
Example:
MOV BL, B7H ; BL is made B7H
RCR BL, 1 ; shift the content of BL register one place to the right.
Before execution,
B7 B6 B5 B4 B3 B2 B1 B0 CY
1 0 1 1 0 1 1 1 0
After execution,
B7 B6 B5 B4 B3 B2 B1 B0 CY
0 1 0 1 1 0 1 1 1
4. RCL instruction
Rotate Through Carry Left
Rotates the bits in the destination to the left "count" times with
all data pushed out the left side reentering on the right. The Carry Flag holds the last bit rotated out.
Examples:
RCL,BX,6 ;BX rotates left through carry 6 places
RCL AH,CL ;AH rotates left through carry by the number specified in CL
String Instructions
String is a group of bytes/words and their memory is always allocated in a sequential order.
Following is the list of instructions under this group −
REP − Used to repeat the given instruction till CX ≠ 0.
REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to another.
CMPS/CMPSB/CMPSW − Used to compare two string bytes/words.
INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the provided memory
location.
OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the provided memory location to
the I/O port.
SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in AL or string word
with a word in AX.
LODS/LODSB/LODSW − Used to store the string byte into AL or string word into AX.
1. MOVS/MOVSB/MOVSW
These instructions copy a word or byte from a location in the data segment to a location in the extra
segment. The offset of the source is in SI and that of destination is in DI. For multiple word/byte transfers the
count is stored in the CX register.
When direction flag is 0, SI and DI are incremented and when it is 1, SI and DI are decremented.
MOVS affect no flags. MOVSB is used for byte sized movements while MOVSW is for word sized.
Example:
CLD ; clear the direction flag to auto increment SI and DI MOV AX, 0000H ;
MOV DS, AX ; initialize data segment register to 0 MOV ES, AX ; initialize extra segment register to 0
MOV SI, 2000H ; Load the offset of the string1 in SI MOV DI, 2400H ; Load the offset of the string2 in DI
MOV CX, 04H ; load length of the string in CX
2. REP/REPE/REPZ/REPNE/REPNZ
REP is used with string instruction; it repeats an instruction until the specified condition becomes false.
Example:
REP => CX=0
REPE/REPZ => CX=0 OR
ZF=0
REPNE/REP => CX=0 OR
NZ ZF=1
.
3. CMPS/CMPSB/CMPSW
CMPS is used to compare the strings, byte wise or word wise. The comparison is affected by subtraction
of content pointed by DI from that pointed by SI. The AF, CF, OF, PF, SF and ZF flags are affected by this
instruction, but neither operand is affected.
MGM Polytechnic College, Kilimanoor (CT Department) Page 42
MICROPROCESSORS AND INTERFACING (5131)
Example:
MOV SI, OFFSET F_STRING ; point first string
4. LODS/LODSB/LODSW
This instruction copies a byte (word) of string from the location pointed to by SI. The SI value is
automatically incremented (depending on the DF value) after each movement by 1 byte (2 bytes) for a byte
string (word string).
This instruction is preferred over MOV for string manipulation due to its treatment of the SI pointer
and the ability to use it with REP prefixes.
5.STOS/STOSB/STOSW
he STOS instruction copies a byte (word) from AL (AX) to a memory location stored in [ES:DI]. DI is
automatically incremented after the copy, the DF determines the increment/decrement.
In effect, it replaces a string element with a byte (word) from AL (AX).
STOS does not affect any flags. After copying the content DI is automatically incremented or
decremented, based on the value of direction flag.
Example:
MOV DL, OFFSET D_STRING ; assign DI with destination address.
STOS D_STRING ; assembler uses string name to determine byte or
word, if byte then AL is used and if of word size, AX is used
6. SCAS/SCASB/SCASW
SCAS(/B/W) compares a byte in AL or a word in AX with a byte or word pointed to by DI in ES. The
direction flag determines the direction of scan.
Used with the REP prefix to find the first occurrence of a specified byte(word) in a string.
MGM Polytechnic College, Kilimanoor (CT Department) Page 43
MICROPROCESSORS AND INTERFACING (5131)
7. INS/INSB/INSW instructions
Loads data from a port to the destination [ES:DI]. DI is incremented or decremented depending on the
value of DF.
Example
Move a word from <port address> to STR
8. OUTS/OUTSB/OUTSW
Transfers a data byte/word from [DS:SI] to a port specified in the DX register. SI is
incremented/decremented depending on the value of the DF.
Example
Move a byte from STR to the port PORT
1. CALL instruction
The CALL instruction is used to transfer execution to a subprogram or procedure. There are two types of
CALL instructions, near and far.
A near CALL is a call to a procedure which is in the same code segment as the CALL instruction. 8086
when encountered a near call, it decrements the SP by 2 and copies the offset of the next instruction after the
CALL on the stack. It loads the IP with the offset of the procedure then to start the execution of the procedure.
A far CALL is the call to a procedure residing in a different segment.
Here value of CS and offset of the next instruction both are backed up in the stack. And then branches to
the procedure by changing the content of CS with the segment base containing procedure and IP with the offset
of the first instruction of the procedure.
Example:
Near call
CALL PRO ; PRO is the name of the procedure
CALL CX ; Here CX contains the offset of the first instruction of
the procedure, that is replaces the content of IP with the content of CX
Far call
CALL DWORD PTR[8X] ; New values for CS and IP are fetched from four memory
locations in the DS. The new value for CS is fetched from [8X] and
[8X+1], the new IP is fetched from [8X+2] and [8X+3].
2. RET instruction
RET instruction will return execution from a procedure to the next instruction after the CALL instruction
in the calling program. If it was a near call, then IP is replaced with the value at the top of the stack, if it had
been a far call, then another POP of the stack is required. This second popped data from the stack is put in the
CS, thus resuming the execution of the calling program. RET instruction can be followed by a number, to
specify the parameters passed.
RET instruction does not affect any flags.
General format:
RET
Example:
p1 PROC ; procedure
declaration.
MOV AX, 1234h ;
RET ; return to
caller.
p1 ENDP
3. JMP Instruction
This is also called as unconditional jump instruction, because the processor jumps to the specified
location rather than the instruction after the JMP instruction. Jumps can be short jumps when the target address
is in the same segment as the JMP instruction or far jumps when it is in a different segment.
General Format:
JMP <targetaddress>
Example:
MOV AL,05H
JMP label1 ; jump over to label
MOV AL, 00H
label1: MOV [2000H], AL
RET
There are many conditional jump instructions like JC : Jump on carry (CF=set)
JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
JAE/JNB − Used to jump if above/not below instruction satisfies.
JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
JC − Used to jump if carry flag CF = 1
JE/JZ − Used to jump if equal/zero flag ZF = 1
JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
JGE/JNL − Used to jump if greater than/equal/not less than instruction satisfies.
JL/JNGE − Used to jump if less than/not greater than/equal instruction satisfies.
JLE/JNG − Used to jump if less than/equal/if not greater than instruction satisfies.
JNC − Used to jump if no carry flag (CF = 0)
JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
JNO − Used to jump if no overflow flag OF = 0
JNP/JPO − Used to jump if not parity/parity odd PF = 0
JNS − Used to jump if not sign SF = 0
JO − Used to jump if overflow flag OF = 1
JP/JPE − Used to jump if parity/parity even PF = 1
JS − Used to jump if sign flag SF = 1
JA/JNBE
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes both CF and ZF to have value equal to 0, else no action is taken.
[dest: addressin the range of -128 bytes to +127 bytes from the address of instruction after JA/JNBE]
Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JA LABEL1; jumps to the address specified by LABEL1 if value in register AX is above the value 0030H
JAE/JNB
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes CF to have value equal to 0, else no action is taken.
Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JAE LABEL1; jumps to the address specified by LABEL1 if value in register AX is above or equal to the
value 0030H
JBE/JNA
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes either the CF or ZF to have value equal to 1, else no action is taken.
Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JBE LABEL1; jumps to the address specified by LABEL1 if value in register AX is below or equal to the
value 0030H
JC
Jumps to the destination label mentioned in the instruction if the value of CF is 1, else no action is taken.
Format: JC dest
[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JC]
Example:
JC LABEL1; jumps to the address specified by LABEL1 if CF is set
JE/JZ
Jumps to the destination label mentioned in the instruction if the ZF is set, else no action is taken.
Example:
JZ LABEL1; jumps to the address specified by LABEL1 if ZF=1
JG/JNLE
Jump if greater/ Jump if not less than or equal
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes ZF to have value equal to 0 and CF and OF to have same values, else no action is taken.
Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JG LABEL1; jumps to the address specified by LABEL1 if value in register AX is more positive than the
value 0030H
JGE/JNL
Jump if greater than or equal/ Jump if not less than
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes CF to have value equal to OF, else no action is taken.
Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JGE LABEL1; jumps to the address specified by LABEL1 if value in register AX is more positive or equal to
the value 0030H
JL/JNGE
Jump if less than/ Jump if not greater than or equal
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes CF to have value not equal to OF, else no action is taken.
Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JL LABEL1; jumps to the address specified by LABEL1 if value in register AX is more negative than the
value 0030H
JLE/JNG
Jump if less than or equal/ Jump if not greater than
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes either the ZF to have value equal to 1 or OF to have value unequal to SF, else no action is
taken.
Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JLE LABEL1; jumps to the address specified by LABEL1 if value in register AX is more negative than or
equal to the value 0030H
JNE/JNZ
Jump if not equal/ Jump if not zero
Jumps to the destination label mentioned in the instruction if the ZF is 0, else no action is taken.
Example:
JNZ LABEL1; jumps to the address specified by LABEL1 if ZF=0
JNO
Jump if no overflow
Jumps to the destination label mentioned in the instruction if the OF is 0, else no action is taken. The
overflow flag is set when a signed arithmetic operation produces a result too large for the destination register or
memory location to store.
Example:
MGM Polytechnic College, Kilimanoor (CT Department) Page 49
MICROPROCESSORS AND INTERFACING (5131)
JNP/JPO
Jump if no parity/ Jump if parity odd
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes PF to have value equal to 0, else no action is taken.
Example:
OR AX, AX; Set flags
JNP LABEL1; even parity is expected, jumps to the address specified by LABEL1 if parity is found to be odd
JNS
Jump if not signed (Jump if positive)
Jumps to the destination label mentioned in the instruction if the SF is set, else no action is taken. If
the sign flag is 0 it indicates a positive signed number. Hence the instruction causes a jump if the result of
previous instruction is positive.
Example:
ADD AL,BL; add signed bytes in AL and BL
JNS LABEL1; jumps to the address specified by LABEL1 if SF=0 due to the add instruction above
JO
Jump if overflow
Jumps to the destination label mentioned in the instruction if the OF is set, else no action is taken. The
overflow flag is set when a signed arithmetic operation produces a result too large for the destination register or
memory location to store.
Format: JO dest
[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JO]
Example:
ADD AL,BL; add signed bytes in AL and BL
JZ LABEL1; jumps to the address specified by LABEL1 if OF=1 due to the add instruction above
JP/JPE
Jump if parity/ Jump if parity even
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes PF to have value equal to 1, else no action is taken.
Example:
OR AX, AX; Set flags
JNP LABEL1; odd parity is expected, jumps to the address specified by LABEL1 if parity is found to be even
JS
Jump if signed (Jump if negative)
Jumps to the destination label mentioned in the instruction if the SF is 0, else no action is taken. If
the sign flag is 0 it indicates a positive signed number. Hence the instruction causes a jump if the result of
previous instruction is negative.
Format: JS dest
[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JS]
Example:
ADD AL,BL; add signed bytes in AL and BL
JS LABEL1; jumps to the address specified by LABEL1 if SF=1 due to the add instruction above
LOOP
Contents of CX register are decremented by 1 and if the new value in CX register is non-zero, a jump is
taken to the destination label mentioned in the instruction, else no action is taken.
Format: LOOP dest
[dest: addressin the range of -128 bytes to +127 bytes from the address of instruction after LOOP]
LOOPE/LOOPZ
Contents of CX register are decremented by 1 and if the new value in CX register is non-zero and the ZF
is set, a jump is taken to the destination label mentioned in the instruction, else no action is taken.
Format: LOOPE dest, LOOPZ dest
[dest: addressin the range of -128 bytes to +127 bytes from the address of instruction after LOOPE/LOOPZ]
LOOPNE/LOOPNZ
Contents of CX register are decremented by 1 and if the new value in CX register is non-zero and the ZF
is 0, a jump is taken to the destination label mentioned in the instruction, else no action is taken.
Format: LOOPNE dest, LOOPNZ dest
[dest: addressin the range of -128 bytes to +127 bytes from the address of instruction after LOOPNE/LOOPNZ]
JCXZ
The Jump if CX is Zero causes a jump to a label given in the instruction if CX contains all zeros;
unsigned comparison is used.
JCXZ does not look at ZF when deciding to jump. Destination label must be in the range of -128 to 127 bytes
from the address of the instruction after the JCXZ instruction.
Flags: None
5. NOP instruction
At the end of NOP instruction, no operation is done other than the fetching and decoding of the
instruction. It takes 3 clock cycles. NOP is used to fill in time delays or to provide space for instructions while
trouble shooting. NOP affects no flags.
1. STC instruction
This instruction sets the carry flag. It does not affect any other flag.
2. CLC instruction
This instruction resets the carry flag to zero. CLC does not affect any other flag.
3. CMC instruction
This instruction complements the carry flag. CMC does not affect any other flag.
4. STD instruction
This instruction is used to set the direction flag to one so that SI and/or DI can be decremented
automatically after execution of string instruction. STD does not affect any other flag.
5. CLD instruction
This instruction is used to reset the direction flag to zero so that SI and/or DI can be incremented
automatically after execution of string instruction. CLD does not affect any other flag.
6. STI instruction
This instruction sets the interrupt flag to 1. This enables INTR interrupt of the 8086. STI does not affect
any other flag.
7. CLI instruction
This instruction resets the interrupt flag to 0. Due to this the 8086 will not respond to an interrupt signal
on its INTR input. CLI does not affect any other flag.
Interrupt Instructions
These instructions are used to call the interrupt during program execution.
INT − Used to interrupt the program during execution and calling service specified.
INTO − Used to interrupt the program during execution if OF = 1
IRET − Used to return from interrupt service to the main program
PRCEDURES
A procedure is group of instructions that usually performs one task. It is a reusable section of a software
program which is stored in memory once but can be used as often as necessary. A procedure can be of two
types.
1) Near Procedure
2) Far Procedure
Near Procedure: A procedure is known as NEAR procedure if it is written (defined) in the same code segment
which is calling that procedure. Only Instruction Pointer (IP register) contents will be changed in NEAR
procedure.
FAR procedure : A procedure is known as FAR procedure if it is written (defined) in the different code
segment than the calling segment. In this case both Instruction Pointer (IP) and the Code Segment (CS) register
content will be changed.
RET instruction : The RET instruction will return execution from a procedure to the next instruction after call
in the main program. At the end of every procedure RET instruction must be executed.
Example:-
MOV AL, 1
MOV BL, 2
CALL m2
CALL m2
CALL m2
CALL m2
m2 PROC
MUL BL ; AX = AL * BL.
RET ; return to caller.
m2 ENDP
END
Reusability of code
Less usage of memory
Development becomes easier
Reduced development time
easier
MACROS
Macro is a group of instruction. The macro assembler generates the code in the program each time where
the macro is “called”.
Macros can be defined by MACRO and ENDM assembler directives. Creating macro is very similar to
creating a new opcode that can used in the program, as shown below.
To use a macro it is only necessary to call it by its name, as if it were another assembler instruction,
since directives are no longer necessary as in the case of the procedures
It is important to note that macro sequences execute faster than procedures because there is no CALL
and RET instructions to execute. The assembler places the macro instructions in the program each time when it
is invoked. This procedure is known as Macro expansion.
Syntax:
Name-of_macro MACRO [ARGUMENT 1,……….ARGUMENT N]
Instructions
ENDM
Example:-
Advantages:
1) Program written with macro is more readable.
2) Macro can be called just writing by its name along with parameters, hence no extra code is required like
CALL & RET.
3) Execution time is less bcz of no linking and returning
4) Finding errors during debugging is easier.
Disadvantages :
1) object code is generated every time a macro is called hence object file becomes lengthy.
2) For large group of instructions macro cannot be preferred.
BCD is Binary Coded Decimal. Its 2 versions are Packed BCD and Unpacked BCD
Eg: 64 is packed BCD Number. Its Unpacked BCD is 06, 04
The steps for converting a Packed BCD number into Unpacked BCD is:-
1. ANDing the BCD number with F0
st
2. The result obtained from the 1 step is shifted right 4 times.
3. ANDing the BCD number with 0F.
MGM Polytechnic College, Kilimanoor (CT Department) Page 56
MICROPROCESSORS AND INTERFACING (5131)
1. 64 AND F0 60
2. Shift right 4 times 06
3. 64 AND 0F 04
BCD Calculations
The DAA (Decimal Adjust after Addition) instruction allows addition of numbers represented in 8-bit
packed BCD code. It is used immediately after normal addition instruction operating on BCD codes. This
instruction assumes the AL register as the source and the destination, and hence it requires no operand.
The effect of DAS (Decimal Adjust after Subtraction) instruction is similar to that of DAA, except
that it is used after a subtraction instruction.
For example in the following program, that NUM1 and NUM2 are decimal numbers coded in BCD format, the
result should be 61
NUM1 DB 27H
NUM2 DB 35H
DAA operation:
For the processor there is no difference between a BCD and a hexadecimal number, all numbers are seen as
hexadecimal numbers. After performing an addition and the result is saved in the AL register, conversion to
decimal is carried out as follows:
Example:
Suppose that the result obtained after adding 27 to 35, is 5CH. To convert this to the value that we would expect
after a decimal addition, the DAA instruction is used.
ASCII OPERATIONS
The ASCII arithmetic instructions function with ASCII-coded numbers. These numbers range in value from
30H to 39H for the numbers 0 to 9. There are four instructions used with ASCII arithmetic operations:
1. AAA (ASCII Adjust after Addition)
2. AAD (ASCII Adjust before Division)
3. AAM (ASCII Adjust after Multiplication)
4. AAS (ASCII Adjust after Subtraction)
2. AAD Instruction
Unlike all other adjustment instructions, the AAD instruction appears before a division.
The AAD instruction requires that the AX register contain a two-digit unpacked BCD number (not
ASCII) before executing.
After adjusting the AX register with AAD, it is divided by an unpacked BCD number to generate a
single-digit result in AL with any remainder in AH.
Example:
MOV BL, 9H
MOV AX, 702H
AAD
DIV BL
The above example show how 72 is unpacked BCD is divided by 9 to produce a quotient of 8. The
0702H loaded into AX register is adjusted by the AAD instruction to 0048H.
MGM Polytechnic College, Kilimanoor (CT Department) Page 58
MICROPROCESSORS AND INTERFACING (5131)
3. AAM Instruction
Adjust the result of the multiplication of two unpacked BCD values to create a pair of unpacked BCD
values.
The AX register is the implied source and destination operand for this instruction.
The AAM instruction is only useful when it follows an MUL instruction that multiplies (binary
multiplication) two unpacked BCD values and stores a word result in AX registers.
4. AAS Instruction
Adjust the result of the subtraction of two unpacked BCD values to create a unpacked BCD result.
The AL register is the implied source and destination for this instructions.
The AAS instruction is only useful when it follows a SUB instruction that subtracts (binary subtraction)
one unpacked BCD valued from another and stores a byte result in the AL register.
If the subtraction produced a decimal carry, the AH register is decremented by 1, and the CF and AF
flags are set.
If no decimal carry occurred, the CF and AF flags are cleared, and the AH register is unchanged