Friday, September 9, 2011

Statement: Write an ALP to find the smallest number amongst the numbers in the array. Assume that the length of the array is stored at memory location D000H and the array begins from memory location D001 H. Store the result at E000H.

LDA D000H
MOV C,A
LXI H,D001H
MOV A,M
INX M
LOOP:
CMP M
JC SKIP
MOV A,M
SKIP:
INX H
DCR C
JNZ LOOP
STA E000H
HLT

Explanation:

In this program we have not given the length of the array, it is stored at some memory
location.

First of all load the accumulator with the length of the array stored at memory location D000H. Copy the data of accumulator into register C.

HL pair is used as a memory pointer, and it points to the memory location D001H. Copy the data of memory location pointed by HL pair to the accumulator.(In short  copy the data of memory location D001H into the accumulator. ;-)  ) Memory pointer is incremented by one.

The 'Loop':
The data of accumulator and memory location is compared. If the data in accumulator is less than the data stored in memory location then it will jump to the 'SKIP' loop.Otherwise the data stored in memory location is smaller so it will be copied to the accumulator.

'SKIP' loop:

When the data in accumulator is smaller than the data stored at memory location at that time the data of accumulator will remain as it is, as we want to find the minimum number.So memory pointer is incremented by one, and counter is decremented by 1. Again it will go to the LOOP and will continue comparing the data of memory location and the accumulator until the counter is equal to zero.

At last, the smallest number will be stored at memory location E000H.

HLT stands for 'stop'. :-)

No comments:

Post a Comment