January 26, 2012
Write a program to find Square of a given number
Apparatus Used: Microprocessor, Keyboards
Assumption: The Square of a number can be calculated by adding consecutive odd numbers starting from 1. In order to find the square of 3, add three odd numbers starting from 1. 1 + 3 + 5 = 9.
To find the Square of 4, 1 + 3 + 5 + 7 = 16.
Algorithm:
- Set the number who’s Square is to be calculated as the counter.
- Get first odd number.
- Clear the accumulator to store output.
- Add first odd number to accumulator.
- Get Second number and add to the accumulator and so on until counter is zero.
- Store the output.
Program:
Memory Add
|
Mnemonics
|
Op-code
|
Comments
|
2000
|
LXI
|
H,2050 H
|
Location to store the number
|
2003
|
MOV
|
C,M
|
Save that number in Register C
|
2004
|
MVI
|
A,00 H
|
Store 00 to accumulator
|
2006
|
MVI
|
B, 01H
|
Get the First Odd Number
|
2008
|
ADD
|
B
|
Add the number to accumulator
|
2009
|
DCR
|
C
|
Decrement Counter
|
200A
|
INR
|
B
| |
200B
|
INR
|
B
|
Get the next odd number
|
200C
|
JNZ
|
2008
|
Continue till counter =0
|
200F
|
STA
|
2052
|
Location to store output
|
2012
|
RST
|
5
|
Set the Break-point
|
2013
|
END
|
End the program
|
Description of 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.
ADD: This instruction is used to add the content of specified register to the content of accumulator and store output in accumulator.
INR: It will increment the content of specified register by one.
DCR: It will decrement the content of specified register by one.
JNZ: If result is not zero it will jump to the target address.
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: 2050-04
Output: 2052-10
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 (M2050)
- Press ENTER and Provide your Input at location (2050: 04)
- 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 (M2052) and press ENTER
- You will get your desired output.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment