# page no 310
# example 10.1
# BCD TO BINARY
# BCD into its binary equivalent.
# given BCD no is 72
import math
def dec2bin(n):
return ''.join(str(1 & int(n) >> i) for i in range(8)[::-1])
def modulo(a,n):
b=a%n
return b
a=72#
print('binary equivalent of 72 is=')
print(bin(72)[2:])
x=modulo(a,10)# # seperating the units digit
print('\n')
print('Unpacked BCD1 ')
print(dec2bin(x))#
a=a/10# # seperating the tens place digit
a=math.floor(a)#
print('\n \n Unpacked BCD2')#
print(dec2bin(a))#
print('\n \n Multiply BCD2 by 10 and add BCD1')#
## page no 321
## example no 10.2
## ADDITION OF PACKED BCD NUMBERS
import math
def dec2bin(f):
if f >= 1:
g = int(math.log(f, 2))
else:
g = -1
h = g + 1
ig = math.pow(2, g)
st = ""
while f > 0 or ig >= 1:
if f < 1:
if len(st[h:]) >= 10: # 10 fractional digits max
break
if f >= ig:
st += "1"
f -= ig
else:
st += "0"
ig /= 2
st = st[:h] + "." + st[h:]
return st
a=77#
b=48#
x=a%10#
y=b%10#
z=x+y#
if z>9:
p=z+6#
print ('After addition BCD1 is: ')
print (dec2bin(p))#
print ('MSB of this sequence is the carry generated after addition. \n \n')
else:
print ('After addition BCD1 is: ')
print (z)#
x=a/10#
x=math.floor(x)#
y=b/10#
y=math.floor(y)#
z=x+y#
if z>9:
p=z+6#
p=p+1# ## this 1 is the carry of BCD1.
print ('After addition BCD2 is: ')
print (dec2bin(p))#
print ('MSB of this sequence is the carry generated after addition.')
else:
print ('After addition BCD1 is: ')
print (z)#
print ('\n \nBCD1 : 0101 \n \n')#
print ('BCD2: 0010')
## page no 325
## example no 10.3
## EXCHANGE OF DATA
print ('2050H--> 3FH \n \n');
print ('2051H--> 42H \n \n');
print ('DE--> 856FH \n');
print ('D--> 85H E--> 6FH \n \n');
print ('LHLD 2050H \n'); ## loads the HL register pair with data on 2050H & 2051H.
print ('H--> 42H L--> 3FH \n \n');
print ('XCHG \n'); ## exchange the data of HL register pair with DE register pair.
print ('D<-->H E<-->L \n');
print ('D--> 42H E--> 3FH \n H--> 85H L--> 6FH \n \n');
print ('SHLD 2050H \n'); ## stores the 16bit dat in HL register pair on memory location 2051H & 2050H.
print ('2050H--> 6FH \n');
print ('2051H--> 85H');
## page no 326
## examle no 10.4
## ADDITION OF TWO 16 BIT NUMBERS
def hex2dec(s):
return int(s, 16)
def dec2hex(n):
return "%X" % n
print ('B--> 27H C--> 93H \n')#
print ('D--> 31H E--> 82H \n \n')#
b=hex2dec('27')#
c=hex2dec('93')#
d=hex2dec('31')#
e=hex2dec('82')#
print ('MOV A,C \n \n')#
a=c#
print ('ADD E \n')#
a=a+e#
Z=a-256#
X=dec2hex(Z)#
print ('Sum =')
print(X)#
if a>255:
print ('CY=1 \n \n')#
CY=1#
else:
print ('CY=0 \n')#
CY=0#
print ('MOV L,A \n')#
print ('L-->')#
print(X)#
print ('\n \n MOV A,B \n \n')#
a=b#
print ('ADC D \n')#
a=a+d+CY# ## CY is added because of the previous carry as per the instructions ADC (add with carry)
T=dec2hex(a)#
print ('Sum =')
print(T)#
if a>255:
print ('CY=1 \n \n')
else:
print ('CY=0 \n \n')
print ('MOV H,A \n')#
print ('H-->')#
print(T)#
print ('\n \n SHLD 2050H \n')# ## stores the contents of HL register pair on memory locations 2051H & 2050H.
print ('2050H--> ')#
print(X)#
print ('2051H--> ')#
print(T)#
## page no 326
## examle no 10.5
## SUBTRACTION OF TWO 16 BIT NUMBERS
def hex2dec(s):
return int(s, 16)
def dec2hex(n):
return "%X" % n
print ('B--> 85H C--> 38H \n')#
print ('D--> 62H E--> A5H \n \n')#
b=hex2dec('85')#
c=hex2dec('38')#
d=hex2dec('62')#
e=hex2dec('A5')#
print ('MOV A,C \n \n')#
a=c#
print ('SUB E \n')#
a=a-e#
Z=a+256#
X=dec2hex(Z)#
print ('Difference =')
print (X)#
if a<0:
print ('Borrow=1 \n \n')#
B=1#
else:
print ('Borrow=0 \n')
B=0#
print ('MOV C,A \n')#
print ('C-->')#
print (X)#
print ('\n \n MOV A,B \n \n')#
a=b#
print ('SBB D \n')#
a=a-d-B# ## 1 is subtracted because of the previous borrow as per the instructions SBB (subtract with borrow)
T=dec2hex(a)#
print ('Difference =')
print (T)#
if a<0:
print ('Borrow=1 \n \n')
else:
print ('Borrow=0 \n \n')
print ('MOV B,A \n')#
print ('B-->')#
print (T)#
## page no 327
## example no 10.6
## DISPLAY CONTENTS OF STACK
print ('LXI H,0000H \n'); ## clears the HL register pair
print ('H--> 00H L--> 00H \n \n');
print ('DAD SP \n'); ## place the stack pointer content in HL
print ('H--> higher bytes of stack pointer register \n');
print ('L--> lower bytes of stack pointer register \n \n');
print ('MOV A,H \n'); ## copies the contents of H in A.
print ('H--> A \n \n');
print ('OUT PORT1 \n \n');
print ('MOV A,L \n'); ## copies the contents of L in A.
print ('L--> A \n \n');
print ('OUT PORT2');
## page no 327
## example no 10.7
## SUBROUTINE TO SET THE ZERO FLAG
import math
def hex2dec(s):
return int(s, 16)
def dec2bin(f):
if f >= 1:
g = int(math.log(f, 2))
else:
g = -1
h = g + 1
ig = math.pow(2, g)
st = ""
while f > 0 or ig >= 1:
if f < 1:
if len(st[h:]) >= 10: # 10 fractional digits max
break
if f >= ig:
st += "1"
f -= ig
else:
st += "0"
ig /= 2
st = st[:h] + "." + st[h:]
return st
print ('CHECK: PUSH H \n \n')# ## sends the contents of H to the location pointed by the stack pointer.
print (' MVI L,FFH \n')#
l=hex2dec('FF')#
l=dec2bin(l)#
print (' L--> ')# ## set all bits in L to logic 1.
print(l)#
print ('\n \n PUSH PSW \n \n')# ## save flags on top of the stack
print (' XTHL \n \n')# ## set all bits in the top stack location.
print (' POP PSW \n \n')# ## now the zero flag is set.
print (' JZ NOEROR \n \n')#
print (' JMP ERROR \n \n')#
print ('NOEROR: POP H \n \n')# ## retrives the data from the stack into H if zero flag is set
print (' RET')#
## page no 328
## example no 10.8
## TRANSFER A PROGRAM TO AN ADDRESS IN HL REGISTER
print ('\n \nThe program can be transfered using Jump instruction. \n \n');
print ('PCHL is a 1 byte instruction that can also be used in place of Jump instruction \n \n');