MPMC Unit-1-Part-B PDF
MPMC Unit-1-Part-B PDF
MPMC Unit-1-Part-B PDF
The directive EXTRN informs the assembler that the names, procedures and labels
declared after this directive have been already defined in some other AL modules.
While in other module, where names, procedures and labels actually appear, they
must be declared public using the PUBLIC directive.
Ex: MODULE1 SEGMENT PUBLIC
FACT FAR MODULE1 ENDS
MODULE2 SEGMENT EXTRN
FACT FAR MODULE2 END
13. GROUP: Group the related segments
The label is used to assign name to the current content of the location counter.
Ex: CONTINUE LABEL FAR
The label CONTINUE can be used for a FAR jump, if the program contains the above
statement.
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.
27. FAR PTR: This directive indicates the assembler that the label following FAR
PTR is not available within the same segment and the address of the label is of 32-
bits i.e 2-bytes of offset followed by 2-bytes of segment address.
Ex : JMP FAR PTR LABEL
28. NEAR PTR: This directive indicates that the label following NEAR PTR is in the
same segment and needs only 16-bit
i.e 2-byte offset to address it Ex :
JMP NEAR PTR LABEL CALL NEAR
PTR ROUTINE
5. Summarize the Data Copy/ Transfer Instructions used in 8086
PUSH: Push to Stack: This instruction pushes the contents of the specified
register/memory location on to the stack. The stack pointer is decremented by 2, after each
execution of the instruction. The actual current stack-top is always occupied by the
previously pushed data. Hence, the push operation decrements SP by two and this store
the two-byte contents of the operand onto the stack. The higher byte is pushed first and
then the lower byte. Thus out of the two decremental stack addresses the higher byte
occupies the higher address and the lower byte occupies the lower address.
Syntax: PUSH reg
[SP] ← [SP]-2
[[S]] ← [reg]
Ex:
1) PUSH AX
2) PUSH DS
3) PUSH [5000H]; content of location 5000H & 5001H in DS are pushed onto the stack.
POP: Pop from stack: This instruction when executed, loads the specified register / memory
location with the contents
of the memory location of which address is formed using the current stack segment and
stack pointer as usual. The
stack pointer is incremented by 2. The POP instruction serves exactly opposite to the PUSH
instruction.
Syntax:
i) POP mem
[SP] ← [SP] +2
[mem] ← [[SP]]
ii) POP reg
[SP] ← [SP] + 2
[reg] ← [[SP]]
Ex: 1. POP AX
2. POP DS
3. POP [5000H]
XCHG: Exchange: This instruction exchanges the contents of the specified source and
destination operands, which may
be registers or one of them may be a memory location. However, exchange of data contents
of two memory locations
is not permitted.
Syntax:
i) XCHG AX, reg 16
[AX] [reg 16]
Ex: XCHG AX, DX
ii) XCHG mem, reg
[mem] [reg]
Ex: XCHG [BX], DX
Register and memory can be both 8-bit and 16-bit and memory uses DS as segment
register.
iii) XCHG reg, reg
[reg] [ reg ]
Ex: XCHG AL, CL XCHG DX, BX
Other examples:
1. XCHG [5000H], AX; This instruction exchanges data between AX and a memory
location [5000H] in the data segment.
2. XCHG BX; This instruction exchanges data between AX and BX.
I/O Operations:
IN: Input the port: This instruction is used for reading an input port. The address of
the input port may be specified in the instruction directly or indirectly AL and AX are
the allowed destinations for 8 and 16-bit input operations. DX is the only register
(implicit), which is allowed to carry the port address.
Ex: 1. IN AL, DX
[AL] ← [PORT DX]
Input AL with the 8-bit contents of the port addressed by DX
2. IN AX, DX
[AX] ← [PORT DX]
3. IN AL, PORT [AL] ← [PORT]
4. IN AX, PORT [AX] ← [PORT]
5. IN AL, 0300H; This instruction reads data from an 8-bit
port whose address is 0300H and stores it in AL.
6. IN AX ; This instruction reads data from a 16-bit port whose
address is in DX (implicit) and stores it in AX.
OUT: Output to the Port: This instruction is used for writing to an output port.The
address of the output port may be specified in the instruction directly or implicitly in
DX. Contents of AX or AL are transferred to a directly or indirectly addressed port after
execution of this instruction. The data to an odd addressed port is transferred on D 8 –
D15 while that to an even addressed port is transferred on D0-D7.The registers AL and
AX are the allowed source operands for 8- bit and 16-bit operations respectively.
Ex: 1. OUTDX,AL
[PORT DX] ← [AL]
2. OUT DX,AX
[PORT DX] ← [AX]
3. OUT PORT,AL
[PORT] ← [AL]
4. OUT PORT,AX
[PORT] ← [AX]
Output the 8-bit or 16-bit contents of AL or AX into an I/O port addressed by the
contents of DX or local port.
5. OUT 0300H,AL; This sends data available in AL to a port whose address is
0300H
6. OUT AX; This sends data available in AX to a port whose address is specified
implicitly in DX.
6.
Procedures and Macros:
The four major ways of passing parameters to and from a procedure are:
1. In registers
2. In dedicated memory location accessed by name
3. With pointers passed in registers
4. With the stack
The type of procedure depends on where the procedure is stored in the memory.
If it is in the same code segment where the main program is stored the it is called
near procedure otherwise it is referred to as far procedure.
For near procedure CALL instruction pushes only the IP register contents on the
stack, since CS register contents remains unchanged for main program.
But for Far procedure CALL instruction pushes both IP and CS on the stack.
Syntax:
Procedure name PROC near
instruction 1
instruction 2 RET
Procedure name ENDP Example:
near procedure:
ADD2 PROC near
ADD AX,BX
RET
ADD2 ENDP
far procedure:
Procedures segment
Assume CS : Procedures
ADD2 PROC far
ADD AX,BX
Procedures ends
Recursive Procedure
A recursive procedure is procedure which calls itself.
ALP for Finding Factorial of number using procedures
CODE SEGMENT
ASSUME CS:CODE
START: MOV AX,7
CALL FACT
MOV AH,4CH
INT 21H
FACT PROC NEAR
MOV BX,AX
DEC BX
BACK: MUL BX
DEC BX
JNZ BACK
RET
ENDP
CODE ENDS
END START
Macros:
A macro is a group of repetitive instructions in a program which are codified only
once and can be used as many times as necessary.
A macro can be defined anywhere in program using the directives MACRO and ENDM
Each time we call the macro in a program, the assembler will insert the defined group
of instructions in place of the call.
The assembler generates machine codes for the group of instructions each time the
macro is called.
Using a macro avoids the overhead time involved in calling and returning from a
procedure.
Syntax of macro:
macroname MACRO
instruction1 instruction2
.
. ENDM
ENDM
Example:
READ MACRO
MOV AH,01H
INT 21H
ENDM
DISPLAY MACRO
MOV DL,AL
MOV AH,02H
INT 21H
ENDM
Code Segment
Assume CS:Code, DS: Data
Start: MOV AX,Data
MOV DS,AX
XOR BX,BX
XOR DX,DX
MOV CL,Count
MOV SI,OFFSET List
AGAIN: MOV AX,[SI]
SHL AX,01H
JC NEG
INC BX
JMP NEXT
NEG: INC DX
NEXT: ADD SI,02H
DEC CL
JNZ AGAIN
MOV AH,4CH
INT 21H
Code Ends
End Start
9. Write an ALP to find no of Even & Odd numbers from a given series of signed numbers
Data Segment
15. Write an ALP to arrange the given list of numbers in ascending order using bubble
sorting
Data Segment
List DB 23, 45, 56, 89, 78
Count EQU 04H
Data Ends
Code Segment
Assume CS:Code, DS:Data
Start: MOV AX,Data
MOV DS,AX
MOV DL,Count-1
AGAIN: MOV CL,DL
MOV SI,OFFSET List
BACK: MOV AL,[SI]
CMP AL,[SI+1]
JL GO
XCHG [SI+1],AL
XCHG [SI],AL
GO: INC SI
LOOP BACK
DEC DL
JNZ AGAIN
MOV AH,4CH
INT 21H
Code Ends
End Start
16. Write an ALP to find weather the given data is even or odd parity, if even parity put
DL=00H, else put DL=01H, Assuming the given data is multi byte
Data Segment
NUM DD 33 44 88 66H
BYTE_COUNT EQU 04H
Data Ends
Code Segment
Assume CS:Code,DS:Data
Start: MOV AX,Data
MOV DS,AX
MOV DH,BYTE_COUNT
XORAL,AL
MOV CL,00H
MOV SI,OFFSET NUM
NXT-BYTE: MOV AL,[SI]
JP EVEN-PARITY
INC CL
EVEN-PARITY: INC SI
MOV AL,00H
DEC DH
JNZ NXT-BYTE
MOV DL,00H
RCR CL,01H
JNC CLEAR
INC DL
CLEAR: MOV AH,4CH
INT 21H
Code Ends
End Start
17. Write an ALP to read a character from key board and convert it in to upper case
before display it, the program must be needed to terminate by pressing CNTRL+C key
Data Segment
M-1 DB ‘Enter the key:$’
M-2 DB ‘the upper case letter is:$’
Data Ends
Code Segment
Assume CS:Code, DS: Data
Start: MOV AX,Data
MOV DS,AX
MOV AX,09H
MOV DX,OFFSET M-1
INT 21H
MOV AH,01H
INT 21H
CMP AL,61H
JB NO
CMP AL,7AH
JA NO
AND AL,5FH ;or AND AL,DFH
NO: MOV DL,AL
MOV AH,09H
MOV DX,OFFSET M-2
INT 21H
MOV AH,02H
INT 21H
JMP Start
Code Ends
End Start
18. Write an ALP to perform addition, Subtraction, multiplication, division on given
operands, perform BCD operation for addition & subtraction.
Data Segment
OPR-1 Equ 98H
OPR-2 Equ 49H
SUM DW 01 DUP (00)
DIFF DW 01 DUP (00)
PROD DW 01 DUP (00)
DIV DW 01 DUP (00)
Data Ends
Code Segment
Assume CS: Code, DS: Data
Start: MOV AX,Data
MOV DS,AX
MOV BL,OPR-2
XORAL,AL
MOV AL,OPR-1
ADD AL,BL
DAA
MOV BYTE_PTR SUM,AL
JNC GO-1
INC [SUM+1]
GO-1: XORAL,AL
MOV AL,OPR-1
SUB AL,BL
DAS
MOV BYTE_PTR DIFF,AL
JNB GO-2
INC [DIFF+1]
GO-2: XORAL,AL
MOV AL,OPR-1
MUL BL
MOV WORD_PTR PROD,AX
XORAX,AX
MOV AL,OPR-1
DIV BL
MOV WORD_PTR DIV,AX
MOV AH,4CH
INT 21H
Code Segment
End Start
19. Write an ALP to compare the first 10 bytes beginning at STRG-1 with the first 10
bytes beginning at STRG-2 and branches to MATCH if they are equal, otherwise
continue in sequence.
Data Segment
MSG-1 DB 10,13 ‘Enter String-1:$’
MSG-2 DB 10,13 ‘Enter String-2:$’
MSG-3 DB 10,13 ‘STRG-1 and STRG-2 are not matching $’
MSG-4 DB 10,13 ‘STRG-1 and STRG-2 are matching $’
STRG-1 DB ‘PSCMR’
STRG-2 DB ‘PSRCMR’
Data Ends
Code Segment
Assume CS:Code, DS:Data
Start: MOV AX,Data
MOV DS,AX
MOV AH,09H
MOV DX,OFFSET MSG-1
INT 21H
MOV SI,OFFSET STRG-1
INT 21H
MOV BX,OFFSET MSG-2
INT 21H
MOV DI,OFFSET STRG-2
INT 21H
MOV CL.0AH
REP: CMP SB
JCXZ MATCH
MOV BX,00H
MOV BX,OFFSET MSG-3
MATCH: MOV BX,00H
MOV BX,OFFSET MSG-4
INT 21H
CODE ENDS
END START
20. Write an ALP to add two multi byte numbers and store the result in third number.
The stored number in the form of byte list stored with the lowest byte first.
Data Segment
Num-1 DB 05H,12H, 45H, 56H, 49H, 78H, 42H, 79H, 41H, 71H
Num-2 DB 45H, 12H, 49H, 56H, 78H, 42H, 79H, 71H, 41H, 05H
Num-3 DB 0AH DUP (00)
BYTES EQU 0AH
Data Ends
Code Segment
Assume CS: Code, DS:Data
Start: MOV AX,Data
MOV DS,AX
MOV CL,BYTES
MOV SI,OFFSET Num-1
MOV DI,OFFSET Num-2
MOV BX,OFFSET Num-3
XOR AX,AX
NXT BYTE: MOV AX,[SI]
ADC AX,[DI]
MOV BYTE_PTR [BX],AX
INC SI
INC DI
DEC CX
JNZ NXT BYTE
JNC NO CARRY
MOV BYTE_PTR [BX],01
NO CARRY: MOV AH,4CH
INT 21H
Code Ends
End Start
21. It is necessary to define a block of data in 8086 assembly language program. The
length of the block is 90,000 bytes. Give the initialization of data segment for the
above block of data. It is necessary to add second element and 68,000th element of
the above data. Give the sequence of instructions to perform the above operation.
Sol: In 8086 the length of each segment is limited to 64KB (65536 bytes). Hence we
cannot load define a continous block of 90,000 bytes. For this purpose we need to
define two segments. One segment holding the first 62536 bytes of data while the other
holding remaining 24464 bytes of data.
Data Segment
Block-1 DB FFFFH DUP (0)
Data Ends
Extra Segment
Block-2 DB 24464H DUP (0)
Extra Ends
Code Segment
Assume CS:Code, DS:Data
Start: MOV AX,Data
MOV DS,AX
MOV AX,Extra
MOV ES,AX
LEA SI,Block-1
LEA DI,Block-2
MOV CX,FFFFH
INITI-1: MOV [SI],CX
INC SI
LOOP INITI-1
MOV CX,5F90H
INITI-2: MOV [DI],CX
INC DI
LOOP INITI-2
LEA SI,Block-1
LEA DI,Block-2
MOV AL,[SI+1]
ADD AL,[DI+2464]
Code Ends
End Start