##page no 164
##example no 6.1
##LOAD A DATA TO ONE REGISTER AND MOVE IT TO ANOTHER.
import math
def hex2dec(s):
return int(s, 16)
def dec2hex(n):
return "%X" % n
A=hex2dec('82')# ##storing the decimal value of hexadecimal no 82 in accumulator A
B=dec2hex(A)# ##storing the hexadecimal value of A in B
print('B=\n')
print (B)# ##displaying the hexadecimal number in register B
#page no 164
#example 6.2
#TO SWITCH ON SOME DEVICES
#let the switches which are ON are at bit no D0,D1,D2,D3,D6#
import math
def hex2dec(s):
return int(s, 16)
def dec2bin(n):
return ''.join(str(1 & int(n) >> i) for i in range(8)[::-1])
x=hex2dec('4F')# #hexadecimal to decimal conversion
y=dec2bin(x)# #decimal to binary conversion
print ('At output port 01H: ')# #same input appears at the putput
print (y)#
print ('Value 1s are showing the devices are ON.')
print ('Value 0s are showing the devices are switched OFF.')#
##page no 174
##example 6.3
##ADDITION OF TWO NUMBERS.
##93H is stored in accumulator. Converting it into decimal.
def hex2dec(s):
return int(s, 16)
def dec2hex(n):
return "%X" % n
A=hex2dec('93')#
##B7H is stored in register C. Converting it into decimal.
C=hex2dec('B7')#
X=A+C# ## the result comes out to be 330
Z=X-256#
##X=330# ## this is a decimal value. Converting it into hexadecimal
Y=dec2hex(Z)#
print ('Sum= ')
print (Y)#
if X>255:
print ('CY=1')
else:
print ('CY=0')
##page no 175
##example 6.4
##CONTINUATION OF PREVIOUS EXAMPLE.
##the sum of previous example is added to 35H
def dec2hex(n):
return "%X" % n
def hex2dec(p):
return int(p, 16)
S=hex2dec('4A'); ##4AH is converted into decimal value.
A=hex2dec('35'); ##35H is converted into decimal value
s=A+S; ##the result comes out to be 127. it is a decimal value
Y=dec2hex(s);
print ('Sum= ')
print (Y);
if s>255:
print ('CY=1')
else:
print ('CY=0')
##page no 175
##example no 6.5
##INCRIMENTING ACCUMULATOR CONTENT.
##accumulator holds the data FFH
def hex2dec(s):
return int(s, 16)
def dec2hex(n):
return "%X" % n
A=hex2dec('FF')# ##converting FFH into decimal value
##decimal value of 01H is 01. Adding 01 to A
Y=A+1# ##the result comes out to be 256
Z=Y-256#
X=dec2hex(Z)#
print ('Sum =')
print (X)#
if Y>255:
print ('CY=1 \n')
else:
print ('CY=0 \n')
if Z>127:
print ('S=1 \n')
else:
print ('S=0 \n')
if Z>0:
print ('Z=0 \n')
else:
print ('Z=1 \n')
##page no 179
##example 6.6
##SUBTRACTION OF TWO NUMBERS.
##accumulator has 97H. Converting it into decimal value
def hex2dec(s):
return int(s, 16)
def dec2hex(n):
return "%X" % n
A=hex2dec('97')#
##register B has 65H. Finding 2's compliment of 65H.
B=hex2dec('65')#
X=256-B#
Y=A+X#
S=Y-256#
Z=dec2hex(S)#
print ('Subtraction= ')
print (Z)#
if Y>255:
CY=1#
print ('The result is positive. \n')#
else:
CY=0#
print ('The result is negative. \n')
if S>127:
print ('S=1 \n')
else:
print ('S=0 \n')
if S>0:
print ('Z=0 \n')
else:
print ('Z=1 \n')
#page no 185
#example no 6.7
#PERFORMING LOGICAL OPERATIONS.
#register B holds 93H.Binary of 93H is 10010011
#register A holds 15H. Binary of 15H is 00010101.
def bitor(a,b):
d=dec2bin(a|b)
return d
def bitcmp(a):
s=~a
d=dec2bin(s)
return d
def dec2bin(n):
return ''.join(str(1 & int(n) >> i) for i in range(8)[::-1])
def bitxor(a,b):
d=dec2bin(a^b)
return d
def bin2dec(s):
return str(int(s,2))
B=int('10010011',2)# #taking the value of A in matrix form.
A=int('00010101',2)# #taking the value of B in matrix form.
Y= bitor(A,B)# # getting OR of A & B
print('OR of A & B is')
print(Y)#
if Y==1:
print('S=1 \n')#
else:
print('S=0 \n')#
if Y==0:
print('Z=1 \n')#
else:
print('Z=0 \n')#
print('CY=0 \n')#
R=bitxor(A,B)# #getting XOR of A & B
print('XOR of A & B is')
print(R)#
if R==1:
print('S=1 \n')#
else:
print('S=1 \n')#
if R==0:
print('Z=1 \n')#
else:
print('Z=0 \n')#
print('CY=0 \n')#
K=bitcmp(A)# #getting the compliment of A
print('Compliment of A is: \n')#
print(K)#
##page no 186
##example no 6.8
##KEEPING THE RADIO ON.
##to keep the radio on without affecting the other appliances, the D4 bit should always be 1
##assuming an input input binary 10101010
import numpy
A=[1, 0, 1, 0, 1, 0, 1, 0]#
B=[0, 0, 0, 1, 0, 0, 0, 0]#
Y=numpy.asarray(A)|numpy.asarray(B)# ##ORing input (A) with B to keep the D4 bit always set
print (Y)#
print ('D4 bit will always be one without affecting the other bits')#
##page no 187
##example no 6.9
##TURN OFF THE AIR CONDITIONER.
##to turn OFF the air conditioner, reset bit D7
##Assuming the same input as earlier as it is a continuation of previous example.
import numpy
A=[1,0,1,0,1,0,1,0]#
B=[0,1,1,1,1,1,1,1]#
Y=numpy.asarray(A)&numpy.asarray(B)# ##ANDing input (A) with B to keep the D4 bit always set
print (Y)#
print ('D7 bit will always be zero without affecting the other bits')#