MP MC Unit 2 Material
MP MC Unit 2 Material
1
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
- DOCUMENTATION: you should document the program. E.g. each page of the document
contains page number and name of the program, heading block containing the abstract about
the program, comments should be added wherever necessary.
These types of instructions are used to transfer data from source operand to destination operand.
All the store, load, move, exchange, input and output operations belong to this category.
b) Arithmetic and Logical Instructions:
All the instructions performing arithmetic, logical, increment, decrement, compare and scan
instructions belong to this category.
c) Branch Instructions:
These instructions transfer control of execution to the specified address. All the call, jump,
interrupt and return instructions belong to this category.
d) Loop Instructions:
The LOOP, LOOPNZ and LOOPZ instructions belong to this category. These are useful to
implement different loop structures.
e) Machine control Instructions:
These instructions control the machine status. NOP, HLT, WAIT and LOCK instructions belongs to
this category.
2
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
These instructions involve the bitwise shifting or rotation in either direction with or without a
count in CX.
h) String Instructions:
These instructions involve string manipulation operations like load, scan, compare, store etc. These
instructions are only to be operated upon the string.
Ex: - PUSH AX
PUSH DS
PUSH [5000H]
3
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
4
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
XLAT
3
5
(AL) ← 5 (BX)
AL [BX+AL]
Base of table
Simple input and output port transfer Instructions:
IN:
Copy a byte or word from specified port to accumulator.
Ex: - IN AL,03H
IN AX, DX
OUT:
Copy a byte or word from accumulator specified port.
Ex: - OUT 03H, AL
OUT DX, AX
LEA:
Load effective address of operand in specified register.
[reg] offset portion of address in DS
Ex: - LEA reg, offset
LDS:
Load DS register and other specified register from memory.
[reg] [mem]
[DS] [mem + 2]
Ex: - LDS reg, mem
LES:
Load ES register and other specified register from memory.
[reg] [mem]
[ES] [mem + 2]
Ex: - LES reg, mem
5
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
LAHF:
Load (copy to) AH with the low byte the flag register.
[AH] [Flags low byte]
Ex: - LAHF
SAHF:
Store (copy) AH register to low byte of flag register.
[Flags low byte] ← [AH]
Ex: - SAHF
PUSHF:
Copy flag register to top of stack.
[SP] ← [SP] – 2
[[SP]] ← [Flags]
Ex: - PUSHF
POPF:
Copy word at top of stack to flag register.
[Flags] ← [[SP]]
[SP] ← [SP] + 2
Ex: - POPF
b) Arithmetic Instructions:
The 8086 provides many arithmetic operations: addition, subtraction, negation, multiplication and
comparing two values.
ADD:
The add instruction adds the contents of the source operand to the destination operand.
Ex: - ADD AX, 0100H
ADD AX, BX
ADD AX, [SI]
ADD AX, [5000H]
ADD [5000H], 0100H
ADD 0100H
6
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
SUB: Subtract
The subtract instruction subtracts the source operand from the destination operand and the result is left in
the destination operand.
Ex: - SUB AX, 0100H
SUB AX, BX
SUB AX, [5000H]
SUB [5000H], 0100H
SBB: Subtract with Borrow
The subtract with borrow instruction subtracts the source operand and the borrow flag (CF) which may
reflect the result of the previous calculations, from the destination operand
Ex: - SBB AX, 0100H
SBB AX, BX
SBB AX, [5000H]
SBB [5000H], 0100H
INC: Increment
This instruction increases the contents of the specified Register or memory location by 1. Immediate data
cannot be operand of this instruction.
Ex: - INC AX
INC [BX]
INC [5000H]
DEC: Decrement
The decrement instruction subtracts 1 from the contents of the specified register or memory location.
Ex: - DEC AX
DEC [5000H]
7
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
NEG: Negate
The negate instruction forms 2’s complement of the specified destination in the instruction. The
destination can be a register or a memory location. This instruction can be implemented by inverting each
bit and adding 1 to it.
Ex: - NEG AL
AL = 0011 0101 35H Replace number in AL with its 2’s complement
AL = 1100 1011 = CBH
CMP: Compare
This instruction compares the source operand, which may be a register or an immediate data or a memory
location, with a destination operand that may be a register or a memory location
Ex: - CMP BX, 0100H
CMP AX, 0100H
CMP [5000H], 0100H
CMP BX, [SI]
CMP BX, CX
MUL: Unsigned Multiplication Byte or Word
This instruction multiplies an unsigned byte or word by the contents of AL.
Ex: - MUL BH; (AX) ← (AL) x (BH)
MUL CX; (DX) (AX) ← (AX) x (CX)
MUL WORD PTR [SI]; (DX) (AX) ← (AX) x ([SI])
IMUL: Signed Multiplication
This instruction multiplies a signed byte in source operand by a signed byte in AL or a signed word in
source operand by a signed word in AX.
Ex: - IMUL BH
IMUL CX
IMUL [SI]
CBW: Convert Signed Byte to Word
This instruction copies the sign of a byte in AL to all the bits in AH. AH is then said to be sign extension
of AL.
Ex: - CBW
AX= 0000 0000 1001 1000 Convert signed byte in AL signed word in AX.
Result in AX = 1111 1111 1001 1000
CWD: Convert Signed Word to Double Word
This instruction copies the sign of a byte in AL to all the bits in AH. AH is then said to be sign extension
of AL.
8
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
Ex: - CWD
Convert signed word in AX to signed double word in DX: AX
DX= 1111 1111 1111 1111
Result in AX = 1111 0000 1100 0001
DIV: Unsigned division
This instruction is used to divide an unsigned word by a byte or to divide an unsigned double word by a
word.
Ex: - DIV CL ; Word in AX / byte in CL
; Quotient in AL, remainder in AH
DIV CX ; Double word in DX and AX / word in CX, and Quotient in AX, remainder in DX
AAA: ASCII Adjust After Addition
The AAA instruction is executed after an ADD instruction that adds two ASCII coded operand to give a
byte of result in AL. The AAA instruction converts the resulting contents of AL to a unpacked decimal
digits.
AAA operation:
1) In AL If rightmost nibble is >9 (ie) A to F or Auxiliary Flag=1
ADD 6 to rightmost nibble
2) Clear left nibble form AL.
3) In AH ADD 1
4) Set Carry and Auxiliary Carry
Ex: - ADD CL, DL ; [CL] = 34H = ASCII for 4
; [DL] = 38H = ASCII for 8
; Result [CL] = 6CH
AAA ; [AL] =02, unpacked BCD for 2
; [AH] =01, unpacked BCD for 1
AAS: ASCII Adjust AL after Subtraction
This instruction corrects the result in AL register after subtracting two unpacked ASCII operands. The
result is in unpacked decimal format. The procedure is similar to AAA instruction except for the
subtraction of 06 from AL.
AAS operation:
1) AAS checks the rightmost nibble in AL If rightmost nibble is >9 (ie) A to F Or
Auxiliary Flag=1, Then Subtract 6 from rightmost nibble
2) Clear left nibble in AL.
3) Subtracts 1 from AH
9
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
Logical Instructions:
AND: Logical AND
This instruction bit by bit ANDs the source operand that may be an immediate register or a memory
location to the destination operand that may a register or a memory location. The result is stored in the
destination operand.
Ex: - AND AX, 0008H
AND AX, BX
OR: Logical OR
This instruction bit by bit ORs the source operand that may be an immediate, register or a memory
location to the destination operand that may a register or a memory location. The result is stored in the
destination operand.
Ex: - OR AX, 0008H
OR AX, BX
NOT: Logical Invert
This instruction complements the contents of an operand register or a memory location, bit by bit.
Ex: - NOT AX
NOT [5000H]
XOR: Logical Exclusive OR
This instruction bit by bit XORs the source operand that may be an immediate, register or a memory
location to the destination operand that may a register or a memory location. The result is stored in the
destination operand.
Ex: - XOR AX, 0098H
XOR AX, BX
TEST: Logical Compare Instruction
The TEST instruction performs a bit by bit logical AND operation on the two operands. The result of this
AND ing operation is not available for further use, but flags are affected.
11
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
c) Branch Instructions:
Branch Instructions transfers the flow of execution of the program to a new address specified in the
instruction directly or indirectly. When this type of instruction is executed, the CS and IP registers get
loaded with new values of CS and IP corresponding to the location to be transferred.
The Branch Instructions are classified into two types
i. Unconditional Branch Instructions.
ii. Conditional Branch Instructions.
Unconditional Branch Instructions:
In Unconditional control transfer instructions, the execution control is transferred to the specified
location independent of any status or condition. The CS and IP are unconditionally modified to the new
CS and IP.
CALL: Unconditional Call
This instruction is used to call a Subroutine (Procedure) from a main program. Address of procedure may
be specified directly or indirectly.
There are two types of procedure depending upon whether it is available in the same segment or in another
segment.
i. Near CALL i.e., ±32K displacement.
ii. Far CALL i.e., anywhere outside the segment.
On execution this instruction stores the incremented IP & CS onto the stack and loads the CS & IP
registers with segment and offset addresses of the procedure to be called.
RET: Return from the Procedure.
At the end of the procedure, the RET instruction must be executed. When it is executed, the previously
stored content of IP and CS along with Flags are retrieved into the CS, IP and Flag registers from the stack
and execution of the main program continues further.
INT N: Interrupt Type N.
In the interrupt structure of 8086, 256 interrupts are defined corresponding to the types from 00H to FFH.
When INT N instruction is executed, the type byte N is multiplied by 4 and the contents of IP and CS of
the interrupt service routine will be taken from memory block in 0000 segment.
INTO: Interrupt on Overflow
This instruction is executed, when the overflow flag OF is set. This is equivalent to a Type 4 Interrupt
instruction.
12
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
d) LOOP Instructions:
LOOP Unconditionally
This instruction executes the part of the program from the Label or address specified in the instruction up
to the LOOP instruction CX number of times. At each iteration, CX is decremented automatically and
JUMP IF NOT ZERO structure.
Ex: - MOV CX, 0004H
MOV BX, 7526H
Label 1: MOV AX, CODE
OR BX, AX
LOOP Label 1
Conditional Branch Instructions:
When this instruction is executed, execution control is transferred to the address specified
relatively in the instruction, provided the condition implicit in the opcode is satisfied. Otherwise execution
continues sequentially.
JZ/JE Label
Transfer execution control to address ‘Label’, if ZF=1.
JNZ/JNE Label
Transfer execution control to address ‘Label’, if ZF=0
JS Label
Transfer execution control to address ‘Label’, if SF=1.
JNS Label
Transfer execution control to address ‘Label’, if SF=0.
JO Label
Transfer execution control to address ‘Label’, if OF=1.
JNO Label
Transfer execution control to address ‘Label’, if OF=0.
JNP Label
Transfer execution control to address ‘Label’, if PF=0.
13
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
JP Label
Transfer execution control to address ‘Label’, if PF=1.
JB Label
Transfer execution control to address ‘Label’, if CF=1.
JNB Label
Transfer execution control to address ‘Label’, if CF=0.
JCXZ Label
Transfer execution control to address ‘Label’, if CX=0
Conditional LOOP
LOOPZ / LOOPE Label
Loop through a sequence of instructions from label while ZF=1 and CX=0.
15
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
16
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
17
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
18
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
19
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
4. Register Indirect:
In this addressing mode, the address of the memory location which contains data or operand is
determined in an indirect way using offset registers. The offset address of data is in either BX or SI or DI
register. The default segment register is either DS or ES.
Ex: - MOV AX, BX
The data is present in a memory location in DS whose offset is in BX. The effective address is
10H DS BX
5. Indexed:
In this addressing mode offset of the operand is stored in one of the index register. DS and ES are
the default segments for index registers SI and DI respectively
Ex: - MOV AX, SI
6. Register Relative: In this addressing mode the data is available at an effective address formed by
adding an 8-bit or 16-bit displacement with the content of any one of the registers BX, BP, SI and DI in
the default either DS or ES segment.
7. Based Indexed:
In this addressing mode the effective address of the data is formed by adding the content of a base
register (any one of BX or BP) to the content of an index register (any one of SI or DI). The default
segment register may be ES or DS.
21
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
DIRECTIVES:
DB: Define Byte
The DB directive is used to reserve byte or bytes of memory locations in the available memory.
Example:
LIST DB 0lH, 02H, 03H, 04H
This statement directs the assembler to reserve four memory locations for a list named LIST and initialize
them with the above specified four values.
The EVEN directive updates the location counter to the next even address if the current location
counter contents are not even, and assigns the following routine or variable or constant to that address.
24
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
Ex: - EVEN
PROCEDURE ROOT
.
.
.
ROOT ENDP
EQU: Equate
The directive EQU is used to assign a label with a value or a symbol. The use of this directive is
just to reduce the recurrence of the numerical values or constants in a program code.
Ex: -
LABEL EQU 0500H
ADDITION EQU ADD
MODULEl ENDS
MODULE2 SEGMENT
MODULE2 ENDS
25
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
ORG: Origin
The ORG directive directs the assembler to start the memory allotment for the particular segment,
block or code from the declared address in the ORG statement while starting the assembly process for a
module, the assembler initializes a location counter to keep track of the allotted addresses for the module.
If the ORG statement is not written in the program, the location counter is initialized to 0000.
PROC: Procedure
The PROC directive marks the start of a named procedure in the statement. Also, the types NEAR
or FAR specify the type of the procedure, i.e whether it is to be called by the main program located within
64K of physical memory or not.
Ex: - RESULT PROC NEAR
26
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
OPERATORS:
OFFSET: Offset of a Label
When the assembler comes across the OFFSET operator along with a label, it first computes the
16-bit displacement (also called as offset interchangeably) of the particular label, and replaces the string
'OFFSET LABEL' by the computed displacement. This operator is used with arrays, strings, lables and
procedures to decide their offsets in their default segments.
Ex: - DATA SEGMENT
LIST DB 10H
DATA ENDS
CODE SEGMENT
MOV SI, OFFSET LIST
CODE ENDS
PTR: Pointer
The pointer operator is used to declare the type of a label, variable or memory operand. The
operator PTR is prefixed by either BYTE or WORD.
If the prefix is BYTE, then the particular label, variable or memory operand is treated as an 8-bit
quantity, while if WORD is the prefix, then it is treated as a 16- bit quantity.
Ex: - MOV AL, BYTE PTR [SI] ; Moves content of memory location addressed by SI (8-bit) to AL
INC BYTE PTR [BX] ; Increments byte contents of memory location addressed by BX
MOV BX, WORD PTR [2000H] ; Moves 16-bit content of memory location 2000H to BX, i.e.
[2000H] to BL [2001 H] to BH
INC WORD PTR [3000H] ; Increments word contents of memory location 3000H
Considering contents of 3000H (lower byte) and 3001
H(higher byte) as a 16-bit number
SEG: Segment of a Label
The SEG operator is used to decide the segment address of the label, variable, or procedure and
substitutes the segment base address in place of ‘SEG label’.
Ex: - MOV AX, SEG ARRAY ; This statement moves the segment address
MOV DS, AX ; of ARRAY in which it is appearing, to register AX and then to DS.
SHORT
The SHORT operator indicates to the assembler that only one byte is required to code the
displacement for a jump (i.e. displacement is within -128 to +127 bytes from the address of the byte next
to the jump opcode).
Ex: - JMP SHORT LABEL
27
Downloaded by Chathrapathi ([email protected])
lOMoARcPSD|35672726
1. Editor
- An editor is a program which allows you to create a file containing the assembly
language statements for your program.
Example: PC-Write, Wordstar.
- As you type in your program, the editor stores the ASCII codes for the letters and
numbers in successive RAM locations.
- When you have typed in your entire program, you then save the file on the hard disk.
This file is called source file and the extension is .asm.
2. Assembler
An assembler program is used to translate the assembly language mnemonics for
instructions to corresponding binary codes. When you run the assembler, it reads the
source file of your program from the disk where you have saved it after editing.
On the first pass through the source program, the assembler determines the displacement
of named data items, the offset of labels, etc. and puts this information in a symbol table.
On the second pass through the source program, the assembler produces the binary code
for each instruction and inserts the offsets, etc. that it calculated during the first pass.
- The assembler generates 2 files on the floppy disk or hard disk. The first file is called
object file (.obj).
- The second file generated by assembler is called the assembler list file and is given
extension (.lst).
3. Linker
- A linker is a program used to join several object files into one large object file.
- The linker produces a link file which contains the binary codes for all the combined
modules.
- The linker also produces a link map file which contains the address information about the
linked files (.exe).
4. Locator
A locator is a program used to assign the specific address of where the segments of object
code are to be loaded into memory.
A locator program called EXE2BIN comes with the IBM PC Disk Operating System
(DOS). EXE2BIN converts a` .exe file to a .bin file which has physical addresses.
5. Debugger
- 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.
- The debugger allows you to look at the contents of registers and memory locations after
your program runs.
- It allows you to change the contents of registers and memory locations and re-run the
program.
- Some debuggers allow you to stop execution after each instruction so that you can check
or alter after each register contents.
- A debugger also allows you to set a breakpoint at any point in your program. If you insert
a breakpoint at any point in your program, the debugger will run the program up to the
instruction where you put the breakpoint and then stop the execution.
6. Emulator
- An emulator is a mixture of hardware and software.
- It is used to test and debug the hardware and software of an external system, such as the
prototype of a microprocessor based instrument. Part of the hardware of an emulator is a
multi wire cable which connects the host system to the system being developed.