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

No comments:

Post a Comment