CHAPTER 7 - Programming Techniques With Additional Instructions

EXAMPLE 7.1 - PG NO: 216

In [1]:
##page no 216
##example no 7.1
##STEPS TO ADD 10 BYTES OF DATA.
print ('The micriprocessor needs :')#
print ('a. a counter to count 10 data bytes')#
print ('b. an index or a memory pointer to locate where data bytes are stored')#
print ('c. to transfer data from a memory location to the microprocessor')#
print ('d. to perform addition')#
print ('e. registers for temporary storage of partial answers')#
print ('f. a flag to indicate the completion of the task')#
print ('g. to store or output the result')#
The micriprocessor needs :
a. a counter to count 10 data bytes
b. an index or a memory pointer to locate where data bytes are stored
c. to transfer data from a memory location to the microprocessor
d. to perform addition
e. registers for temporary storage of partial answers
f. a flag to indicate the completion of the task
g. to store or output the result

EXAMPLE 7.2 - PG NO: 219

In [2]:
##page no 219
##example no 7.2
##LOADING 16-BIT NUMBER.
##working of LXI instruction.
print ('LXI H,2050H'); ## loads HL register pair.
print ('L=50H'); ## 50H in L register.
print ('H=20H'); ##20H in H register pair.
print ('LXI instruction takes 3 bytes of memory and 10 clock periods.')
##working of MVI instruction.
print ('MVI H,20H');
print ('H=20H'); ## load 20H in register H.
print ('MVI L,50H'); ## load 50H in register L.
print ('L=50H');
print ('2 MVI instructions take 4 bytes of memory and 14 clock periods.')
LXI H,2050H
L=50H
H=20H
LXI instruction takes 3 bytes of memory and 10 clock periods.
MVI H,20H
H=20H
MVI L,50H
L=50H
2 MVI instructions take 4 bytes of memory and 14 clock periods.

EXAMPLE 7.3 - PG NO: 220

In [3]:
#page no 220
#example no 7.3
# TRANSFER OF DATA BYTES TO ACCUMULATOR.
# Memory location 2050H has the data F7H.

# using MOV instruction.
#indirect addressing mode.
def hex2dec(s):
	 return int(s, 16)

def dec2hex(n):
	 return "%X" % n


print '%s' %('LXI H,2050H')#
print '%s' %('H=20H   L=50H \n \n')# # the 16-bit address of the data is loaded in HL register pair.
M=hex2dec('F7')# # M is the memory location pointer of address 2050H.
print '%s' %('MOV A,M ')#
A=dec2hex(M)#
print '%s' %('A= ')#
print (A)# # the contents of the HL register pair are used as memory pointer to the location 2050H.

# using LDAX instruction.
# indirect addressing mode.

print '%s' %('LXI B,2050H')#
print '%s' %('B=20H   C=50H \n \n')# # the 16-bit address of the data is loaded in BC register pair.
M=hex2dec('F7')# # M is the memory location pointer of address 2050H.
print '%s' %('LDAX B ')#
A=dec2hex(M)#
print '%s' %('A= ')#
print (A)# # the contents of the BC register pair are used as memory pointer to the location 2050H.

# using LDA instruction.
# direct addressing mode.
print '%s' %('\n LDA 2050H \n')# #directly sends the data of memory location 2050H to accumulator.
print '%s' %('A= ')#
print (A)#
LXI H,2050H
H=20H   L=50H 
 

MOV A,M 
A= 
F7
LXI B,2050H
B=20H   C=50H 
 

LDAX B 
A= 
F7

 LDA 2050H 

A= 
F7

EXAMPLE 7.4 - PG NO: 222

In [4]:
#page no 222
#example no 7.4
# USE OF ADDRESSING MODES.
#register B contains 32H
B=32#

#using indirect addressing modes
print '%s %d' %('B= ',B )#
print('\n')#
print '%s' %('1) LXI H,8000H')# # loads HL register pair.
print('\n')#
print '%s' %('H=80H     L=00H')# 
print('\n')#
print '%s' %('MOV M,B')# # contents of register B are moved in memory location pointed by HL register pair.

M=B# 

print '%s %d' %('\n 8000H --> ',M)#
print('\n')#
print '%s' %('LXI D,8000H')# #loads the memory location 8000H in DE register pair.
print('\n')#
print '%s' %('D=80H     E=00H')# 
print('\n')#
print '%s' %('MOV A,B')#

A=B#

print '%s %d' %('A= ',A)#
print('\n')#
print '%s' %('STAX D')# #stores the value of accumulator in the memory location pointer by DE register pair.
print('\n')#
print '%s %d' %('8000H --> ',A)#
print('\n')#
#using direct addressing mode.
print '%s' %('2) A= F2H')#
print('\n')#
print '%s' %('STA 8000H')# #this instruction stores the value of accumulator in the memory location 8000H.
print('\n')#
print '%s' %('8000H --> F2H')#
print('\n')#
#using indirect addressing mode.
print '%s' %('3) LXI H,8000H')# # loads HL register pair.
print('\n')#
print '%s' %('H=80H     L=00H')# 
print('\n')#
print '%s' %('MVI M,F2H')# #moving the data in the memory.
print('\n')#
print '%s' %('8000H --> F2H')#
B=  32


1) LXI H,8000H


H=80H     L=00H


MOV M,B

 8000H -->  32


LXI D,8000H


D=80H     E=00H


MOV A,B
A=  32


STAX D


8000H -->  32


2) A= F2H


STA 8000H


8000H --> F2H


3) LXI H,8000H


H=80H     L=00H


MVI M,F2H


8000H --> F2H

EXAMPLE 7.5 - PG NO: 224

In [5]:
#page no 224
#example no 7.5
# INCREMENT A NUMBER.
print '%s' %('LXI B,2050H \n')# #loads the data 2050H in BC register pair.
print '%s' %('B=20H   C=50H \n')#
B=20#
C=50#
print '%s' %('INX B')#
C=C+1#
print '%s %d %s %d' %('B= ',B, 'C= ',C)#
print('\n')#
print '%s' %('The contents of BC register pair will be 2051H')#
print('\n')#
print '%s' %('INR B')#
B=B+1#
print '%s %d' %('B=',B)#
print('\n')#
print '%s' %('INR C')#
C=50#
C=C+1#
print '%s %d' %('C=',C)#
print('\n')#
print '%s' %('The contents of BC register pair will be 2151H')#
LXI B,2050H 

B=20H   C=50H 

INX B
B=  20 C=  51


The contents of BC register pair will be 2051H


INR B
B= 21


INR C
C= 51


The contents of BC register pair will be 2151H

EXAMPLE 7.6 - PG NO: 228

In [6]:
##page no 228
##example no 7.6
## ARITHEMETIC OPERATIONS.
def hex2dec(s):
	 return int(s, 16)

def dec2hex(n):
	 return "%X" % n


print ('A-->30H \n')#
print ('2040H-->68H \n')#
print ('2041H-->7FH \n')#
print ('LXI H,2040H \n')# ## loads HL register pair.
print ('H=20H     L=40H   M=68H \n')# 
print ('ADD M \n')#
A=hex2dec('30')#
M=hex2dec('68')#
S=A+M# ## adds the contents of A and data at memory location 2040H.
s=dec2hex(S)#
print ('\n Content of A after addition with 2040H= ')#
print (s)#
print('\n')
print ('INX H \n')# ## takes the program to the next memory location.
print ('H=20H     L=41H   M=7FH \n')# 
print ('SUB M \n')#
M=hex2dec('7F')#
D=S-M# ## subtracts the contents of A from the data at memory location 2041H.
d=dec2hex(D)#
print ('\n Content of A after subtraction with 2041H= ')#
print (d)#
A-->30H 

2040H-->68H 

2041H-->7FH 

LXI H,2040H 

H=20H     L=40H   M=68H 

ADD M 


 Content of A after addition with 2040H= 
98


INX H 

H=20H     L=41H   M=7FH 

SUB M 


 Content of A after subtraction with 2041H= 
19

EXAMPLE 7.7 - PG NO: 229

In [7]:
##page no 229
##example no 7.7
## INCREMENT & DECREMENT.
def hex2dec(s):
	 return int(s, 16)

def dec2hex(n):
	 return "%X" % n


print ('LXI H,2040H\n' )# ## loads HL register pair.
print ('H=20H     L=40H\n' )#
print ('MVI M,59H\n' )# 
M=59#
M=hex2dec('59' )#
print ('2040H-->59H\n' )
print ('INR M\n' )#
M=M+1# ## increments the value at the memory location by 1.
m=dec2hex(M)#
print ('Content of 2040H after increment= ' )#
print (m)#
print('\n' )
print ('INX H\n' )# ##takes the program to the next memory location.
print ('H=20H     L=41H\n' )#
print ('MVI M,90H\n' )#
M=90#
M=hex2dec('90' )#
print ('2041H-->90H\n' )#
print ('DCR M\n' )#
M=M-1# ##decrements the value at the memory location by 1.
m=dec2hex(M)#
print ('Content of 2041H after decrement= ' )#
print (m)#
LXI H,2040H

H=20H     L=40H

MVI M,59H

2040H-->59H

INR M

Content of 2040H after increment= 
5A


INX H

H=20H     L=41H

MVI M,90H

2041H-->90H

DCR M

Content of 2041H after decrement= 
8F

EXAMPLE 7.8 - PG NO: 233

In [8]:
##page no 233
## example no 7.8
## LEFT ROTATION (RLC) OF BITS.
## initially
print ('Accumulator= AAH \n');
print ('D7 D6 D5 D4 D3 D2 D1 D0 \n');
print (' 1  0  1  0  1  0  1  0   =AAH \n \n');
print ('CY= 0 \n \n');
print ('RLC \n \n');
print ('CY= 1 \n \n');
## carry flag is set because D7 bit was 1.
print ('D7 D6 D5 D4 D3 D2 D1 D0 \n');
print (' 0  1  0  1  0  1  0  1   =55H \n \n'); ## after the executuion of first RLC.
## RLC instruction places D7 bit in CY flag as well as in D0 bit.
print ('RLC \n \n');
print ('CY= 0 \n \n');
## carry flag is reset because D7 bit was 0.
print ('D7 D6 D5 D4 D3 D2 D1 D0 \n');
print (' 1  0  1  0  1  0  1  0   =AAH \n \n'); ## after the executuion of second RLC.
## RLC instruction places D7 bit in CY flag as well as in D0 bit.
Accumulator= AAH 

D7 D6 D5 D4 D3 D2 D1 D0 

 1  0  1  0  1  0  1  0   =AAH 
 

CY= 0 
 

RLC 
 

CY= 1 
 

D7 D6 D5 D4 D3 D2 D1 D0 

 0  1  0  1  0  1  0  1   =55H 
 

RLC 
 

CY= 0 
 

D7 D6 D5 D4 D3 D2 D1 D0 

 1  0  1  0  1  0  1  0   =AAH 
 

EXAMPLE 7.9 - PG NO: 234

In [9]:
##page no 234
## example no 7.9
## LEFT ROTATION (RAL) OF BITS.
## initially
print ('Accumulator= AAH \n');
print ('D7 D6 D5 D4 D3 D2 D1 D0 \n');
print (' 1  0  1  0  1  0  1  0   =AAH \n \n');
print ('CY= 0 \n \n');
print ('RAL \n \n');
print ('CY= 1 \n \n');
## carry flag is set because D7 bit was 1.
print ('D7 D6 D5 D4 D3 D2 D1 D0 \n');
print (' 0  1  0  1  0  1  0  0   =54H \n \n'); ## after the executuion of first RAL.
## RAL instruction places D7 bit in CY flag & CY flags bit is send to D0 bit.
print ('RAL \n \n');
print ('CY= 0 \n \n');
## carry flag is reset because D7 bit was 0.
print ('D7 D6 D5 D4 D3 D2 D1 D0 \n');
print (' 1  0  1  0  1  0  0  1   =A9H \n \n'); ## after the executuion of second RAL.
## RAL instruction places D7 bit in CY flag & CY flags bit is send to D0 bit.
Accumulator= AAH 

D7 D6 D5 D4 D3 D2 D1 D0 

 1  0  1  0  1  0  1  0   =AAH 
 

CY= 0 
 

RAL 
 

CY= 1 
 

D7 D6 D5 D4 D3 D2 D1 D0 

 0  1  0  1  0  1  0  0   =54H 
 

RAL 
 

CY= 0 
 

D7 D6 D5 D4 D3 D2 D1 D0 

 1  0  1  0  1  0  0  1   =A9H 
 

EXAMPLE 7.10 - PG NO: 235

In [10]:
##page no 235
## example no 7.10
## RIGHT ROTATION (RRC & RAR) OF BITS.
## initially
print ('Accumulator= 81H \n');
print ('D7 D6 D5 D4 D3 D2 D1 D0 \n');
print (' 1  0  0  0  0  0  0  1   =81H \n \n');
print ('CY= 0 \n \n');
print ('RRC \n \n');
print ('CY= 1 \n \n');
## carry flag is set because D0 bit was 1.
print ('D7 D6 D5 D4 D3 D2 D1 D0 \n');
print (' 1  1  0  0  0  0  0  0   =C0H \n \n'); ## after the executuion of RRC.
## RRC instruction places D0 bit in CY flag as well as in D7 bit.



## initially
print ('Accumulator= 81H \n');
print ('D7 D6 D5 D4 D3 D2 D1 D0 \n');
print (' 1  0  0  0  0  0  0  1   =81H \n \n');
print ('CY= 0 \n \n');
print ('RAR \n \n');
print ('CY= 1 \n \n');
## carry flag is set because D0 bit was 1.
print ('D7 D6 D5 D4 D3 D2 D1 D0 \n');
print (' 0  1  0  0  0  0  0  0   =40H \n \n'); ## after the executuion of RAR.
## RAR instruction places D0 bit in CY flag & CY flags bit is send to D7 bit.
Accumulator= 81H 

D7 D6 D5 D4 D3 D2 D1 D0 

 1  0  0  0  0  0  0  1   =81H 
 

CY= 0 
 

RRC 
 

CY= 1 
 

D7 D6 D5 D4 D3 D2 D1 D0 

 1  1  0  0  0  0  0  0   =C0H 
 

Accumulator= 81H 

D7 D6 D5 D4 D3 D2 D1 D0 

 1  0  0  0  0  0  0  1   =81H 
 

CY= 0 
 

RAR 
 

CY= 1 
 

D7 D6 D5 D4 D3 D2 D1 D0 

 0  1  0  0  0  0  0  0   =40H 
 

EXAMPLE 7.11 - PG NO: 241

In [11]:
#page no 241
#example no 7.11
# COMPARISION OF DATA.
def hex2dec(s):
     return int(s, 16)

def dec2bin(n):
    return ''.join(str(1 & int(n) >> i) for i in range(8)[::-1])

def dec2hex(n):
	 return "%X" % n


def isequalbitwise(a,b):
	e=a-b
	if(e==0):
		return 'True'
	else:
		return 'False'



print('MVI A,64H')# #loads accumulator with 64H.
print('A-->64H')#
print('LXI H,2050H')# # loads HL register pair.
print('H=20H     L=50H')#
print('M-->9AH')# #assumed in the solution.
print('CMP M')#
#this command compares the contents of A with M by subtracting M from A.
A=hex2dec('64')#
#register M has 9AH. Finding 2's compliment of 9AH.
M=hex2dec('9A')#
t=isequalbitwise(A,M)# #compares the two datas bitwise.
if(A==M):            # Jump condition
    print('\n Result after comparision is= ')#
    print('OUT1')#
else:
   print('Result after comparision is= ')#
   print(t)# #this shows the false condition of the bitwise comparision.
MVI A,64H
A-->64H
LXI H,2050H
H=20H     L=50H
M-->9AH
CMP M
Result after comparision is= 
False