Mic 314321 Notes
Mic 314321 Notes
PROGRAMMING
(314321)
(Semester IV- Computer Engineering)
By,
Prof. Nagesh A. Goden
(Lecturer, Computer Engineering Department)
Microprocessor Programming (314321)
INDEX
[Marks-14]
Ans:
1) It is a 16 bit μp.
2) 8086 has a 20 bit address bus can access up to 220 memory locations (1MB).
3) It can support up to 64K I/O ports.
4) It provides 16-bit registers. AX,BX,CX,DX,CS,SS,DS,ES,BP,SP,SI,DI,IP & FLAG
REGISTER.
5) It has multiplexed address and data bus AD0-AD15 and A16 – A19.
6) 8086 is designed to operate in two modes, Minimum and Maximum.
7) It can prefetches up to 6 instruction bytes from memory and queues them in order to speed up
instruction execution.
8) Interrupts:-8086 has 256 vectored interrupts.
9) Provides separate instructions for string manipulation.
10) Operating clock frequencies 5MHz, 8MHz, 10MHz.
Ans: ALE :This active high ,output signal used to indicate availability of valid address on address/data
lines and is connected to latch enable input of latches (8282 or 74LS373) .
READY: This input is used to insert wait state into the timing cycle of the 8086. If the ready pin is at
logic 1, it has no effect on the operation of the microprocessor. If it is logic 0, the 8086 enters the waits
state and remains the idle. This pin is used to interface the operating peripherals with the 8086.
Ans:BHE: BHE stands for Bus High Enable. It is available at pin 34 and used toindicate the transfer of
data using data bus D8-D15. This signal is low duringthe first clock cycle, thereafter it is active.
A0: A0 is analogous to BHE for the lower byte of the data bus, pinsD0-D7. A0 bitis Low during T1 state
when a byte is to be transferred on the lower portion ofthe bus in memory or I/O operations.
Ans: Ready:It is used as acknowledgement from slower I/O device or memory.It is Active high signal,
when high; it indicates that the peripheral device isready to transfer data.
M/IO: This signal is used to differentiate between I/O & memory operations. When it is high, it indicates
memory operation and when low, it indicates I/O operation.
INTR: This is a level triggered interrupt request input, checked during last clockcycle of each instruction
to determine the availability of request. If anyinterrupt request is occurred, the processor enters the
interrupt acknowledgecycle.
6. Name the general purpose register of 8086, give brief description of each.
Ans: 1. AX (Accumulator) – Used to store the result for arithmetic / logical operations All I/O data
transfer using IN & OUT instructions use “A” register(AH / AL or AX).
2. BX – Base – used to hold the offset address or data in indirect addressing mode.
3. CX – acts as a counter for repeating or looping instructions.
4. DX – Used with AX to hold 32 bit values during multiplication and division. Used to hold
address of I/O port in indirect addressing mode.
Ans: CS – Code Segment – holds base address for all executable instructions in a program.
SS -Stack segment- holds the Base address of the stack.
DS – Data Segment – default base address for variables.
ES – Extra Segment – additional base address for memory variables in extra segment.
BIU (Bus Interface Unit) :BIU takes care of all data and addresses transfers on the buses for the EU like
sending addresses, fetching instructions from the memory, reading data from the ports and the memory as
well as writing data to the ports and the memory. EU has no direction connection with System Buses so
this is possible with the BIU. EU and BIU are connected with the Internal Bus.
It has the following functional parts −Instruction queue , Segment register , Instruction pointer
EU (Execution Unit) :Execution unit gives instructions to BIU stating from where to fetch the data and
then decode and execute those instructions. Its function is to control operations on data using the
instruction decoder & ALU. EU has no direct connection with system buses as shown in the above figure,
it performs operations over data through BIU.
It has the following functional parts – ALU, Flag Register, General Purpose Register
C-Carry Flag : It is set when carry/borrow is generated out of MSB of result. (i.e D7 bit for 8-bit
operation, D15 bit for a 16 bit operation).
P-Parity Flag This flag is set to 1 if the lower byte of the result contains even number of 1‟s otherwise it
is reset.
AC-Auxiliary Carry Flag This is set if a carry is generated out of the lower nibble, (i.e. From D3 to D4
bit)to the higher nibble
Z-Zero Flag This flag is set if the result is zero after performing ALU operations. Otherwise it is reset.
S-Sign Flag This flag is set if the MSB of the result is equal to 1 after performing ALU operation ,
otherwise it is reset.
O-Overflow Flag This flag is set if an overflow occurs, i.e. if the result of a signed operation is large
enough to be accommodated in destination register.
Control Flags:
T-Trap Flag If this flag is set ,the processor enters the single step execution mode.
I-Interrupt Flag it is used to mask(disable) or unmask(enable)the INTR interrupt. When this flag is
set,8086 recognizes interrupt INTR. When it is reset INTR is masked.
D-Direction Flag It selects either increment or decrement mode for DI &/or SI register during string
instructions.
This takes longer time when compared to pipelined processor. In this ,the fetch, decode and execute
operations are performed in parallel, so only five clock cycles are required to execute the same three
instructions as shown Fig(b).
In 8086, pipelining is implemented by providing 6 byte queue where as long as 6 one byte instructions can
be stored well in advance and then one by one instruction goes for decoding and executions. So, while
executing first instruction in a queue, processor decodes second instruction and fetches 3rd instruction
from the memory In this way, 8086 perform fetch, decode and execute operation in parallel i.e. in single
clock cycle as shown in above fig (b)
Advantages of pipelining:
The execution unit always reads the next instruction byte from the queue in BIU. This is faster
than sending out an address to the memory and waiting for the next instruction byte to come.
More efficient use of processor.
Quicker time of execution of large number of instruction.
In short pipelining eliminates the waiting time of EU and speeds up the processing. -The 8086 BIU
will not initiate a fetch unless and until there are two empty bytes in its queue. 8086 BIU normally
obtains two instruction bytes per fetch.
A logical address is the address at which an item (memory cell, storageelement) appears to reside from
the perspective of an executing applicationprogram. A logical address may be different from the physical
address dueto the operation of an address translator or mapping function.
Effective Address or Offset Address: The offset for a memory operand iscalled the operand's effective
address or EA. It is an unassigned 16 bitnumber that expresses the operand's distance in bytes from the
beginning ofthe segment in which it resides. In 8086 we have base registers and indexregisters.
Calculate the physical address for the given DS=345AH and SI=13DCH
Physical Address = DS(0000) + SI
= 345AH (0000) + 13DCH
= 345A0+13DC
= 3597CH
Advantages of Segmentation:
The size of address bus of 8086 is 20 and is able to address 1 Mbytes( ) of physical memory.
The compete 1 Mbytes memory can be divided into 16 segments,each of 64 Kbytes size.
It allows memory addressing capability to be 1 MB.
It gives separate space for Data, Code, Stack and Additional Datasegment as Extra segment size.
The addresses of the segment may be assigned as 0000H to F000Hrespectively.
The offset values are from 00000H to FFFFFH
Segmentation is used to increase the execution speed of computersystem so that processor can able
to fetch and execute the data frommemory easily and fast.
General Data Registers: The registers AX, BX, CX and DX are the general purpose 16-bit registers.
AX is used as 16-bit accumulator. The lower 8-bit is designated as AL and higher 8-bitis designated as AH.
AL can be used as an 8-bit accumulator for 8-bit operation.All data register can be used as either 16 bit or
8 bit.
BX is a 16 bit register, but BLindicates the lower 8-bit of BX and BH indicates the higher 8-bit of BX.
The register CX is used default counter in case of string and loop instructions.The register BX is used as
offset storage for forming physical address in case ofcertain addressing modes.
DX register is a general purpose register which may be used as an implicit operand ordestination in case of
a few instructions.
Segment Registers:
The 8086 architecture uses the concept of segmented memory. 8086 able to address toaddress a memory
capacity of 1 megabyte and it is byte organized. This 1 megabytememory is divided into 16 logical
segments. Each segment contains 64 kbytes ofmemory.
The index registers are used as general purpose registers as well as for offset storagein case of indexed,
base indexed and relative base indexed addressing modes.
The register SI is used to store the offset of source data in data segment.
The register DI is used to store the offset of destination in data or extra segment.
The index registers are particularly useful for string manipulation.
Flag Register:
Ans: Intel 8086 is a 16-bit HMOS microprocessor. It is available in 40 pin DIP chip. It uses a 5V DC
supply for its operation. The 8086 uses 20-line address bus. It has a 16-line data bus. The 20 lines of the
address bus operate in multiplexed mode. The 16-low order address bus lines have been multiplexed with
data and 4 high-order address bus lines have been multiplexed with status signals.
BHE’/S7 : Bus High Enable/Status. During T1 it is low. It is used to enable data onto the most significant
half of data bus, D8-D15. 8-bit device connected to upper half of the data bus use BHE (Active Low)
signal. It is multiplexed with status signal S7. S7 signal is available during T2, T3 and T4.
RD: This is used for read operation. It is an output signal. It is active when low.
READY : This is the acknowledgement from the memory or slow device that they have completed the
data transfer. The signal made available by the devices is synchronized by the 8284A clock generator to
provide ready input to the microprocessor. The signal is active high(1).
INTR : Interrupt Request. This is triggered input. This is sampled during the last clock cycles of each
instruction for determining the availability of the request. If any interrupt request is found pending, the
NMI : Non maskable interrupt. This is an edge triggered input which results in a type II interrupt. A
subroutine is then vectored through an interrupt vector lookup table which is located in the system
memory. NMI is non-maskable internally by software. A transition made from low(0) to high(1) initiates
the interrupt at the end of the current instruction. This input has been synchronized internally.
INTA : Interrupt acknowledge. It is active low(0) during T2, T3 and Tw of each interrupt acknowledge
cycle.
MN/MX’ : Minimum/Maximum. This pin signal indicates what mode the processor will operate in.
RQ’/GT1′, RQ’/GT0′ : Request/Grant. These pins are used by local bus masters used to forc the
microprocessor to release the local bus at the end of the microprocessor‟s current bus cycle. Each of the
pin is bi-directional. RQ‟/GT0′ have higher priority than RQ‟/GT1′.
LOCK’ : Its an active low pin. It indicates that other system bus masters have not been allowed to gain
control of the system bus while LOCK‟ is active low(0). The LOCK signal will be active until the
completion of the next instruction.
TEST’ : This examined by a „WAIT‟ instruction. If the TEST pin goes low(0), execution will continue,
else the processor remains in an idle state. The input is internally synchronized during each of the clock
cycle on leading edge of the clock.
CLK : Clock Input. The clock input provides the basic timing for processing operation and bus control
activity. Its an asymmetric square wave with a 33% duty cycle.
RESET : This pin requires the microprocessor to terminate its present activity immediately. The signal
must be active high(1) for at least four clock cycles.
Vcc : Power Supply( +5V D.C.)
GND : Ground
QS1,QS0 : Queue Status. These signals indicate the status of the internal 8086 instruction queue
according to the table shown below
DT/R : Data Transmit/Receive. This pin is required in minimum systems, that want to use an 8286 or
8287 data bus transceiver. The direction of data flow is controlled through the transceiver.
DEN : Data enable. This pin is provided as an output enable for the 8286/8287 in a minimum system
which uses transceiver. DEN is active low(0) during each memory and input-output access and for INTA
cycles.
HOLD/HOLDA : HOLD indicates that another master has been requesting a local bus .This is an active
high(1). The microprocessor receiving the HOLD request will issue HLDA (high) as an acknowledgement
in the middle of a T4 or T1 clock cycle.
ALE : Address Latch Enable. ALE is provided by the microprocessor to latch the address into the 8282 or
8283 address latch. It is an active high(1) pulse during T1 of any bus cycle. ALE signal is never floated, is
always integer.
[Marks-08]
Linker: It is a programming tool used to convert Object code into executable program called .EXE
module. It combines, if requested, more than one separated assembled modules into oneexecutable
module such as two or more assembly programs or an assembly languagewith C program.
6. Draw the symbols used in a flowchart while developing ALP, Mention the use of each
symbol.
Ans:
DW (Define Word) :
This is used to define a word (16-bit) type variable.
The range of values : 0 – 65535 for unsigned numbers -32768 to 32767 for signed numbers
This can be used to define a single word or multiple words.
ASSUME:-
Assume directive is used to tell Assembler the name of the logical segment it should use for the
specified segment.When program is loaded the processor segment register should point to the
respective logical segments.
Example: - Assume CS: MSBTE_CODE, DS: MSBTE_DATA
SEGMENT:
Used to indicate the beginning of logical segment . Preceding the SEGMENT directive is the name
you want to give the segment.
Syntax: Segment_Name SEGMENT [Word/Public]
Linker: It is a programming tool used to convert Object code into executable program called .EXE
module. It combines, if requested, more than one separated assembled modules into oneexecutable
module such as two or more assembly programs or an assembly languagewith C program.
Debugger: Debugger is the program that allows the extension of program in single step mode
under the control of the user. The process of locating & correcting errors using a debugger is
known as Debugger.
4. Initialization checklist: Initialization task is to make the checklist of entire variables, constants,
all the registers, flags and programmable ports
5. Choosing instructions: Choose those instructions that make program smaller in size and more
importantly efficient in execution.
[Marks-16]
10. What is role of XCHG instruction in assembly language program? Give example
Ans: Role of XCHG: This instruction exchanges the contents of a register with the contents of another
register or memory location.
Example:
XCHG AX, BX ; Exchange the word in AX with word in BX.
13. List any four instructions from the bit manipulation instructions of 8086.
Ans: Bit Manipulation Instructions: These instructions are used to perform operations where data bits
are involved i.e. operations like logical, shift, etc.
Following is the list of instructions under this group −
1. Write classification of instruction set of 8086. Explain any one type out of them.
Ans:
Classification of instruction set of 8086
Data Transfer Instructions
Arithmetic Instructions
Bit Manipulation Instructions
String Instructions
Program Execution Transfer Instructions (Branch & Loop Instructions)
Processor Control Instructions
Iteration Control Instructions
Interrupt Instructions
1) Arithmetic Instructions:
These instructions are used to perform arithmetic operations like addition, subtraction,
multiplication, division, etc.
ADD:
The add instruction adds the contents of the source operand to the destination operand.
SUB: Subtract
The subtract instruction subtracts the source operand from the destination operand and the result is
left in the destination operand.
Eg. SUB AX, 0100H
SUB AX, BX
SUB AX, [5000H]
SUB [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.
Eg. INC AX
INC [BX]
INC [5000H]
DEC: Decrement
The decrement instruction subtracts 1 from the contents of the specified register or memory
location.
Eg. DEC AX
DEC [5000H]
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.
Eg. 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
Eg. CMP BX, 0100H
CMP AX, 0100H
CMP [5000H], 0100H
CMP BX, [SI]
CMP BX, CX
These instructions are used to control the processor action by setting/resetting the flag values.
STC:
It sets the carry flag to 1.
CLC:
It clears the carry flag to 0.
CMC:
It complements the carry flag.
STD:
It sets the direction flag to 1. If it is set, string bytes are accessed from higher memory address to
lower memory address.
CLD:
It clears the direction flag to 0. If it is reset, the string bytes are accessed from lower memory
address to higher memory address.
5)TEST
Syntax : TEST Destination, Source
This instruction AND‟s the contents of a source byte or word with thecontents of specified
destination byte or word and flags are updated, flags are updated as result ,but neither operands are
changed.
Operation performed:
Ans: 1) REP:
REP is a prefix which is written before one of the string instructions. It will cause During length
counter CX to be decremented and the string instruction to be repeated until CX becomes 0.
Two more prefix.
REPE/REPZ: Repeat if Equal /Repeat if Zero.
It will cause string instructions to be repeated as long as the compared bytesor words Are equal
and CX≠0.
REPNE/REPNZ: Repeat if not equal/Repeat if not zero.
It repeats the strings instructions as long as compared bytes or words are notequalAnd CX≠0.
Example: REP MOVSB
5] LODS/LODSB/LODSW:
Load String byte into AL or Load String word into AX.
Syntax:
LODS/LODSB/LODSW
A.G.Patil Polytechnic Institute, Solapur Page 29
Microprocessor Programming (314321)
Operation: AL/AX < ----- DS: [SI]
IT copies a byte or word from string pointed by SI in data segment into ALor AX.CXmay contain
the counter and DF may be either 0 or 1
Example
LEA SI, destination
CLD
LODSB
Or
MOV CL, 04H
ROR AX, CL
iii) IDIV BL
v) INC AX
6. Explain four rotate instructions with their syntax, operation and example.
Ans:
1.ROL – Rotate bits of byte or word left, MSB to LSB and to CF
Syntax: ROL destination, count
Eg:
ROL BL, 2 ; Rotate all bits in BL left by 1 bit ,copy MSB to LSB and to CF
IF BL = 11110000
After Execution 11000011, CF= 1
2. ROR – Rotate bits of byte or word right, LSB to MSB and to CF
Syntax: ROR destination, count
Eg:
ROR BL, 2 ; Rotate all bits in BL right by 1 bit ,copy LSB to MSB and to CF
IF BL = 11110000
After Execution 00111100, CF= 0
3.RCL – Rotate bits of byte or word left, MSB to CF and CF to LSB.
Syntax: RCL destination, count
Eg:
RCL BL, 2 ; Rotate all bits in BL left by 1 bit ,copy MSB to CF and CF to LSB
IF BL = 11110000, CF=0
After Execution 11000001 , CF= 1
[Marks-20]
Ans:
18. Write ALP for addition of two 8bit numbers. Assume suitable data.
Ans:
DATA SEGEMT
NUM 1 DB 10H
NUM2 DB 20H
RESULT DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:MOV AX, DATA
MOV DS,AX
MOV AL, NUM1
MOV BL, NUM2
ADD AL,BL
MOV RESULT, AL
INT 21H
CODE ENDS
END START
12. Write an ALP to find largest number in array of elements 10H, 24H, 02H, 05H, 17H.
Ans: DATA SEGMENT
ARRAY DB 10H,24H,02H,05H,17H
LARGEST DB 00H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV DX,DATA
MOV DS,DX
MOV CX,04H
MOV SI ,OFFSET
ARRAY MOV AL,[SI]
UP: INC SI
CMP AL,[SI]
JNC NEXT
MOV AL,[SI]
NEXT: DEC CX
JNZ UP
MOV LARGEST,AL
MOV AX,4C00H
INT 21H
CODE ENDS
END START
13. Write an ALP to reverse a string. Also draw flowchart for same.
Ans: Program:
DATA SEGMENT
STRB DB 'GOOD MORNING$'
REV DB 0FH DUP(?)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV DX,DATA
MOV DS,DX
LEA SI,STRB
MOV CL,0FH
LEA DI,REV
ADD DI,0FH
UP:MOV AL,[SI]
MOV [DI],AL
INC SI
DEC DI
LOOP UP
INT 21H
CODE ENDS
END START
Flowchart:
14. Write an assembly language program to find largest number from array of 10 numbers.
Ans:
DATA SEGMENT
ARRAY DB 15H,45H,08H,78H,56H,02H,04H,12H,23H,09H
LARGEST DB 00H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV DX,DATA
MOV DS,DX
MOV CX,09H
MOV SI ,OFFSET ARRAY
MOV AL, [SI]
UP:INC SI
CMP AL,[SI]
JNC NEXT
MOV AL,[SI]
NEXT: DEC CX
JNZ UP
MOV LARGEST,AL ; AL=78h
MOV AX,4C00H
INT 21H
CODE ENDS
END START
15. Write an assembly language program to perform addition of two 16-bit numbers.
Ans:
DATA SEGMENT
N1 DW 2804H
N2 DW 4213H
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
MOV AX, N1
MOV BX, N2
ADD AL,BL
MOV CL,AL
MOV AL,AH
ADD AL,BH
MOV CH,AL
MOV AH,4CH
INT 21H
CODE ENDS
END START
16. Write an assembly language program to sort an array of 10 numbers in ascending order.
Ans:
DATA SEGMENT
ARRAY DB 15h,05h,08h,78h,56h, 60h, 54h, 35h, 24h, 67h
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA
START:MOV DX, DATA
MOV DS, DX
MOV BL,0AH
step1: MOV SI,OFFSET ARRAY
MOV CL,09H
step: MOV AL,[SI]
CMP AL,[SI+1]
JC Down
XCHG AL,[SI+1]
XCHG AL,[SI]
Down : ADD SI,1
LOOP step
DEC BL
JNZ step1
CODE ENDS
END START
17. Write an assembly language program to multiply two 16-bit unsigned numbers.
Ans:
DATA SEGMENT
N1 DW 2401H
N2 DW 1324H
C DD ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AX,N1
MOV BX,N2
MUL BX
MOV WORD PTR C,AX
MOV WORD PTR C+2,DX
INT 21H
CODE ENDS
END START
19. Write an assembly language program to multiply two 8-bit unsigned numbers.
Ans:
DATA SEGMENT
NUM1 DB 05H
NUM2 DB 02H
RESULT DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV DX,DATA
MOV DS,DX
MOV AL,NUM1
MOV AH,NUM2
MUL NUM2
MOV RESULT,AX
MOV AX,4C00H
INT 21H
CODE ENDS
END START
20. Write an assembly language program to add two 8-bit BCD numbers.
Ans:
DATA SEGMENT
OP1 EQU 92H
OP2 EQU 52H
RESULT DB 02 DUP(00)
DATA ENDS
ASSUME CS: CODE , DS:DATA
CODE SEGMENT
START: MOV AX,DATA
MOV DS,AX
MOV BL,OP1
XOR AL,AL
MOV AL,OP2
ADD AL,BL
DAA
MOV RESULT ,AL
JNC MSBO
INC [RESULT+1]
MSBO: MOV AH,4CH
INT 21H
CODE ENDS
END START
21. Write an assembly language program to find reverse order of a given string. Also, write algorithm
and draw flowchart.
Ans:
Algorithm:
1. Initialize the Data segment and Code Segment registers.
2. SI=start of string to be reversed.
3. Assign CX=string length.
4. DI=reverse string address pointer.
5. Assign DI=string length.
6. Read the first character pointed by SI.
7. Store at the last character position pointed by DI.
8. Decrement DI to point to the next character, if DI !=0, go to step 6.
9. Stop.
Flowchart:
Program:
DATA SEGMENT
STRB DB 'GOOD MORNING$'
REV DB 0FH DUP(?)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV DX,DATA
MOV DS,DX
LEA SI,STRB
MOV CL,0FH
LEA DI,REV
ADD DI,0FH
UP:MOV AL,[SI]
MOV [DI],AL
INC SI
DEC DI
LOOP UP
MOV AH,4CH
INT 21H
CODE ENDS
END START
24. Write ALP and draw flow chart to perform Block Transfer without using String Instruction
Ans:
25. Write an ALP to count the number of positive and negative numbers in array.
Ans:
;Count Positive No. And Negative No.S In Given ;Array Of 16 Bit No.
;Assume array of 6 no.s
DATA SEGMENT
ARRAY DW F423H,6523H,B658H,7612H, 2300H,1559H
COUNT DW 06H
POS_COUNT DB ?
NEG_COUNT DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV DX,0000H
MOV CX,COUNT
MOV SI, OFFSET ARRAY
NEXT: MOV AX,[SI]
ROR AX,01H
JC NEGATIVE
INC DL
JMP COUNT_IT
NEGATIVE: INC DH
COUNT_IT: INC SI
INC SI
LOOP NEXT
MOV NEG_COUNT,DL
MOV POS_COUNT,DH
INT 21H
CODE ENDS
END START
DATA SEGMENT
ARRAY1 DW F423H, 6523H, B658H, 7612H, 9875H,
2300H, 1559H, 1000H, 4357H, 2981H
COUNT DW 0AH
ODD_COUNT DB ?
EVENCNT DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV DX,0000H
DATA SEGMENT
BLOCK1 DW 1001H,4003H,6005H,2307H,4569H, 6123H,
1865H, 2345H,4000H,8888H
DATA ENDS
EXTRA SEGMENT
BLOCK2 DW ?
EXTRA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA,ES:EXTRA
START: MOV AX,DATA
MOV DS,AX
MOV AX,EXTRA
MOV ES,AX
MOV CX,000AH
LEA SI,BLOCK1
LEA DI,ES:BLOCK2
CLD
REPNZ MOVSW
MOV AX,4C00H
INT 21H
CODE ENDS
END START
[Marks-12]
Ans:
Macro: Small sequence of the codes of the same pattern are repeated frequently at different places
which perform the same operation on the different data of same data type, such repeated code can
be written separately called as Macro.
Syntax:
Macro_nameMACRO[arg1,arg2,…..argN)
…..
End
Procedure: A procedure is group of instructions that usually performs one task. It is a reusable
section of a software program which is stored in memory once but can be used as often as
necessary.
A procedure can be of two types. 1)Near Procedure 2) Far Procedure
21. Write any two differences between NEAR and FAR procedure.
Ans:
2. Write an assembly language program to solve p= x2+y2 using Macro. (x and y are 8 bit
numbers.
Ans:
PROG MACRO a,b
MOV al,a
MUL al
MOV bl,al
MOV al,b
MUL al
ADD al,bl
ENDM
DATA SEGMENT
x DB 02H
y DB 03H
p DB DUP()
DATA ENDS
CODE SEGMENT
A.G.Patil Polytechnic Institute, Solapur Page 50
Microprocessor Programming (314321)
ASSUME CS:CODE,DS:DATA
START:MOV ax,data
MOV ds,ax
PROG x, y
MOV p,al
MOV ah,4Ch
Int 21H
CODE ENDS
END START
In some situation it may happen that Procedure 1is called from main program Procrdure2 is called
from procedure1And procrdure1 is again called from procdure2. In this situation program
execution flow reenters in the procedure1. These types of procedures are called re enterant
procedures. The RET instruction at the end of procrdure1 returns to procedure2.
The RETinstruction at the end of procedure2 will return the execution to procedure1.Procedure1
will again executed from where it had stopped at the time of calling procrdure2 and the RET
instruction at the end of this will return the program execution to main program. The flow of
program execution for re-entrant procedure is as shown in FIG
Reentrant Procedure:
A reentrant procedure is one in which a single copy of the program code can be shared by multiple
users during the same period of time. Re-entrance has two key aspects: The program code cannot
modify itself and the local data for each user must be stored separately.
Recursive procedures:
An active procedure that is invoked from within itself or from within another active procedure is a
recursive procedure. Such an invocation is called recursion. A procedure that is invoked
recursively must have the RECURSIVE attribute specified in the PROCEDURE statement.
DATA SEGMENT
NUM1 DB 10H
NUM2 DB 20H
NUM3 DB 30H
NUM4 DB 40H
RESULT DB?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE,DS:DATA
START:MOV AX,DATA
MOV DS,AX
MOV AL,NUM1
MOV BL,NUM2
CALL SUM
MOV CL,AL
MOV AL, NUM3
MOV BL,NUM4
CALL SUM
MUL CL
MOV RESULT,AX
MOV AH,4CH
INT 21H
CODE ENDS
END START
6. Write ALP using macro to perform multiplication of two 8 Bit Unsigned numbers.
Ans:
; Macro For Multiplication
Syntax:
Macro_nameMACRO[arg1,arg2,…..argN)
….. Endm
Example:
MyMacro MACRO p1, p2, p3 ; macro definition with arguments
MOV AX, p1
MOV BX, p2
MOV CX, p3
ENDM ;indicates end of macro.
DATA SEGMENT
DATA ENDS
CODE SEGMENT ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MYMACRO 1, 2, 3 ; macro call
MYMACRO 4, 5, DX
MOV AH,4CH
INT 21H
CODE ENDS
END START
Advantages of Macro:
1. Simplify and reduce the amount of repetitive coding.
2. Reduces errors caused by repetitive coding.
3. Makes program more readable.
4. Execution time is less as compare to procedure as no extra instructions are required.
9. Write an assembly language program for sum of series of 05 numbers using procedure.
Ans:
DATA SEGMENT
NUM1 DB 10H,02H,30H,04H,05H
RESULT DB 1 DUP(0)
CARRY DB 0H
DATA ENDS
CODE SEGMENT
START:ASSUME CS:CODE,DS:DATA
MOV DX,DATA
MOV DS,DX
MOV CL,05H
CALL SERIES_ADD ;Procedure Call
MOV AX,4C00H
INT 21H
SERIES_ADD PROC
MOV SI, OFFSET NUM1
UP:MOV AL,[SI]
ADD RESULT,AL
JNC NEXT
INC CARRY
NEXT:INC SI
LOOP UP
RET
SERIES_ADD ENDP
CODE ENDS
END START
10. Explain with suitable example how parameters are passed on the stack. Also, list out different
parameter passing ways in procedure.
Ans:
To pass a large number of parameters to the called procedure, the parameters can be placed on the
stack for the calling procedure. Here, it is useful to use the stack base pointer i.e BP register to make a
frame boundary for easy access to the parameters. The stack can also be used to pass parameters back
from the called procedure to the calling procedure. The procedure during its execution pops back the
appropriate parameters as and when required.
In the example given below, The variable NUM is used in the called procedure using BP register,
which points to the corresponding location in the stack.