Sunday, April 25, 2010

Statement: Data bytes are stored in ascending order in memory location starting from E000 H to E00F H. Write an ALP to insert an additional data byte in the data sequences so that its ascending order is maintained.

LXI H,E000 H
MVI B,11 H
MVI A,55 H
NET:
CMP M
JC NEXT
INX H
DCR B
JNZ NET
NEXT:
MOV C,A
MOV A,M
MOV M,C
INX H
DCR B
JNZ NEXT
HLT

Explanation:
Here we have memory locations from E000 H to E000F H , means we have total 16 memory location so we have to execute the loop 11H time. Accumulator has given the data 55H.

'NET' loop:
first of all we have compared the data of memory with accumulator. (A-M) If the data of memory is bigger than the data stored in accumulator then there will be a carry and it will jump to the loop NEXT.

If there is not any carry then HL pair will be incremented by 1. Data of register B will be decremented by 1. If the data of register B is not zero then it will jump to the loop NET again.

'NEXT' loop:
The data of accumulator has been moved to register C.
The data of memory has been moved to the accumulator.
The data of register C has been moved to the memory location pointed by HL pair. (It is just like swaping two data.)
HL pair is incremented by 1.
Data of register B is decremented by 1.
If it is not zero it will jump to the loop NEXT.
This loop will be executed when the data stored in accumulator is smaller then the data stored in memory and we want to put this data in ascending order in given memory block.  So we have swaped the data of accumulator and memory to put it in memory  block. After adding the data of accumulator in memory block we want the other data as it was. So we have written JNZ NEXT to write all other data in the same order as it was.

HLT stands for 'Stop'.

No comments:

Post a Comment