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

Sunday, April 11, 2010

Statement: A set of 8 data are stored in memory location starting from F000H. Write a program to subtract two bytes at a time & store the result in same memory location with first byte replaced by the result of subtraction & second byte replaced with 01 H if result is in 2's complement otherwisee 00H

LXI H,F000 H
MVI B,04 H
NET:
MOV A,M
INX H
SUB M
JM MIN
DCX H
MOV M,A
MVI A,00 H
JMP NEXT
MIN:
DCX H
MOV M,A
MVI A,01 H
NEXT:
INX H
MOV M,A
INX H
DCR B
JNZ NET
HLT

Explanation:

Here we have given 8 data but we have to subtract 2 bytes at a time. So the loop should be executed 4 times as 4*2=8. Because of this register B is given the data 04 H.

Now first of all memory location is assigned to HL register pair. (NET loop)The data of memory location F000 H is copied in to accumulator. Then the memory address assigned to HL register pair is incremented by 1. After that the data stored in that memory location is subtracted from the data of accumulator. Result will be stored in accumulator. (A-M ->A)

If the result is negative(means if it is 2's complement) then the sign flag will be affected and loop MIN will be executed. Memory address of HL pair will be decremented the result of subtraction will be stored in memory location pointed by HL pair and 01 H data will be moved to accumulator. After that NEXT loop will be executed.

If the sign flag is not affected then memory address pointed by HL pair will be decremented by 1. The result will be stored in that memory location and accumulator will be stored with 00 H.

After that it will jump to NEXT loop.

In this loop first of all memory address will be incremented by 1 and the data of accumulator is moved to memory. Now you would think why we have done this right? Remember earlier the memory location of HL pair is decremented by 1. ( if it is executing first time then HL pair will be having F000 H and not the F001 H so in this loop first we will make it to F001 H understood??)

Then again it will incremented by 1 (for our example here it will become F003 H) Then the value of register B will be decremented if it is not zero then NET loop will be executed again.

HLT stands for 'stop'. :-)

Wednesday, April 7, 2010

Statement: A set of memory is stored in memory locations starting from F000H & the last reading is the set is FFH. Write an ALP to check each byte in the string & save the bytes in the reange of (100)d to (150)d (both inclusive) in memory location starting from F020 H. Also store in memory location F01F H the number of bytes accepted from the given set which lies within given range.

LXI H,F000H
LXI B,F020H
NXT:
MOV A,M
CPI FF
JZ END
CPI 63H
JC END1
CPI 97H
JNC END1
STAX B
INX B
INR D
END1:
INX H
JMP NXT
END:
LXI H,F01FH
MOV M,D
HLT.

Explanation:
In this program, the last reading is FF H so first of all we will compare the data with FF. So when the data will be FF H it will give zero as a result and it will jump to the END loop.

The other thing which is given is the range! It is 100 to 150 in decimal. So in HEX it is 64 H to 96 H. But it also says that 100 and 150 are included. So for comparison our range will be 63 H to 97 H so that if  the data is 64 H or 96 H, it will execute the loop.

The data which is stored in accumulator is compared with 63 H, if the data is smaller then 63 H it will jump to the END loop. If data is bigger then 63 H then it will be compared with 97 H. If the data is greater then 97 H then END loop will be executed. Otherwise the content of accumulator will be stored in BC register pair. Then the memory location of BC register pair is incremented and the content of  register D is also incremented. After that the memory location of HL register pair is incremented and the NXT loop will be executed again.

When the data is FF H , the END loop is executed that means F01F memory location is assigned by HL register pair. The data of register D is moved to the memory location F01F H.( it will give the number of how many times NXT loop is executed.)

HLT stands for  'Stop'.

Monday, April 5, 2010

Statement: Write an ALP to find out maximum number in a block of data. The block starts at E000H. The first data show length of block. Store the result at the end of block.

LXI H,E000
MOV B,M
INX H
MOV A,M
NEXT:
INX H
CMP M
JNC END
MOV A,M
END:
DCR B
JNZ NEXT
INX H
MOV M,A
HLT
Statement: Write an ALP to add the two numbers FFH & 0B H. From the result subtract numbers 05 H. Store highter
order byte in H & store lower order byte in L.

MVI A,FFH
MVI B,0BH
ADD B
SBI 05
MOV L,A
MVI A,00
RAL
MOV H,A
HLT
Statement: Write an ALP to add the two numbers FFH & 0B H. From the result subtract numbers 05 H. Store highter
order byte in H & store lower order byte in L.

MVI A,FFH
MVI B,0BH
ADD B
SBI 05
MOV L,A
MVI A,00
RAL
MOV H,A
HLT
Statement: Write an ALP to delete the data byte stored at memory location whose address is stored at DE register.

LXI D,F000H
XCHG
MVI M,11H (this can be any number...)
MVI A,00H
STA F000H
HLT
Statement: A two 16 bit data are stored in consecutive memory location starting from F000H with lower bytes stored first.Write an ALP to copy this data in four consecutive memory location starting from E000H

LXI H,1221H (it can be any number)
LXI D,2211H
SHLD F000H
XCHG
SHLD F002H
XCHG
SHLD E000H
XCHG
SHLD E002H
HLT
Statement: Write an ALP to exchange the 16 bit data stored in register BC & register DE (with & without XCHG)

LXI B,1122H (it can be any data)
LXI D,1221H(it can be any data)
MOV H,D
MOV L,E
MOV D,B
MOV E,C
MOV B,H
MOV C,L
HLT

With XCHG:-

LXI B,1122H( it can be any data)
LXI D,1221H ( it can be any data)
MOV H,B
MOV L,C
MOV B,D
MOV C,E
XCHG
HLT
Statement: Write an Assembly Language Program to interchange the data content stored at memory location F000H and F001H

LXI H,1122H (it can be any data)
SHLD F000
XCHG
MOV H,E
MOV L,D
SHLD F000
HLT