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'. :-)
Statement: Write an ALP to reverse the contents of a block of N bytes and transfer the
reverse contents to destination block.

MVI C,0AH
LXI H,E000H
LXI D,E100H
BACK:
MOV A,M
STAX D
INX H
DCX D
DCR C
JNZ BACK
HLT
 
Explanation:
First of all initialize the counter. Here we have a source block of 10 bytes. Therefore, we stored 0AH data into register C.
HL pair is used to initialize source memory pointer. (In this program it is E000H)
DE pair is used to initialize destination memory pointer.(In this program it is E100H - if your destination block is starting at suppose any memory location 'wxyz' then intialize the DE pair with 'wxyz+counter value'as we want to store the reverse contents to destination block so we are DECREMENTING it by one. )

'BACK'  Loop:

The data stored in first memory location of source block(E000H) is copied to the accumulator.
The data of accumulator is stored in register D.

Memory pointer is incremented by 1. (HL register pair.)
Destination pointer is decremented by 1. (DE register pair.) As we want to store the contents in reverse.

Register C is decremented by 1. IF it is not equal to zero then it will again go to the 'BACK' loop.

HLT stands for 'stop'. :-)
Statement: Multiply two 8 bit numbers stored in memory location D000H and D001H.
Store the result in memory locations E000H and E001H.

LDA D000H
MOV E,A
LDA D001H
MOV C,A
MVI D,00H
LXI H,0000H
BACK:
DAD D
DCR C
JNZ BACK
SHLD E000H
HLT

Explanation: 
[Remember: If we want to multiply 4 and 3 then 4*3= 4+4+4=3+3+3+3=12. In this program we are going to multiply two numbers using this method.]

First of all load the data (number) in accumulator stored at memory location D000H  using LDA instruction. Copy the data in register E. After that load accumulator with the data(another number) stored at memory location D001H and copy it in register C.

MVI D,00H instruction is used to clear the data of register D.(if there is any)
The same goes with HL register pair.

'BACK' Loop:

DAD instruction is used to add the specified register pair to HL pair. Here, we have D=00H and E=number or data which will be added to HL pair.Another number stored in register C works as a counter so we have decremented it by 1. After that if the value of register C is not zero then it will again jump to the 'Back' loop. So, 'number 1' will be added to itself,(How many times???) 'number 2' times.

When the value of C register becomes 00H it will jump out of the loop. HL pair will be containing the final result of multiplication, and it is stored at memory location E000H and E001 H using SHLD instruction.

HLT stands for 'stop'. :-)

Friday, September 2, 2011

Statement: Write an ALP of 8085 to calculate the sum of series of even numbers. Assume that the length of the series is stored at memory location E000 H and the series itself begins at memory location E001 H. Store the result at memory location F000H.

LDA E000H
MOV C,A
MVI B,00H
LXI H,E001H
L1:
MOV A,M
ANI 01H
JNZ L2
MOV A,B
ADD M
MOV B,A
L2:
INX H
DCR C
JNZ L1
MOV A,B
STA F000H
HLT

Explanation:


         In this program we have to find sum of series of even numbers. The length is stored at some memory location in this program it is E000H but it can be any location. The same way starting location of the series in this program is E001H but it can be any.

        So first of all we have copied the length of the series into the accumulator. Then this data is copied to the register C. After that we have intialized register B with 00H (We are going to use it to store the sum.). HL register pair is initialized with E001 H.

'L1' Loop:

       The data stored in memory location (M) pointed by HL pair is copied to accumulator. The data is 'anded' with 01H . ( If the result of ANDing is zero it indicates that the number is even.) If the result of ANDing is not zero then it will jump to L2.

       If the result is zero then the sum (data of register B) is copied to the accumulator and the data stored in memory location is added to the accumulator. (Remember, in the starting of this loop we had just COPIED the data of M to A so data is still stored at M.) The result is again copied at register B using MOV B,A.

'L2' Loop:


      HL pair is incremented by 1. The data in register C (=counter=length )is decremented by 1. If the data in register C is not equal to 00H then it'll jump to L1 loop.
     Otherwise  resultant sum is copied to accumulator then it is stored at memory location given in the statement.

HLT stands for 'stop'.
Statement: Write an ALP of 8085 to count the number of 1's in a byte stored in the H register and store the count in E register.

MVI B,00H
MVI C,08H
MOV A,H
L2:
RAR
JNC L1
INR B
L1:
DCR C 
JNZ L2
MOV E,B
HLT

Explanation:


         In this program we are given an 8 bit data and we have to find how many 1's are there in the data ok?  To find the number of 1's we are going to take two counter.
         So, first of all we have initialized counter 1 (register B) with 00H and counter 2 (register C) with 08 H (as H register will be having 8 bit data.)
Then we have copied the data of register H to accumulator A. 


'L2' Loop:


        In this loop the data stored in accumulator is rotated alongwith carry. After the rotation if there isn't any carry then it will jump to loop L1.  Otherwise counter 1 (register B) is incremented by 1.


'L1' Loop:


       This loop will come into picture when there is no carry. At that time counter 2 (register C) is decremented by 1. If C is not equal to 00H then it'll jump to L2 loop.
        If C is equal to 00H then the data of register B is copied into the register E, which is the number of 1's in the given data.


HLT stands for 'stop'.

Thursday, September 9, 2010

Statement: Take 10 bytes of data from RAM location 45H to 54 H. Add 05H to each of them & save the result data in RAM location 79H down to 70H

ORG 0000H
MOV R0,#10
MOV R1,#45H
MOV R2,#79H
BACK:
MOV A,@R1
ADD A,#05H
MOV @R2,A
INR R1
DCR R2
DJNZ R0,BACK
END


Explanation:-

First of all we have stored data 10 (decimal) in register R0.
Then we have stored 45H in register R1 and 79H in register R2

'BACK' loop:-

This loop starts with instruction MOV A,@R1
Here @R1 is used therefore the 8051 will load the accumulator with the value from Internal RAM which is specified by the data of  R1.Which means that the data of R1 is currently 45H. So accumulator will contain the data which is stored at memory location 45H. (Indirect Addressing)

After that whatever the data is copied accumulator will be added by 05H.

Then the data of the accumulator will be copied in the memory location which is specified by the data of register R2.

Data of R1 is incremented by 1. (as we want to go from 45 H to 54 H)
Data of R2 is decremented by 1.(as we want to go from 79 H to 70 H)
R0 is decremented by 1. (and it jumps to BACK if the value of register R0 is not zero.)

END stands for 'stop'.

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'.