Demp Unit 5
Demp Unit 5
The instructions of 8086 are classified into data transfer, arithmetic, logical, flag manipulation, control transfer,
shift/rotate, string and machine control instructions.
The data transfer instructions include MOV, PUSH, POP, XCHG, XLAT, IN, OUT, LEA, LDS, LES, LSS, LAHF and SAHF
MOV: MOV instruction copies a word or byte of data from a specified source to a specified destination. The
destination can be a register or a memory location. The source can be a register or a memory location or an
immediate number. The general format of MOV instruction is
PUSH: PUSH instruction is used to store the word in a register or a memory location into stack as explained in stack
addressing modes. SP is decremented by 2 after execution of PUSH.
Examples:
POP: POP instruction copies the top word from the stack to a destination specified in the instruction. The destination
can be a general purpose register, a segment register or a memory location. After the word is copied to the specified
destination, the SP is incremented by 2 to point to the next word in the stack.
Examples:
XCHG: The XCHG instruction exchanges the contents of a register with the contents of a memory location. It cannot
exchange directly the contents of two memory locations. The source and destination must both be words or they
must both be bytes. The segment registers cannot be used in this instruction.
Examples:
XLAT: The XLAT instruction is used to translate a byte in AL from one code to another code. The instruction replaces
a byte in the AL register with a byte in memory at [BX], which is data in a lookup table present in memory.
Before XLAT is executed, the lookup table containing the desired codes must be put in data segment and the offset
address of the starting location of the lookup table is stored in BX. The code byte to be translated is put in AL. When
XLAT is now executed, it adds the content of the AL with BX to find the offset address of the data in the above lookup
table and the byte in that offset address is copied to AL.
IN: The IN instruction copies data from a port to AL or AX register. If an 8-bit port is read, the data is stored in AL and
if a 16 bit port is read, the data is stored in AX. The IN instruction has two formats namely fixed port and variable
port.
For the fixed port type IN instruction, the 8-bit address of a port is specified directly in the instruction. With this
form, anyone of 256 possible ports can be addressed.
Examples:
IN AL, 80H; Input a byte from the port with address 80H to AL IN AX, 40H; Input a word from port with address 40H
to AX
For the variable port type IN instruction, the port address is loaded into DX register before the IN instruction. Since
DX is a 16 bit register, the port address can be any number between 0000H and FFFFH. Hence up to 65536 ports are
addressable in this mode. The following example shows a part of the program having IN instruction and the
operations done when the instructions are executed are given in the corresponding comment field.
Examples:
IN AL, DX ; Input a byte from 8-bit port with port address FE50H into AL IN AX, DX ; Input a word from 16-bit port
with port address FE50H into AX
OUT: The OUT instruction transfers a byte from AL or a word from AX to the specified port. Similar to IN instruction,
OUT instruction has two forms namely fixed port and variable port.
This instruction determines the offset address of the variable or memory location named as the source and puts this
offset address in the indicated 16 bit register.
Examples:
LEA BX, COST; Load BX with offset address of COST in data segment where COST is the name assigned to a memory
location in data segment.
LEA CX, [BX][SI]; Load CX with the value equal to (BX)+(SI) where (BX) and (SI) represents content of BX and SI
respectively.
LDS: Load register and DS with words from memory the general form of this instruction is
The LDS instruction copies a word from the memory location specified in the instruction into the register and then
copies a word from the next memory location into the DS register.
LDS is useful for initializing SI and DS registers at the start of a string before using one of the String instructions.
Example:
LDS SI,[2000H]; Copy content of memory at offset address 2000H in data segment to lower byte of SI, content of
2001H to higher byte of SI. Copy content at offset address 2002H in data segment to lower byte of DS and 2003H to
higher byte of DS.
LES, LSS: LES and LSS instructions are similar to LDS instruction except that instead of DS register, ES and SS registers
are loaded respectively along with the register specified in the instruction.
LAHF: This instruction copies the low byte of flag register into AH.
Except SAHF and POPF instructions, all other data transfer instructions do not affect flag register.
Arithmetic and Logical instructions
The data from the source and destination are added and the result is placed in the destination. The source may be
an immediate number, a register or a memory location. The destination can be a register or memory location. But
the source and destination cannot both be memory locations. The data from the source and destination must be of
the same type either bytes or words.
Examples:
ADD AL,[BX]; Add content of AL and the byte from memory at [BX] and store result in AL. The flags AF, CF, OF, PF, SF
and ZF flags are affected by the execution of ADD instruction
ADC: This instruction adds the data in source and destination along with the content of carry flag and stores the
result in the destination. The general format of this instruction is
SUB: The general form of subtract (SUB) instruction is SUB destination, source
It subtracts the number in the source from the number in the destination and stores the result in destination. The
source may be an immediate number, a register or a memory location. The destination can be a register or memory
location. But the source and destination cannot both be memory locations. The data from the source and destination
must be of the same type either bytes or words.
For subtraction, the carry flag (CF) functions as borrow flag. If the result is negative after subtraction, CF is set,
otherwise it is reset. The rules for source and destination are same as that of ADD instruction. The flags AF, CF, OF,
PF, SF and ZF are affected by SUB instruction.
SBB: Subtract with Borrow
SBB instruction subtracts the content of source and content of carry flag from the content of destination and stores
the result in destination. The rules for the source and destination are same as that of SUB instruction. AF, CF, OF, PF,
SF and ZF are affected by this instruction.
INC: The increment (INC) instruction adds 1 to a specified register or to a memory location. The data incremented
may be a byte or word. Carry flag is not affected by this instruction. AF, OF, PF, SF and ZF flags are affected.
Examples:
INC BYTE PTR [BX]; Increment byte in memory at [BX] by 1 INC WORD PTR [SI]; Increment word in memory at [SI] by
1
In the above examples, the term BYTE PTR and WORD PTR are assembler directives which are used to specify the
type of data (byte or word respectively) to be incremented in memory.
DEC: The decrement (DEC) instruction subtracts 1 from the specified register or memory location. The data
decremented may be a byte or word. CF is not affected and AF,OF, PF, SF and ZF flags are affected by this instruction.
NEG: The negate (NEG) instruction replaces the byte or word in the specified register or memory location by its 2’s
complement (i.e. changing the sign of the data). CF,AF, SF, PF, ZF and OF flags are affected by this instruction.
Examples:
NEG BYTE PTR [BX]; Take 2’s complement of the byte in memory at [BX] and store result in the same place.
This instruction compares a byte or word in the source with a byte or word in the destination and affects only the
flags according to the result. The content of source and destination are not affected by the execution of this
instruction. The comparison is done by subtracting the content of source from destination.
AF, OF, SF, ZF, PF and CF flags are affected by the instruction. The rules for source and destination are same as that
of SUB instruction.
Example:
After the instruction CMP AX, DX is executed, the status of CF, ZF and SF will be as follows:
CF ZF SF
If AX=DX 0 1 0
If AX>DX 0 0 0
If AX<DX 1 0 1
MUL: The multiply (MUL) instruction is used for multiplying two unsigned bytes or words. The general form of MUL
instruction is
MUL Source
The source can be byte or word from a register or memory location which is considered as the multiplier. The
multiplicand is taken by default from AL or AX for byte or word type data respectively. The result of multiplication is
stored in AX or DX-AX (i.e. Most significant word of result in DX and least significant word of result in AX) for byte or
word type data respectively. (Note: Multiplying two 8 bit data gives 16 bit result and multiplying two 16 bit data
gives 32 bit result).
Examples:
MUL CH; Multiply AL and CH and store result in AX MUL BX; Multiply AX and BX and store result in DX-AX
If the most significant byte of the 16 bit result is 00H or the most significant word of a 32 bit result is 0000h, both CF
and OF will both be 0s. Checking these flags allows us to decide whether the leading 0s in the result have to be
discarded or not. AF, PF, SF and ZF flags are undefined (i.e. some random number will be stored in these bits) after
the execution of MUL instruction
IMUL: The IMUL instruction is used for multiplying signed byte or word in a register or memory location with AL or
AX respectively and stores the result in AX or DX-AX respectively. If the magnitude of the result does not require all
the bits of the destination, the unused bits are filled with copies of the sign bit. If the upper byte of a 16 bit result or
upper word of a 32 bit result contains only copies of the sign bit (all 0s or all 1s) then CF and OF will both be 0
otherwise both will be 1. AF, PF, SF and ZF are undefined after IMUL. To multiply a signed byte by a signed word, the
byte is moved into a word location and the upper byte of the word is filled with the copies of the sign bit. If the byte
is moved into AL, by using the CBW (Convert Byte to Word) instruction, the sign bit in AL is extended into all the bits
of AH. Thus AX contains the 16 bit sign extended word.
Examples:
IMUL BL; multiply AL with BL and store result in AX IMUL AX; multiply AX and AX and store result in DX-AX
IMUL BYTE PTR [BX]; multiply AL with byte from memory at [BX] and store result in AX IMUL WORD PTR [SI]; Multiply
AX with word from memory at [SI] and store result in DX-AX
DIV: The divide (DIV) instruction is used for dividing unsigned data. The general form of DIV instruction is
DIV source
Where source is the divisor and it can be a byte or word in a register or memory location. The dividend is taken by
default from AX and DX-AX for byte or word type data division respectively.
Examples
DIV DL; Divide word in AX by byte in DL. Quotient is stored in AL and remainder is stored in AH
DIV CX; Divide double word (32 bits) in DX-AX by word in CX. Quotient is stored in AX and remainder is stored in DX
DIV BYTE PTR [BX]; Divide word in AX by byte from memory at [BX]. Quotient is stored in AL and remainder is stored
in AH.
IDIV: The IDIV instruction is used for dividing signed data. The general form and the rules for IDIV instruction are
same as DIV instruction. The quotient will be a signed number and the sign of the remainder is same as the sign of
the dividend.
To divide a signed byte by a signed byte, the dividend byte is put in AL and using CBW (Convert Byte to Word)
instruction, the sign bit of the data in AL is extended to AH and thereby the byte in AL is converted to signed word in
AX. To divide a signed word by a signed word, the dividend byte is put in AX and using CWD (Convert Word to
Double word) instruction, the sign bit of the data in AX is extended to DX and thereby the word in AX is converted to
signed double word in DX-AX.
If an attempt is made to divide by 0 or if the quotient is too large or two low to fit in AL or AX for 8 or 16 bit division
respectively (i.e. either when the result is greater than +127 decimal in 8 bit division or +32767 decimal in 16 bit
division or if the result is less than -128 decimal in 8 bit division or - 32767 decimal in 16 bit division), the 8086
automatically generate a type 0 interrupt. All flags are undefined after a DIV instruction.
This instruction is used to get the result of adding two packed BCD numbers (two decimal digits are represented in 8
bits) to be a BCD number. The result of addition must be in AL for DAA to work correctly. If the lower nibble (4 bits)
in AL is greater than 9 after addition or AF flag is set by the addition then the DAA will add 6 to the lower nibble in
AL. If the result in the upper nibble of AL is now greater than 9 or the carry flag is set by the addition, then the DAA
will add 60H to AL.
Examples:
Consider the execution of the following instructions: ADD AL, CL; AL=10001101=8DH and AF=0 after execution
DAA - Add 0110 (decimal 6) to AL since lower nibble in AL is greater than 9 AL=10010011= 93 BCD and CF=0
DAS is used to get the result is in correct packed BCD form after subtracting two packed BCD numbers. The result of
the subtraction must be in AL for DAS to work correctly. If the lower nibble in AL after a subtraction is greater than
9 or the AF was set by subtraction then the DAS will subtract 6 from the lower nibble of AL. If the result in the upper
nibble is now greater than 9 or if the carry flag was set, the DAS will subtract 60H from AL.
Examples:
DAS; Lower nibble of result is 1111, so DAS subtracts 06H from AL to make AL=0010 1001=29 BCD and CF=0 to
indicate there is no borrow.
AAA (ASCII Adjust after Addition) instruction must always follow the addition of two unpacked BCD operands in AL.
When AAA is executed, the content of AL is changed to a valid unpacked BCD number and clears the top 4 bits of AL.
The CF is set and AH is incremented if a decimal carry out from AL is generated.
Example:
ADD AL, BH ; AL=0BH=11 decimal and CF=0 AAA ; AL=01 and AH=01 and CF=1
Since 05+06=11(decimal)=0101 H stored in AX in unpacked BCD form. When this result is to be sent to the printer,
the ASCII code of each decimal digit is easily formed by adding 30H to each byte.
This instruction always follows the subtraction of one unpacked BCD operand from another unpacked BCD operand
in AL. It changes the content of AL to a valid unpacked BCD number and clears the top 4 bits of AL. The CF is set and
AH is decremented if a decimal carry occurred.
Example:
AAA and AAS affect AF and CF flags and OF, PF, SF and ZF are left undefined. Another salient feature of the above
two instructions are that the input data used in the addition or subtraction can be even in ASCII form of the
unpacked decimal number and still we get the result in ordinary unpacked decimal number form and by adding 30H
to the result , again we get ASCII form of the result.
AAD: The ASCII adjust AX before Division instruction modifies the dividend in AH and AL, to prepare for the division
of two valid unpacked BCD operands. After the execution of AAD, AH will be cleared and AL will contain the binary
equivalent of the original unpacked two digit numbers. Initially AH contains the most significant unpacked digit and
AL contains the least significant unpacked digit.
Example: To perform the operation 32 decimal / 08 decimal Let AH=03H; upper decimal digit in the dividend
DIV CL; Divide AX by CL; AL will contain the quotient and AH will contain the remainder. AAD affects PF, SF and ZF
flags. AF, CF and OF are undefined after execution of AAD.
AAM: The ASCII Adjust AX after Multiplication instruction corrects the value of a multiplication of two valid unpacked
decimal numbers. The higher order digit is placed in AH and the low order digit in AL.
Example:
CL=09 decimal
OR AX, 3030H; To get ASCII code of the result in AH and AL (Note: this instruction is used only when it is needed).
AAM affects flags same as that of AAD.
AND: The AND instruction perform logical AND operation between the corresponding bits in the source and
destination and stores the result in the destination. Both the data can be either bytes or words. The general form of
AND instruction is
The rules for destination and source for AND instruction are same as that of ADD instruction. CF and OF are both 0
after AND. PF, SF and ZF are updated after execution of AND instruction. AF is undefined. PF has meaning only for
ANDing 8-bit operand.
OR: The OR instruction perform logical OR operation between the corresponding bits in the source and destination
and stores the result in the destination. Both the data can be either bytes or words. The general form of OR
instruction is
OR destination, Source
The rules for the source and destination and the way flags are affected for OR instruction are same as that of AND
instruction.
XOR: The XOR instruction performs logical XOR operation between the corresponding bits in the source and
destination and stores the result in the destination. Both the data can be either bytes or words. The general form of
XOR instruction is
The rules for the same source and destination and the way flags are affected for XOR instruction are same as that of
AND instruction.
NOT: The Not instruction inverts each bit (forms the 1’s complement) of the byte or word at the specified
destination. The destination can be a register or a memory location. No flags are affected by the NOT instruction.
Example:
NOT AL; Take 1’s complement of AL NOT BX; Take 1’s complement of BX
TEST: This instruction ANDs the content of a source byte or word with the content of the specified destination byte
or word respectively. Flags are updated, but neither operand is changed. The TEST instruction is often used to set
flags before a conditional jump instruction. The general form of TEST instruction is
The rules for the source and destination are same as that of AND instruction and the way flag are affected is also
same as that of AND instruction.
Example:
• Mnemonics • Function
Mnemonics Descripton
Shift and Rotate instructions: The Shift instructions perform logical left shift and right shift, and arithmetic left shift
and right shift operation. The arithmetic left shift (SAL) and logical left shift (SHL) have the same function.
The destination can be a register or a memory location and it can be a byte or a word. This instruction shifts each bit
in the specified destination some number of bit positions to the left. As a bit is shifted out of the LSB position, a 0 is
put in the LSB position. The MSB will be shifted into carry flag (CF).
If the number of shifts to be done is 1 then it can be directly specified in the instruction with count equal to 1. For
shifts of more than one bit position, the desired number of shifts is loaded into the CL register and CL is put in the
count position of the instruction. CF, SF and ZF are affected according to the result. PF has meaning only when AL is
used as destination. SAL instruction can be used to multiply an unsigned number by a power of 2. Doing one bit or
two bits left shift of a number multiplies the number by 2 or 4 respectively and so on.
Examples:
SAL AX,1; Shift left the content of AX by 1 bit SAL BL,1; Shift left the content of BL by 1 bit
SAL BYTE PTR [SI],1; Shift left the byte content of memory at [SI] by 1 bit
The destination can be a register or a memory location and it can be a byte or a word. This instruction shifts each bit
in the specified destination some number of bit positions to the right. As a bit is shifted out of the MSB position, a
copy of the old MSB is put in the MSB position (i.e. the sign bit is copied into the MSB). The LSB will be shifted into
carry flag (CF).
The rules for the Count in the instruction are same as that of the SAL instruction. CF, SF and ZF are affected according
to the result. PF has meaning only when AL is used as destination.
SHR:
The destination can be a register or a memory location and it can be a byte or a word. This instruction shifts each bit
in the specified destination some number of bit positions to the right. As a bit is shifted out of the MSB position, a 0
is put in the MSB position. The LSB will be shifted into carry flag (CF).
The rules for the Count in the instruction are same as that of the SHL instruction. CF, SF and ZF are affected
according to the result. PF has meaning only when 8 bit destination is used.
ROR: This instruction rotates all the bits of the specified byte or word by some number of bit positions to the right.
The operation done when ROR is executed is shown below.
The data bit moved out of the LSB is also copied into CF. ROR affects only CF and OF. For single bit rotate, OF will be
1 after ROR, if the MSB is changed by the rotate. ROR is used to swap nibbles in a byte or to swap two bytes within a
word. It can also be used to rotate a bit in a byte or word into CF, where it can be checked and acted upon by the JC
and JNC instruction. CF will contain the bit most recently rotated out of LSB in the case of multiple bit rotate.
The rules for count are same as that of the shift instruction which is discussed above. Examples:
ROR BX, CL : Rotate right word in BX by number of bit positions given by CL.
ROL: ROL rotates all the bits in a byte or word in the destination to the left either by 1 bit position and more than 1
bit positions using CL as shown below:
RCR: Rotate the byte or word in the destination right through carry flag (CF) either by one bit position or the number
of bit positions given by the CL as shown below.The flag are affected by similar to ROR.
RCL: Rotate the byte or word in the destination left through carry flag (CF) either by one bit position or the number
of bit positions given by the CL as shown below.The flags are affected similar to ROL.
String Instructions
The string instructions operate on element of strings of bytes or word. Registers SI and DI contain the offset address
within a segment of an element (Byte or Word) in the source string and the destination string respectively. The
source string is in the data segment at the offset address given by SI and destination string is in the extra segment at
the offset address given by DI.After each string operation, SI and/or DI are automatically incremented or
decremented by 1 or 2 (for byte or word operation) according to the D flag in the flag register. If D=0, SI and/or DI
are automatically incremented and if D=1, SI and/or DI are automatically decremented.
MNEMONICS FUNCTION
CMPSB Compare string byte (Done by subtracting byte at ES:[DI] from the byte at DS:[SI
affected and the content of bytes compared is unaffected.
CMPSW Compare string word (Done by subtracting word at ES:[DI] from the word at DS:[SI
affected and the content of words compared is unaffected.
SCASB Compare string byte (Done by subtracting byte at ES:[DI] from the byte at ). Only flags
the content of bytes compared is unaffected.
SCASW Compare string word (Done by subtracting word at ES:[DI] from the byte at AX). Only
and the content of words compared is unaffected.
REPE or REPZ Decrement CX and Repeat the following string operation if CX ≠ 0 and ZF=1.
REPNE or REPNZ Decrement CX and Repeat the following string operation if CX ≠ 0 and ZF=0.
LOCK: The lock instruction asserts for the processor an exclusive hold on the use of the system bus. It activates an
external locking signal ( ) of the processor and it is placed as a prefix in front of the instruction for which a lock is to
be asserted. The lock will function only with the XCHG, ADD, OR, ADC, SBB, AND, SUB, XOR, NOT, NEG, INC and DEC
instructions, when they involve a memory operand. An undefined opcode trap interrupt will be generated if a LOCK
prefix is used with any instruction not listed above.
NOP: No operation. This instruction is used to insert delay in software delay programs.
ESC: This instruction is used to pass instructions to a coprocessor such as the 8087, which shares the address and
data bus with an 8086. Instructions for the coprocessor are represented by a 6- bit code embedded in the escape
instruction.
As the 8086 fetches instruction bytes from memory the coprocessor also catches these bytes from the data bus and
puts them in a queue. However the coprocessor treats all the normal 8086 instructions as NOP instruction. When the
8086 fetches an ESC instruction, the coprocessor decodes the instruction and carries out the action specified by the
6- bit code specified in the instruction.
WAIT: When this instruction is executed, the 8086 checks the status of its input pin and if the input is high, it enters
an idle condition in which it does not do any processing. The 8086 will remain in this state until the 8086’s input pin
is made low or an interrupt signal is received on the INTR or NMI pins. If a valid interrupt occurs while the 8086 is in
this idle state, it will return to the idle state after the interrupt service routine is executed. WAIT instruction does not
affect flags. It is used to synchronize 8086 with external hardware such as 8087 coprocessor.
ASSEMBLERDIRECTIVES
An assembler directive is a message to the assembler that tells the assembler something it needs to know in order
to carry out the assembly process. Or a statement in an assembly-language program that gives instructions to the
assembler. They are describedbelow.
-------------------------------------------------------------------------------------------------------------------------------------------
ASSUME
DB - Defined Byte.
DW - Define Word
-------------------------------------------------------------------------------------------------------------------------------------------
ASSUME Directive:
The ASSUME directive is used to tell the assembler that the name of the logical segment should be used
for a specified segment. The 8086 works directly with only 4 physical segments: a Code segment, a data segment,
a stack segment, and an extra segment.
Example:
ASUMECS:CODE ; this tells the assembler that the logical segment named CODE contains the
instruction statements for the program and should be treated as a code segment.
ASUMEDS:DATA ; this tells the assembler that for any instruction which refers to a data in the data
segment, data will found in the logical segmentDATA.
DB: DB directive is used to declare a byte-type variable or to store a byte in memory location.
Example:
2. NAME DB‘ABCDEF’ ; Declare an array of 6 bytes and initialize with ASCII code forletters
;but leave the 100 bytes uninitialized. Program instructions will load
DW: The DW directive is used to define a variable of type word or to reserve storage location of type word in
memory.
Example:
;This variable is initialized with the value 437Ah when it is loaded into
memory to run.
STOR1 DW100DUP(0) ; Reserve an array of 100 words of memory and initialize all words with
0000.Array is named asSTOR1.
---------------------------------------------------------------------------------------------------------------------------------------------
EQU - Equate
EXTRN
---------------------------------------------------------------------------------------------------------------------------------------------
END: END directive is placed after the last statement of a program to tell the assembler that this is the end of the
program module. The assembler will ignore any statement after an END directive. Carriage return is required after
the END directive.
ENDP: ENDP directive is used along with the name of the procedure to indicate the end of a procedure to the
assembler
Example:
SQUARE_NUMPROCE ; It start the procedure Some steps to find the square root of a number
ENDS - This ENDS directive is used with name of the segment to indicate the end of that logic segment.
Example:
EQU: This EQU directive is used to give a name to some value or to a symbol. Each time the assembler finds the
name in the program, it will replace the name with the value or symbol you given to that name.
Example:
FACTOREQU03H ; you has to write this statement at the starting of your program and later in the
program you can use this asfollows
ADDAL, FACTOR ; When it codes this instruction the assembler will code it as ADDAL,03H
; The advantage of using EQU in this manner is, if FACTOR is used many no of
times in a program and you want to change the value, all you had to do is change
the EQU statement at beginning, it will changes the rest of all.
EVEN: This EVEN directive instructs the assembler to increment the location of the counter to the next even
address if it is not already in the even address. If the word is at even address 8086 can read a memory in 1
buscycle.
If the word starts at an odd address, the 8086 will take 2 bus cycles to get the data. A series of words can
be read much more quickly if they are at even address. When EVEN is used the location counter will simply
incremented to next address and NOP instruction is inserted in that incremented location.
Example:
DATA1 SEGMENT; Location counter will point to 0009 after assembler reads nextstatement SALES DB 9DUP
(?) ; declare an array of 9bytes
RECORD DW 100DUP (0) ; Array of 100 words will start from an even address for quicker read
DATA1ENDS
------------------------------------------------------------------------------------------------------------------------------------------
LABEL
NAME
OFFSET
ORG - Originate
------------------------------------------------------------------------------------------------------------------------------------------
GROUP - The GROUP directive is used to group the logical segments named after the directive into one logical
group segment.
LABEL: LABEL is used to assign a name to the current memory location. If the memory location is within the same
segment then NEAR label is used and if the memory location is available in other than the current segment then
FAR label is used.
OFFSET: Offset directive is used to determine the offset address of the label.
ORG: The Origin directive indicates the start of the memory locations for segments (CODE,DATA,STACK)
INCLUDE - This INCLUDE directive is used to insert a block of source code from the named file into the current
source module.
------------------------------------------------------------------------------------------------------------------------------------------
PROC - Procedure
PTR - Pointer
PUBLC
SEGMENT
SHORT
TYPE
--------------------------------------------------------------------------------------------------------------------------------------------
PROC: The PROC directive is used to identify the start of a procedure. The term near or far is used to specify the
type of theprocedure.
Example:
SMART PROCFAR ; This identifies that the start of a procedure named as SMART and instructs the
assembler that the procedure isfar.
SMART ENDP
This PROC is used with ENDP to indicate the break of the procedure.
PTR: This PTR operator is used to assign a specific type of a variable or to a label.
Example:
INC [BX] ; this instruction will not know whether to increment the byte pointed to by BX or
a word pointed to byBX.
This PTR operator can also be used to override the declared type of variable. If we want to access the a byte in
anarray
PUBLIC/EXTRN - The PUBLIC directive is used to instruct the assembler that a specified name or label will be
accessed from other modules.
Example:
PUBLICDIVISOR,DIVIDEND ; these two variables are public so these are available to all modules. If an
instruction in a module refers to a variable in another assembly module, we can access that module by declaring
as EXTRNdirective.
TYPE - TYPE operator instructs the assembler to determine the type of a variable and determines the number of
bytes specified to that variable.
Example:
ADD BX, TYPE WORD_ ARRAY; hear we want to increment BX to point to next word in an array of words.
Procedures
Procedure is a part of code that can be called from your program in order to make some specific task.
Procedures make program more structural and easier to understand. Generally procedure returns to the same
point from where it was called.
name PROC
; of the procedure...
RET
name ENDP
name - is the procedure name, the same name should be in the top and the bottom, this is used to check
correct closing of procedures. The RET instruction is used to return from procedure
Macros
Macros are just like procedures, but not really. Macros look like procedures, but they exist only until your code
is compiled, after compilation all macros are replaced with real instructions. If you declared a macro and never
used it in your code, compiler will simply ignore it.
Macro definition:
<instructions>
ENDM
PROCEDURES MACROS
Accessed by CALL and RET instructions during Accessed during assembly when name given to
program execution. macro is written as an instruction in the
assembly program.
Machine code for instructions is put only once in the Machine code is generated for instructions each
memory. time a macro is called.
This as all machine code is defined only once so less This due to repeated generation of machine
memory is required. code requires more memory.
Parameters can be passed in register memory Parameters are passed as a part of the
location or stack. statement in which macro is called.
Programming Languages: To run a program, a microcomputer must have the program stored in binary form in
successive memory locations. There are three language levels that can be used to write a program for a
microcomputer.
1. MachineLanguage
2. AssemblyLanguage
3. High-levelLanguages
For all but the very simplest assembly language programs, you will probably want to use some type of
microcomputer development system and program development tools to make your work easier. Most of the
program development tools are programs which you run to perform some function on the program you are
writing.
Program development tools are:
1. Editor
2. Assembler
3. Linker
4. Locator
5. Debugger
6. Emulator
Editor: An editor is a program which allows you to create a file containing the assembly language statements for
your program. When you have typed in your entire program, you then save the file on a hard disk. This file is
called source file. The next step is to process the source file with an assembler. If you are going to use the TASM or
MASM assembler, you should give your source file name the extension .ASM.
Assembler: An assembler is programming tool which is used to translate the assembly language mnemonics for
instructions to the corresponding binary codes. The assembler generates two files. The first file, called the object
file, is given the extension .OBJ. The object file contains the binary codes for the instructions and information
about the addresses of the instructions. After further processing the contents of this file will be loaded into
memory and run. The second file generated by the assembler is called the assembler list file and is given the
extension .LST.
Linker: The linker is program used to join several object files into one large object file. The linkers which come
with the TASM or MASM assemblers produce link files with the .EXEextension.
Locator: A locator is a program used to assign the specific addresses of where the segments of object code are to
be loaded into memory.
Debugger: If your program requires no external hardware or requires only hardware accessible directly from your
microcomputer, then you can use debugger to run and debug your program. A debugger is a program which
allows you to load your object code program into system memory, execute the program, and troubleshoot or’
debug’ it.
Emulator: Another way to run your program is with an emulator. An emulator is a mixture of hardware and
software. It is usually used to test and debug the hardware and software of an external system.
ASSEMBLY LANGUAGEPROGRAMS
Simple programs
CODE SEGMENT
INC SI
DEC CL
UP: INC SI
INC SI
ADC AX, BX
DEC CL
JNZ UP
INT 03H
CODE ENDS
END
2. Write an ALP in 8086 to exchange a block of N bytes between source location and destination.
ASSUME CS:CODE
ORG 4000H
CODE SEGMENT
UP: INCSI
INC DI
LOOP UP
INT 03H
CODE ENDS
END
3. Write an ALP in 8086 to count no. of even and odd numbers from the given array.
ASSUME CS:CODE
ORG 2000H
CODE SEGMENT
UP: INCSI
JC ODD
INC BX
JMP DOWN
ODD: INC DX
DOWN: LOOP UP
INT 03H
CODE ENDS
END
4. Write an ALP in 8086 to check whether the given string is palindrome ornot.
CODE SEGMENT
MOV DI, SI
ADD DI, CX
MOV AL, CL
DIV BL
MOV CL, AL
INC SI
UP: CMPSB
JNE EXIT
INC SI
DEC SI
LOOPUP
INT 03H
INT 03H
CODENDS
END
ASCIITOHEX
DATA SEGMENT
A DB 41H
R DB ?
DATA ENDS
CODE SEGMENT
START: MOVAX,DATA
MOV DS,AX
MOV AL,A
SUB AL,30H
CMP AL,39H
JBE L1
SUB AL,7H
INT 3H
CODE ENDS
END START
END
HEX TO ASCII
DATA SEGMENT
A DB 08H
C DB ?
DATA ENDS
CODE SEGMENT
MOV DS,AX
MOV AL,A
ADD AL,30H
CMP AL,39H
JBE L1
ADD AL,7H
L1: MOVC,AL
INT 3H
CODE ENDS
END START
END
FACTORIAL
DATA SEGMENT
ORG 2000H
FIRST DW 3H
SEC DW 1H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
MOV DS,AX
MOV AX,SEC
MOV CX,FIRST
L1: MUL CX
DEC CX
JCXZ L2
JMP L1
L2: INT 3H
CODE ENDS
END START
END
FIBONOCCI
DATA SEGMENT
ORG 2000H
FIRST DW 0H
SEC DW 01H
THIRD DW 50H
RESULT DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA
START: MOVAX,DATA
MOV DS,AX
MOV AX,FIRST
MOV BX,SEC
MOV CX,THIRD
MOV [SI],AX
L1: INCSI
INC SI
MOV [SI],BX
ADD AX,BX
XCHG AX,BX
CMP BX,CX
INT3H
CODE ENDS
END START
END
9. Write an ALP in 8086 to find the greater number from the given numbers.
GREATER
DATA SEGMENT
ORG 2000H
FIRST DW 5H,2H,3H,1H,4H
DATA ENDS
CODE SEGMENT
MOV CX,COUNT
MOV AX,[SI]
L2: INC SI
INC SI
MOV BX,[SI]
CMP AX,BX
JGEL1
XCHG AX,BX
JMPL1
L1: DEC CX
JCXZ L4
JMP L2
L4: INT 3H
CODEENDS
ENDSTART
END
INTRODUCTION
What is a Microcontroller?
A Microcontroller is a programmable digital processor with necessary peripherals. Both microcontrollers
and microprocessors are complex sequential digital circuits meant to carry out job according to the
program / instructions. Sometimes analog input/output interface makes a part of microcontroller circuit of
mixed mode (both analog and digital nature). A microcontroller can be compared to a Swiss knife with
multiple functions incorporated in the same IC.
Microcontrollers Vs Microprocessors
1. A microprocessor requires an external memory for program/data storage. Instruction execution
requires movement of data from the external memory to the microprocessor or vice versa. Usually,
microprocessors have good computing power and they have higher clock speed to facilitate faster
computation.
2. A microcontroller has required on-chip memory with associated peripherals. A microcontroller can
be thought of a microprocessor with inbuilt peripherals.
3. A microcontroller does not require much additional interfacing ICs for operation and it functions as
a standalone system. The operation of a microcontroller is multipurpose, just like a Swiss knife.
4. Microcontrollers are also called embedded controllers. A microcontroller clock speed is limited only
to a few tens of MHz. Microcontrollers are numerous and many of them are application specific.
Development/Classification of microcontrollers
Microcontrollers have gone through a silent evolution (invisible). The evolution can be rightly termed as
silent as the impact or application of a microcontroller is not well known to a common user, although
microcontroller technology has undergone significant change since early 1970's. Development of some
popular microcontrollers is given as follows.
Intel 4004 4 bit (2300 PMOS trans, 108 kHz) 1971
Intel 8048 8 bit 1976
Intel 8031 8 bit (ROM-less) .
Intel 8051 8 bit (Mask ROM) 1980
Microchip PIC16C64 8 bit 1985
Motorola 68HC11 8 bit (on chip ADC) .
Intel 80C196 16 bit 1982
Atmel AT89C51 8 bit (Flash memory) .
Microchip PIC 16F877 8 bit (Flash memory + ADC) .
Development of microprocessors
Microprocessors have undergone significant evolution over the past four decades. This development is
clearly perceptible to a common user, especially, in terms of phenomenal growth in capabilities of personal
computers. Development of some of the microprocessors can be given as follows.
Intel 4004 4 bit (2300 PMOS transistors) 1971
Intel8080 8-bit(NMOS)
1974
8085 8 bit
Intel8088 16-bit
1978
8086 16 bit
Intel80186 16-bit
1982
80286 16 bit
Intel 80386 32 bit (275000 transistors) 1985
Intel80486SX 32-bit
1989
DX 32 bit (built in floating point unit)
Intel 80586 I
1993
MMX
1997
Celeron II 64 bit
1999
III
2000
IV
Z-80 (Zilog) 8 bit 1976
Motorola Power PC 601 32-bit 1993
602
603 1995
Microcontroller Chips
Broad Classification of different microcontroller chips could be as follows:
Microprocessor:
General-purpose microprocessor
• Designer can decide on the amount of ROM, RAM and I/O ports.
• expansive
• versatility
• general-purpose
Microcontroller :
• CPU, RAM, ROM, I/O and timer are all on a single chip
• single-purpose
• Cost is lower
Advantages of microcontroller:
• Low cost
• More reliable
• Useful for small dedicated applications and not for larger system designs which may require many
more I/O ports.
Harvard suggested a computer with two different memory interfaces, one for the data / variables and the
other for program / instructions. Although Princeton architecture was accepted for simplicity and ease of
implementation, Harvard architecture became popular later, due to the parallelism of instruction
execution.
Princeton Architecture (Single memory interface)
Cycle 1
- Complete previous instruction
- Read the "Move Data to Accumulator" instruction
Cycle 2
- Execute "Move Data to Accumulator" instruction
- Read next instruction
Hence each instruction is effectively executed in one instruction cycle, except for the ones that modify the
content of the program counter. For example, the "jump" (or call) instructions takes 2 cycles. Thus, due to
parallelism, Harvard architecture executes more instructions in a given time compared to Princeton
Architecture.
(bytes) (bytes)
8031 128 None 2 5 1
8032 256 none 2 6 1
8051 128 4k ROM 2 5 1
8052 256 8k ROM 3 6 1
8751 128 4k EPROM 2 5 1
8752 256 8k EPROM 3 6 1
AT89C51 128 4k Flash Memory 2 5 1
AT89C52 256 8k Flash memory 3 6 1
Introduction:
8051 employs Harvard architecture. It has some peripherals such as 32 bit digital I/O, Timers and Serial I/O.
The basic architecture of 8051 is given in fig below
Features:
Various features of 8051 microcontroller are given as follows.
8-bit CPU
16-bit Program Counter
8-bit Processor Status Word (PSW)
8-bit Stack Pointer
Internal RAM of 128bytes
On chip ROM is 4KB
Special Function Registers (SFRs) of 128 bytes
32 I/O pins arranged as four 8-bit ports (P0 - P3)
Two 16-bit timer/counters : T0 and T1
Two external and three internal vectored interrupts
One full duplex serial I/O
Basic Block Diagram:
Accumulator (Acc):
• Operand register
• Implicit or specified in the instruction
• Has an address in on chip SFR bank
B Register: Used to store one of the operands for multiplication and division, otherwise, scratch pad
considered as a SFR.
Program Status Word (PSW): Set of flags contains status information.
Stack Pointer (SP): 8 bit wide register. Incremented before data is stored on to the stack using PUSH or
CALL instructions. Stack defined anywhere on the 128 byte RAM
Data Pointer (DPTR): 16 bit register contains DPH and DPL Pointer to external RAM address. DPH and DPL
allotted separate addresses in SFR bank
Port 0 To 3 Latches & Drivers: Each I/O port allotted a latch and a driver Latches allotted address in SFR.
User can communicate via these ports P0, P1, P2, and P3.
Serial Data Buffer: Internally had TWO independent registers, TRANSMIT buffer (parallel in serial out –
PISO) and RECEIVE buffer (serial in parallel out –SIPO) identified by SBUF and allotted an address in SFR.
Timer Registers: for Timer0 (16 bit register – TL0 & TH0) and for Timer1 (16 bit register – TL1 & TH1) four
addresses allotted in SFR
Control Registers: Control registers are IP, IE, TMOD, TCON, SCON, and PCON. These registers contain
control and status information for interrupts, timers/counters and serial port. Allotted separate address in
SFR.
Timing and Control Unit: This unit derives necessary timing and control signals for internal circuit and
external system bus
Oscillator: generates basic timing clock signal using crystal oscillator.
Instruction Register: decodes the opcode and gives information to timing and control unit.
EPROM & program address Register: provide on chip EPROM and mechanism to address it. All versions
don’t have EPROM.
Ram & Ram Address Register: provide internal 128 bytes RAM and a mechanism to address internally
ALU: Performs 8 bit arithmetic and logical operations over the operands held by TEMP1 and TEMP 2.User
cannot access temporary registers.
SFR Register Bank: set of special function registers address range: 80 H to FF H. Interrupt, serial port and
timer units control and perform specific functions under the control of timing and control unit
REGISTER SET OF 8051
Accumulator
ACC is the Accumulator register. The mnemonics for accumulator-specific instructions, however,
refer to the accumulator simply as A.
B Register
The B register is used during multiply and divide operations. For other instructions it can be
treated as another scratch pad register.
Stack Pointer
The Stack Pointer register is 8 bits wide. It is incremented before data is stored during PUSH and
CALL executions. While the stack may reside anywhere in on-chip RAM, the Stack Pointer is initialized
to 07H after a reset. This causes the stack to begin at location 08H.
Data Pointer
The Data Pointer (DPTR) consists of a high byte (DPH) and a low byte (DPL). Its intended function is
to hold a 16-bit address. It may be manipulated as a 16-bit register or as two independent 8-bit
registers.
Ports 0 to 3
P0, P1, P2 and P3 are the SFR latches of Ports 0, 1, 2 and 3, respectively.
Serial Data Buffer
The Serial Data Buffer is actually two separate registers, a transmit buffer and a receive buffer
register. When data is moved to SBUF, it goes to the transmit buffer where it is held for serial
transmission. (Moving a byte to SBUF is what initiates the transmission.) When data is moved from
SBUF, it comes from the receive buffer.
Timer Registers
Register pairs (TH0, TL0), (TH1, TL1), and (TH2, TL2) are the 16-bit counting registers
for Timer/Counters 0, 1, and 2, respectively.
Capture Registers
The register pair (RCAP2H, RCAP2L) are the capture register for the Timer 2 ‘capture mode’. In this
mode, in response to a transition at the 80C52’s T2EX pin, TH2 and TL2 are copied into RCAP2H and
RCAP2L. Timer 2 also has a 16-bit auto-reload mode, and RCAP2H and RCAP2L hold the reload
value for this mode.
Control Registers: Special Function Registers IP, IE, TMOD, TCON, T2CON, SCON, and PCON
contain control and status bits for the interrupt system, the timer/counters, and the serial port.
The set of Special Function Registers (SFRs) contains important registers such as Accumulator, Register B,
I/O Port latch registers, Stack pointer, Data Pointer, Processor Status Word (PSW) and various control
registers. Some of these registers are bit addressable. Addresses from 80H to FFH of all Special Function
Registers
In 8051, one instruction cycle consists of twelve (12) clock cycles. Instruction cycle is sometimes called as
Machine cycle by some authors.
– 00-1F – 32 bytes 4 banks 00,01,10,11 each containing 8 registers of 8 bits each. Only one
accessible at a time with PSW bits.
– 20-2FH – 16bytes is bit addressable with addresses 0F to 7FH, 20.7 or 20.0, or 0-7
– 30-7F – 80 bytes of general purpose data memory. It is byte addressable, used for stack
• RAM memory space allocation in the 8051
TIMERS / COUNTERS
8051 has two 16-bit programmable UP timers/counters. They can be configured to operate either as timers
or as event counters. The names of the two counters are T0 and T1 respectively. The timer content is
available in four 8-bit special function registers, viz, TL0, TH0, TL1 and TH1 respectively.
In the "timer" function mode, the counter is incremented in every machine cycle. Thus, one can think of it
as counting machine cycles. Hence the clock rate is 1/12 th of the oscillator frequency.
In the "counter" function mode, the register is incremented in response to a 1 to 0 transition at its
corresponding external input pin (T0 or T1). It requires 2 machine cycles to detect a high to low transition.
Hence maximum count rate is 1/24 th of oscillator frequency.
The operation of the timers/counters is controlled by two special function registers, TMOD and TCON
respectively.
Out of these, and are external interrupts whereas Timer and Serial port interrupts are generated
internally. The external interrupts could be negative edge triggered or low level triggered. All these
interrupt, when activated, set the corresponding interrupt flags. Except for serial interrupt, the interrupt
flags are cleared when the processor branches to the Interrupt Service Routine (ISR). The external interrupt
flags are cleared on branching to Interrupt Service Routine (ISR), provided the interrupt is negative edge
triggered. For low level triggered external interrupt as well as for serial interrupt, the corresponding flags
have to be cleared by software by the programmer.
Address: A8H
EA Enable/Disable all
SERIAL INTERFACE
The serial port of 8051 is full duplex, i.e., it can transmit and receive simultaneously.
The register SBUF is used to hold the data. The special function register SBUF is physically two registers.
One is, write-only and is used to hold data to be transmitted out of the 8051 via TXD. The other is, read-
only and holds the received data from external sources via RXD. Both mutually exclusive registers have the
same address 099H.
Pins 1-8: Port 1 Each of these pins can be configured as an input or an output.
Pin 9: RS A logic one on this pin disables the microcontroller and clears the contents of most registers. In
other words, the positive voltage on this pin resets the microcontroller. By applying logic zero to this pin,
the program starts execution from the beginning.
Pins10-17: Port 3 Similar to port 1, each of these pins can serve as general input or output. Besides, all of
them have alternative functions:
Pin 10: RXD Serial asynchronous communication input or Serial synchronous communication output.
Pin 11: TXD Serial asynchronous communication output or Serial synchronous communication clock output.
Pin 18, 19: X2, X1 Internal oscillator input and output. A quartz crystal which specifies operating frequency
is usually connected to these pins. Instead of it, miniature ceramics resonators can also be used for
frequency stability. Later versions of microcontrollers operate at a frequency of 0 Hz up to over 50 Hz.
Pin 21-28: Port 2 If there is no intention to use external memory then these port pins are configured as
general inputs/outputs. In case external memory is used, the higher address byte, i.e. addresses A8-A15 will
appear on this port. Even though memory with capacity of 64Kb is not used, which means that not all eight
port bits are used for its addressing, the rest of them are not available as inputs/outputs.
Pin 29: PSEN If external ROM is used for storing program then a logic zero (0) appears on it every time the
microcontroller reads a byte from memory.
Pin 30: ALE Prior to reading from external memory, the microcontroller puts the lower address byte (A0-
A7) on P0 and activates the ALE output. After receiving signal from the ALE pin, the external register
(usually 74HCT373 or 74HCT375 add-on chip) memorizes the state of P0 and uses it as a memory chip
address. Immediately after that, the ALU pin is returned its previous logic state and P0 is now used as a
Data Bus. As seen, port data multiplexing is performed by means of only one additional (and cheap)
integrated circuit. In other words, this port is used for both data and address transmission.
Pin 31: EA By applying logic zero to this pin, P2 and P3 are used for data and address transmission with no
regard to whether there is internal memory or not. It means that even there is a program written to the
microcontroller, it will not be executed. Instead, the program written to external ROM will be executed. By
applying logic one to the EA pin, the microcontroller will use both memories, first internal then external (if
exists).
Pin 32-39: Port 0 Similar to P2, if external memory is not used, these pins can be used as general
inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE pin is driven high (1) or
as data output (Data Bus) when the ALE pin is driven low (0).
Bank Addressing or Register Addressing: This way of addressing accesses the bytes in the current register
bank. Data is available in the register specified in the instruction. The register bank is decided by 2 bits of
Processor Status Word (PSW).
For example-
ADD A, R0; Adds content of R0 to A and stores in A
Register Indirect Addressing: The address of data is available in the R0 or R1 registers as specified in the
instruction.
For example -
MOV A, @R0 moves content of address pointed by R0 to A
External Data Addressing: Pointer used for external data addressing can be either R0/R1 (256 byte access)
or DPTR (64kbyte access).
For example -
MOVX A, @R0; Moves content of 8-bit address pointed by R0 to A
MOVX A, @DPTR; Moves content of 16-bit address pointed by DPTR to A
External Code Addressing: Sometimes we may want to store non-volatile data into the ROM e.g. look-up
tables. Such data may require reading the code memory.
This may be done as follows -
MOVC A, @A+DPTR; Moves content of address pointed by A+DPTR to A
MOVC A, @A+PC; Moves content of address pointed by A+PC to A
8051 INSTRUCTIONS
8051 has about 111 instructions. These can be grouped into the following categories
1. Arithmetic Instructions
2. Logical Instructions
3. Data Transfer instructions
4. Boolean Variable Instructions
5. Program Branching Instructions
The following nomenclatures for register, data, address and variables are used while write instructions.
A: Accumulator
B: "B" register
C: Carry bit
Direct: 8-bit internal direct address for data. The data could be in lower 128bytes of RAM (00 - 7FH) or it
could be in the special function register (80 - FFH).
@Ri: 8-bit external or internal RAM address available in register R0 or R1. This is used for indirect
addressing mode.
Addr11: 11-bit destination address for short absolute jump. Used by instructions AJMP & ACALL. Jump
range is 2 kbyte (one page).
Rel: 2's complement 8-bit offset (one - byte) used for short jump (SJMP) and all conditional jumps.
Arithmetic Instructions:
Logical Instructions: