Aim To find the square of a given number.

Theory 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 5, 1 + 3 + 5 + 7 + 9 = 25.

Algorithm
1. Set the number whose square is to be calculated as the counter.
2. Add odd numbers starting from zero until counter is zero.

Program

4100: 90 42 00             MOV DPTR,#4200H
4103: E0                        MOVX A,@DPTR                 ; Number in Acc.
4104: 60 0A                  JZ RESULT (4110)             ; If number is zero, store it
4106:                             FA MOV R2,A                       ; Set number as counter
4107: 79 01                  MOV R1,#01                        ; First odd number
4109: 74 00                  MOV A,#00
410B: 29           LOOP: ADD A,R1                             ; Add progressively
410C: 09                       INC R1                                  ; Next odd number
410D:09                        INC R1
411E: DA FB                 DJNZ R2, LOOP (410B)      ; Continue till counter = 0
4110: A3       RESULT: INC DPTR                             ; Store result
4111: F0                       MOVX @DPTR,A
4112: 80 FE        HLT: SJMP HLT (4112)

Procedure
1. Enter the program in memory starting from 4100H.
2. Enter the number in memory location 4200H.
3. Execute the program and verify the result in 4201H.

Test data
Input: Output
4200: 05 4201: 19 19H = 2510

0 comments