Running 2 Youtube Channel : The Masti Adda (Fun based ) : The Knowledge Adda (Knowledge based) : Subscribe for Knowledge with Fun....

Write an assembly language program to calculate the sum of an array of numbers

Written by @
The length of array is not known. The memory location after the last element has the data FF H. Sum can be 8 bit or more than 8-bit.
Apparatus Used: Microprocessor Kit, Keyboard
Assumption: In this program the array starts at memory location 2200 H and we have to store sum 2250 and carry at 2251.This program can not use a counter to terminate the additional loop. The loop is terminated by checking each number in the array. If number is not FF H, the addition is carried out. If the number is FF H, the loop is terminated and the result is stored. The last number FF H is not added to the sum.
Algorithm:
  • Initialize pointer and sum as 0
  • Initialize memory pointer for first data·        
  • Load first data in accumulator
  • Check the number equal to FFH
  • If number is not equal to FF the add it lower byte sum and Increment pointer
  • If carry is generated then increment B to store carry
  • If carry is not generated repeat the process
  • If number is equal to FF the end the process
  • Store output and Carry
  • End the Program
Program:
Memory Address
Op-code
Operand
Comments
2000
LXI
H,2200 H
Initialize the memory pointer
2003
MVI
C,00
Lower-order byte of sum=0
2005
MOV
B,C
Higher-order byte of sum=0
2006
MOV
A,M
Load first data in Accumulator
2007
CPI
FF H
Compare the data with FF H.
2009
JZ
2016
If it is FF H, jump to 2016
200C
ADD
C
If it is not then add byte n no. from series
200D
MOV
C,A
Move result into register C
200E
JNC
2012
If no carry is produced jump to 2012
2011
INR
B
If carry then add to higher byte
2012
INX
H
Increment pointer
2013
JMP
2006
Jump to repeat the process
2016
MOV
A,C
Move LSB sum into A
2017
STA
2250 H
Store output
201A
MOV
A,B
Move the carry into accumulator
201B
STA
2251 H
Store the carry
201E
RST
5
Set Break-point
201F
END

End the program

Used Instruction:
LXI: This instruction is used to store the 16-bit data in the register pair designated in the operand.
MVI: This instruction is used to store 8 bit data in specified register.
MOV: This instruction is used to copy the content from source register to destination register.
CPI: This instruction is used to compare the given data with accumulator.
INX: This instruction is used to increment the content of register pair by 1.
INR: This instruction is used to increment the content of register by one.
ADD: This instruction is used to add the content of specified register to the content of accumulator and store output in accumulator.
RAR: Rotates the bits of accumulator right by one position, through the carry
JNC: Jump if No carry at specified memory address.
JZ: Jump if value of specified register is zero.
DCR: This instruction is used decrement the content of specified register by 1.
STA: This instruction is used to store the content of accumulator at specified memory address.
RST 5: This instruction is used to set break-point for the execution.
END: This instruction is used to execute the program.

Result: 
Input: 2200 H-13 H
            2201 H-E4 H
            2202 H-6B H
            2203 H-33 H
            2204 H-FF H
Output Result: 13+E4+6B+44=195 H
              2250-95 H (Lower Byte)       
              2251-01H (Higher Byte)         
Procedure to look output
  • After press ENTER, You will get first screen
  • Press G and Provide Initial address (as 2000)
  • Press SHIFT+4, you will get first screen again
  • Press M and Provide Input location for Input (M2200)
  • Press ENTER and provide your Input at location (2200:13H)
  • Press ENTER and provide your Input at location (2201:E4H)
  • Press ENTER and provide your Input at location (2202:6BH) and so on…
  • Press SHIFT+4
  • Press G and Provide Initial address (as 2000)
  • Press SHIFT+4, you will get first screen again
  • Press M and Provide address for output (M2250) and press ENTER
  • You will get your desired output.

No comments:

Post a Comment