Module 2
Module 2
Instructions
101003/CS500D
Microprocessors and Microcontrollers
Syllabus
categorized as:
● Example:
● MOV AX, 0005H
● Example:
● MOV AX, [5000H]
● Example:
● MOV BX, AX
● ADD AX, BX → Result in AX
Register Indirect Addressing Mode
● Default segment: DS or ES
● Example:
● MOV AX, [BX]
● MOV AX, [DI]
● Effective address: 10H*DS + [BX]
● Effective address: 10H*ES + [DI]
Indexed Addressing Mode
● Example:
● MOV AX, [SI]
● MOV AX, [DI]
displacement with the content of any one of the registers: BX, BP, SI and DI
● Example:
● MOV AX, 50H [BX]
register (BX or BP) to the content of any one of the index registers: SI or DI
● Example:
● MOV AX, [BX] [SI]
displacement with the sum of the content of any one of the base register (BX or
● Example:
● MOV AX, 50H [BX] [SI]
○ Location to which control is transferred lies in a different segment other than the
current one.
Intersegment Direct
Intersegment
Intersegment Indirect
Modes for control
transfer instructions
Intrasegment Direct
Intrasegment
Intrasegment Indirect
Intrasegment Direct Mode
● Address to which control is transferred lies within the same segment in which the
control transfer instruction lies and appears directly in the instruction as an
immediate displacement value.
● Address to which control is transferred lies within the same segment in which the
control transfer instruction lies and the displacement is passed to the instruction
indirectly.
● This addressing mode provides a means of branching from one code segment to
another code segment.
● Here the CS and IP of the destination address are specified directly in the
instruction.
● Ie. As the contents of a memory block containing four bytes, i.e. IP(LSB), IP(MSB),
CS(LSB) and CS(MSB) sequentially.
● The starting address of the memory block may be referred using any of the
addressing modes, except immediate mode.
● IP (LSB) → 2000H
● IP (MSB) → 2001H
● CS (LSB) → 2002H
● CS (MSB) → 2003H
● Data Copy/Transfer Instructions
● Arithmetic Instructions
8086
● Logical Instructions
● String Instructions
● POP ● LDS/LES
Data Copy/Transfer ● XCHG ● LAHF
Instructions ● XLAT
● IN
● SAHF
● PUSHF
● OUT ● POPF
MOV: Move
● Copies data from a specified source to a specified destination.
● Examples:
MOV AX, 5000H
MOV AX, BX
MOV AX, [SI]
MOV AX, [2000H]
PUSH: Push to Stack POP: Pop from Stack
● Pushes the contents of the specified ● Copies from the stack location pointed
register/memory location in the to by the stack pointer to a destination
instruction on to the stack. specified in the instruction.
● Examples: ● Examples:
PUSH BX POP BX
PUSH DS POP DS
PUSH [5000H] POP [5000H]
XCHG: Exchange XLAT: Translate
● Exchanges the content of a register with ● Used for finding out the codes in case of
the content of another register or with code conversion problems, using look
the content of memory location(s). up table technique.
● Example:
MOV AX, SEG TABLE
MOV DS, AX
MOV AL, CODE
MOV BX, OFFSET TABLE
XLAT
IN: Input the port OUT: Output to the port
● Used for reading an input port. ● Used for writing to an output port.
● Input port address may be specified ● Output port address may be specified
directly or indirectly directly or implicitly in DX
● AL and AX → Destinations for 8 bit and ● AL and AX → Sources for 8 bit and 16 bit
16 bit operations respectively. operations respectively.
● Examples: ● Examples:
IN AL, 0300H OUT 0300H, AL
IN AX OUT AX
LEA: Load Effective Address LDS/LES: Load Pointer to
● Loads the offset of an operand in the DS/ES
specified register.
● Loads the DS or ES register and the
● LEA Register, Source specified destination register in the
instruction with the content of memory
● Example: location specified as source in the
LEA BX, ADR instruction.
● Examples:
LDS BX, 5000H
LES BX, 5000H
LAHF: Load AH from lower SAHF: Store AH to lower
byte of flag byte of flag register
● Loads the AH register with the lower ● Sets or resets the condition code flags in
byte of the flag register. the lower byte of the flag register with a
byte from the AH register.
Arithmetic ● DEC
● SUB
● MUL
● CBW
Instructions ● SBB ● CWD
● CMP ● DIV
● NEG ● IDIV
ADD: Add
● Adds data from source to data in destination and store the result in the destination.
● Examples:
ADD AX, 0100H
ADD AX, BX
ADD AX, [SI]
ADD AX, [5000H]
ADD [5000H], 0100H
ADD 0100H
ADC: Add with Carry CMP: Compare
● Same operation as ADD instruction, but ● Compares the source operand with a
adds the carry bit flag to the result. destination operand.
● All condition code flags except carry flag ● All condition code flags except carry flag
are affected by this instruction. are affected by this instruction.
● DIV Source
● 16 bit operation Dividend → AX, Divisor → specified using any addressing mode except immediate, Quotient → AL,
Remainder → AH
● 32 bit operation Dividend → HSW in DX, LSW in AX, Divisor → specified using any addressing mode except
immediate, Quotient → AX, Remainder → DX
● Examples:
DIV BL
DIV CX
IDIV: Signed Division
● Same as DIV with signed operands
● IDIV Source
● 16 bit operation Dividend → AX, Divisor → specified using any addressing mode except immediate, Quotient → AL,
Remainder → AH
● 32 bit operation Dividend → HSW in DX, LSW in AX, Divisor → specified using any addressing mode except
immediate, Quotient → AX, Remainder → DX
● Examples:
IDIV BL
IDIV CX
CBW: Convert Signed Byte to NEG: Negate
Word
● Converts a signed byte to a signed word.
● Replaces the number in a destination
● Copies the sign bit of the byte in AL to with its 2’s complement.
all the bits in AH.
● Destination → register or a memory
● Result in AX location.
● NEG Destination
CWD: Convert Signed Word
● Affects all condition code flags.
to Double Word
● Examples:
● Copies the sign bit of a word in AX to all NEG AL
the bits in DX. NEG BX
● Assumes the AL register as the source and the destination, and hence it requires no operand.
● DAA
● Assumes the AL register as the source and the destination, and hence it requires no operand.
● DAS
● Examples:
AND AX, 0008H
AND AX, BX
AND AX, [5000H]
AND [5000H], DX
AND: Logical AND
● AX → 3F0FH
● OR Destination, Source
● Examples:
OR AX, 0098H
OR AX, BX
OR AX, [5000H]
OR [5000H], 0008H
OR: Logical OR
● AX → 3F0FH
● OR AX, 0098H
● NOT Destination
● Examples:
NOT AX
NOT [5000H]
● AX → 200FH
● NOT AX
● Examples:
XOR AX, 0098H
XOR AX, BX
XOR AX, [5000H]
XOR: Logical Exclusive OR
● AX → 3F0FH
● Each bit of result is set to 1 if both operand bits are 1, else reset to 0
● Examples:
TEST AX, BX
TEST [0500], 06H
TEST [BX] [DI], CX
SHL/SAL: Shift Logical / Arithmetic Left
● Shift the operand word or byte bit by bit to the left and insert zeros in the newly introduced
LSBs.
● Examples:
SHL BX, CL
Bit CF 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Positions
Operand
1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1
SHL Result
1st 1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1 0
SHL Result
2nd 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1 0 0
SHR: Shift Logical Right
● Shift the operand word or byte bit by bit to the right and insert zeros in the newly introduced
MSBs.
● Examples:
SHR BX, CL
Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 CF
Positions
Operand
1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1
Count =1
0 1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1
Count = 2
0 0 1 0 1 0 1 1 0 0 1 0 1 0 0 1 0
SAR: Shift Arithmetic Right
● Shift the operand word or byte bit by bit to the right and insert MSB of the operand in the
newly introduced MSBs.
● Examples:
SAR BX, CL
Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 CF
Positions
Operand
1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1
Count =1
1 1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1
Count = 2
1 1 1 0 1 0 1 1 0 0 1 0 1 0 0 1 0
ROR: Rotate Right without Carry
● Rotates the contents of the destination operand to right (bit-wise). LSB is pushed to CF and
simultaneously to MSB at each operation.
● Examples:
ROR BX, CL
Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 CF
Positions
Operand
1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 1
Count =1
1 1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 1
Count = 2
0 1 1 0 1 0 1 1 1 1 0 1 0 1 1 1 0
ROL: Rotate Left without Carry
● Rotates the contents of the destination operand to left (bit-wise). MSB is pushed to CF and
simultaneously to LSB at each operation.
● Examples:
ROL BX, CL
Bit CF 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Positions
Operand
1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 1
Count =1
1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 1 1
Count = 2
0 1 0 1 1 1 1 0 1 0 1 1 1 0 1 1 0
RCR: Rotate Right through Carry
● Rotates the contents of the destination operand to right (bit-wise). CF is pushed to MSB is
pushed and LSB is pushed to CF at each operation.
● Examples:
RCR BX, CL
Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 CF
Positions
Operand
1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 1 0
Count =1
0 1 0 1 0 1 1 1 1 0 1 0 1 1 1 0 1
Count = 2
1 0 1 0 1 0 1 1 1 1 0 1 0 1 1 1 0
RCL: Rotate Left through Carry
● Rotates the contents of the destination operand to left (bit-wise). CF is pushed to LSB is
pushed and MSB is pushed to CF at each operation.
● Examples:
RCL BX, CL
Bit CF 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Positions
Operand
0 1 0 0 1 1 1 0 1 1 0 1 1 0 1 0 1
Count =1
1 0 0 1 1 1 0 1 1 0 1 1 0 1 0 1 0
Count = 2
0 0 1 1 1 0 1 1 0 1 1 0 1 0 1 0 1
● REP
● MOVSB/MOVSW
● After the byte or a word is moved, SI and DI are automatically adjusted to point to the next
source element and the next destination element.
● If DF is 0, then SI and DI will incremented by 1 after a byte move and by 2 after a word move.
● If DF is 1, then SI and DI will be decremented by 1 after a byte move and by 2 after a word
move.
MOVSB/MOVSW: Move String Byte to String Word
● REP instruction prefix is used with MOVS instruction to repeat if by a value given in CX.
● Example:
● REP is used to repeat the operation till CX is zero or condition in REP prefix is false.
● After each comparison SI and DI are updated based on DF and counter is decremented.
● In case of mismatch the CF and ZF are modified and execution proceeds further.
● Example:
● Otherwise ZF is reset
SCAS: Scan String Byte or String Word
● Scans a string of bytes or words for an operand byte or word specified in AL or AX.
● Direction → DF
● When match is found in the string with the operand, execution stops and ZF is set.
● If no match, ZF is reset
● Otherwise ZF is reset
LODS: Load String Byte or String Word
● Loads AL or AX by contents of a string.
● Direction → DF
● Example:
CLD
MOV SI, OFFSET SOURCE
LODSB
STOS: Store String Byte or String Word
● Stores the AL or AX contents to a string.
● Direction → DF
● Example:
CLD
MOV DI, OFFSET TARGET
STOSB
● CALL
● RET
● INT N
Unconditional ● INTO
● 2 basic types:
● Near Call
○ Call to a procedure, which is in the same code segment as the CALL instruction.
○ Copies the offset of the next instruction after the CALL into the stack → return address
○ Also loads the IP with the offset of the first instruction in the procedure.
○ A RET instruction at the end of the procedure will return execution to the offset saved on the stack
which is copied back to IP.
● Far Call
○ Decrements the stack pointer by 2 and copies the content of the CS register to the stack.
○ Decrements the stack pointer by 2 again and copies the offset of the instruction after the CALL
instruction to the stack → return address
○ Loads CS with the segment base of the segment that contains the procedure
○ Also loads the IP with the offset of the first instruction in the procedure.
○ A RET instruction at the end of the procedure will return execution to the offset saved on the stack
which is copied back to IP.
● Example:
○ CALL MULT
○ CALL BX
RET: Return from the Procedure
● Return execution from a procedure to the next instruction after the CALL instruction which
was used to call the procedure.
● Near Procedure:
○ Return will be done by replacing the IP with a word from the top of the stack (offset of the next
instruction after the CALL or return address).
○ SP will be incremented by 2
● Far Procedure:
○ Return will be done by replacing the IP with a word from the top of the stack (offset of the next
instruction after the CALL or return address). SP will be incremented by 2
○ The CS register is then replaced with a word from the new top of the stack (segment base part of the
return address). SP will be again incremented by 2
INT N: Interrupt Type N
● 256 interrupts are defined corresponding to the types from 00H to FFH
○ Both the IP and the CS register content will be changed to get the destination location → far jump.
● Examples:
JMP CONTINUE
JMP BX
LOOP: Loop Unconditionally
● Repeats a series of instructions some number of times → loaded into CX.
○ If CX = 0 after the auto decrement, execution will simply go on to the next instruction after LOOP.
● Examples:
MOV CX,0005
MOV BX,0FF7H
Label: MOV AX, CODE1
OR BX, AX
AND DX, AX
Loop Label
● JZ/JE ● JB/JNAE/JC
● JNZ/JNE ● JNB/JAE/JNC
● JS ● JBE/JNA
2 JNZ/JNE JUMP NOT EQUAL / JUMP IF NOT ZERO Label Transfer execution control to address ‘Label’, if ZF = 0
3 JS JUMP IF SIGNED / JUMP IF NEGATIVE Label Transfer execution control to address ‘Label’, if SF = 1
4 JNS JUMP IF NOT SIGNED / JUMP IF POSITIVE Label Transfer execution control to address ‘Label’, if SF = 0
7 JP/JPE JUMP IF PARITY / JUMP IF PARITY EVEN Label Transfer execution control to address ‘Label’, if PF = 1
8 JNP JUMP IF NO PARITY / JUMP IF PARITY Label Transfer execution control to address ‘Label’, if PF = 0
ODD
9 JB/JNAE/JC JUMP IF BELOW / JUMP IF NOT ABOVE Label Transfer execution control to address ‘Label’, if CF = 1
OR EQUAL / JUMP IF CARRY
10 JNB/JAE/JNC JUMP IF NOT BELOW / JUMP IF ABOVE Label Transfer execution control to address ‘Label’, if CF = 0
OR EQUAL / JUMP IF NO CARRY
Mnemonic Displacement Operation
11 JBE/JNA JUMP IF BELOW OR EQUAL / JUMP IF Label Transfer execution control to address ‘Label’, if CF = 1
NOT ABOVE or ZF = 1
12 JNBE/JA JUMP IF NOT BELOW OR EQUAL / JUMP Label Transfer execution control to address ‘Label’, if CF = 0
IF ABOVE or ZF = 0
13 JL/JNGE JUMP IF LESS THAN / JUMP IF NOT Label Transfer execution control to address ‘Label’, if neither
GREATER THAN OR EQUAL SF = 1 nor OF = 1
14 JNL/JGE JUMP IF NOT LESS THAN / JUMP IF Label Transfer execution control to address ‘Label’, if neither
GREATER THAN OR EQUAL SF = 0 nor OF = 0
15 JLE/JNG JUMP IF LESS THAN OR EQUAL / JUMP IF Label Transfer execution control to address ‘Label’, if ZF = 1 or
NOT GREATER neither SF nor OF is 1
16 JNLE/JG JUMP IF NOT LESS THAN OR EQUAL / Label Transfer execution control to address ‘Label’, if ZF = 0 or
JUMP IF GREATER at least any one of SF and OF is 1 (Both SF & OF are
not 0)
17 JCXZ JUMP IF THE CX REGISTER IS ZERO Label Transfer execution control to address ‘Label’, if CX = 0
18 LOOPZ/LOOP LOOP WHILE CX = 0 AND ZF = 1 Label Loop through a sequence of instructions from ‘Label’,
E while ZF =1 and CX = 0
19 LOOPNZ/LO LOOP WHILE CX = 0 AND ZF = 0 Label Loop through a sequence of instructions from ‘Label’,
OPNE while ZF =0 and CX = 0
● CLC → Clear CF
● CMC → Complement CF
● STC → Set CF
● Write an 8086 program to add numbers consecutively 10 times starting from 01H.
MOV AX, 01H
MOV BX, 02H
MOV CX, 01H
LBL1: ADD AX, BX
INC BX
INC CX
CMP CX, 0AH
JNZ LBL1
MOV DX, AX
Assembly Language Programming
● Add the contents of memory location 2000H:0500H to contents of 3000H:0600H
and store the result in 5000H:0700H
Assembly Language Programming
● Find out the largest number from an unordered array of sixteen 8-bit numbers
stored sequentially in the memory locations starting at offset 0500H in the segment
2000H.
Assembly Language Programming
● Write an assembly language program for calculating the factorial of a number using
8086 microprocessor. Number is in memory location 2000:0500. Store the result in
the location 2000:0600 onwards.
MOV CX, 2000H : Initialize DS at 2000h
MOV DS, CX
MOV CX, [0500] : loads 0500 Memory location content to CX Register
MOV AX, 0001 : loads AX register with 0001
MOV DX, 0000 : loads DX register with 0000
LABEL1: MUL CX : multiply AX with CX and store result in DX:AX pair
LOOP LABEL1 : runs loop till CX not equal to Zero
MOV [0600], AX : store AX register content to memory location 0600
MOV [0601], DX : store DX register content to memory location 0601
HLT : stops the execution of program
Assembly Language Programming
● Write an assembly language program in 8086 microprocessor to divide a 16 bit
number by an 8 bit number. Divisor is in memory location 4000:0500 and dividend
in 4000:0501. Store the quotient in the location 4000:0600.
MOV CX, 4000 :initialize DS
MOV DS, CX
MOV SI, 500 :assigns 500 to SI
MOV DI, 600 :assigns 600 to DI
MOV BL, [SI] :value of divisor will be stored in BL
INC SI :increment the content of SI by 1
MOV AX, [SI] :value of dividend will be stored in AX
DIV BL :divide the content of AX by BL, after execution of this instruction the quotient get stored
in AL and remainder in AH
MOV [DI], AX :moves the content of AX to [DI]
HLT :stops executing the program and halts any further execution