CHAPTER 10 -Code Conversion BCD Arithmetic and 16 bit Data Operations

EXAMPLE 10.1 - PG NO:310

In [1]:
# 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')#
binary equivalent of 72 is=
1001000


Unpacked BCD1 
00000010

 
 Unpacked BCD2
00000111

 
 Multiply BCD2 by 10 and add BCD1

EXAMPLE 10.2 - PG NO: 321

In [2]:
## 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')
After addition BCD1 is: 
10101.
MSB of this sequence is the carry generated after addition. 
 

After addition BCD2 is: 
10010.
MSB of this sequence is the carry generated after addition.

 
BCD1 :  0101 
 

BCD2:  0010

EXAMPLE 10.3 - PG NO:325

In [3]:
## 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');
2050H--> 3FH 
 

2051H--> 42H 
 

DE--> 856FH 

D--> 85H      E--> 6FH 
 

LHLD 2050H 

H--> 42H      L--> 3FH 
 

XCHG 

D<-->H        E<-->L 

D--> 42H      E--> 3FH 
 H--> 85H      L--> 6FH 
 

SHLD 2050H 

2050H--> 6FH 

2051H--> 85H

EXAMPLE 10.4 - PG NO:326

In [4]:
## 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)#
B--> 27H      C--> 93H 

D--> 31H      E--> 82H 
 

MOV A,C 
 

ADD E 

Sum =
15
CY=1 
 

MOV L,A 

L-->
15

 
 MOV A,B 
 

ADC D 

Sum =
59
CY=0 
 

MOV H,A 

H-->
59

 
 SHLD 2050H 

2050H--> 
15
2051H--> 
59

EXAMPLE 10.5 - PG NO:326

In [5]:
## 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)#
B--> 85H      C--> 38H 

D--> 62H      E--> A5H 
 

MOV A,C 
 

SUB E 

Difference =
93
Borrow=1 
 

MOV C,A 

C-->
93

 
 MOV A,B 
 

SBB D 

Difference =
22
Borrow=0 
 

MOV B,A 

B-->
22

EXAMPLE 10.6 - PG NO:327

In [6]:
## 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');
LXI H,0000H 

H--> 00H      L--> 00H 
 

DAD SP 

H--> higher bytes of stack pointer register 

L--> lower bytes of stack pointer register 
 

MOV A,H 

H--> A 
 

OUT PORT1 
 

MOV A,L 

L--> A 
 

OUT PORT2

EXAMPLE 10.7 - PG NO:327

In [7]:
## 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')#
CHECK:   PUSH H 
 

         MVI L,FFH 

         L--> 
11111111.

 
         PUSH PSW 
 

         XTHL 
 

         POP PSW 
 

         JZ NOEROR 
 

         JMP ERROR 
 

NOEROR:  POP H 
 

         RET

EXAMPLE 10.8 - PG NO:328

In [8]:
## 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');
 
The program can be transfered using Jump instruction. 
 

PCHL is a 1 byte instruction that can also be used in place of Jump instruction