Showing posts with label Assembly Language. Show all posts
Showing posts with label Assembly Language. Show all posts

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

Aim To write a program to convert an 8-bit binary number to equivalent gray code.

Algorithm
1. The MSB in the gray code is the same as the corresponding bit in a binary number.
2. Going from left to right, add each adjacent pair of binary digits to get the next gray code digit. Disregard carries.

Program

4100: 90 42 00              MOV DPTR,#4200H            ; Address of the number
4103: E0                         MOVX A,@DPTR                  ; Keep number in A
4104: C3                        CLR C                                     ; Clear carry for rotation
4105: 13                        RRC A                                    ; Rotate through carry
4106: F9                        MOV R1,A                              ; Rotated number in R1
4107: E0                        MOVX A,@DPTR                   ; Number again in A
4108: 69                        XRL A,R1             ; XOR rotated and original numbers
4109: A3                        INC DPTR
410A:F0                         MOVX @DPTR,A                   ; Store result in 4201
410B: 80 FE          HLT: SJMP HLT(410B)

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: 90 4201: D8

Aim To write a program to add two BCD numbers.

Description One 8-bit packed BCD number is available in the memory location 4300H and another number is available in the memory location 4301H. Result is available in the location 4302H and carry in 4303H.

Algorithm
1. Get the BCD numbers from the memory locations.
2. Add the BCD numbers with the help of DAA.
3. Store the sum and carry in the memory.

Program
4100: 90 43 00         MOV DPTR,#4300H          ;Address of first BCD number
4103: 79 00              MOV R1,#00                       ;Carry is reset
4105: E0                   MOVX A,@DPTR                  ; First number in A
4106: A3                   INC DPTR                            ; Pointing to second number
4107: F8                   MOV R0,A                            ; First number in R0
4108: E0                   MOVX A,@DPTR                  ; Second number in A
4109: 28                   ADD A,R0                             ; Add both numbers
410A: D4                   DA A                                     ; Convert to BCD
410B: 50 01             JNC NOC (410E)                 ; If no carry, skip
410D:09                    INC R1                                 ; Else increment carry
410E: A3         NOC: INC DPTR
410F: F0                   MOVX @DPTR,A                ; Result in destination address
4110: E9                   MOV A,R1                          ; Carry in A
4111: A3                   INC DPTR
4112: F0                   MOVX @DPTR,A                ; Store carry
4113: 80 FE    HLT: SJMP HLT (4113)              ; Halt

Procedure
1. Enter the program in memory locations starting from 4100H.
2. Enter data in memory locations 4300H and 4301H.
3. Execute the program and verify the sum in memory location 4302H and carry in 4303H.

Test data
Input: 4300: 75 4301: 65 Output: 4302: 40 4303: 01

Aim: To write a program to move a block of memory from one location to another. Five numbers stored in memory locations starting from 4500H must be moved to 4600H onwards.

Algorithm

1. Set byte counter.
2. Get the number from the source and copy it in destination.
3. Decrement counter and repeat the above step until the byte counter is reset.
Program

4100: 79 0A                MOV R1,05H ;                Length of block in R1
4102: 90 45 00           MOV DPTR,#4500H ;    First address in DPTR
4105: E0       NEXT:    MOVX A,@DPTR ;          First no. in Acc.
4106: 75 83 46           MOV DPH,46 ;                4600H in DPTR
4109: F0                      MOVX @DPTR,A ;          Number moved to 4600H
410A:75 83 45            MOV DPH,45H ;             4500H in DPTR
410D:A3                      INC DPTR ;                     Points next location
410E: D9 F5                DJNZ R1,NEXT (4105H) ; Continue till counter is reset
4110: 80 FE     HLT:   SJMP HLT (4110H) ; Halt

Procedure
1. Enter the program in memory locations starting from 4100H.
2. Enter five numbers in memory locations starting from 4500H.
3. Execute the program and verify the result in memory locations starting from 4600H.

Test data
Input: 4500: 01 02 03 04 05
Output: 4600: 01 02 03 04 05

STANDARD PROCEDURE:

1. Analyzing the Problem:
Addition of two 8-bit numbers stored at memory location 2200 & 2201. And store the result at 2202. Basic instructions that we are going to use are LDA, MOV, ADD, STA.

2. Representing Program Operations:


3. Implementing the Solution

 
4. Loading /Running and the Solution.
 Steps to Enter the program in Kit:
· Press RESET Key
· Press SET Key
· Enter Starting Program Address
· Press INC key
· Enter the Hex Code & Press INC Key

Steps to run the program:
· Press RESET Key
· Press RUN Key
· Enter Starting Program Address
· Press EXEC Key
Steps to check registers content after running the code:
· Press RESET Key
· Press REG Key
· Press the A Key to check accumulator
· Press INC Key or DEC key to check all other register’s content.

5. Testing the Solution
After running the program content of the memory addresses:
Observation Table:

 
Conclusions:
Using 8085 microprocessor trainer kit we can add two 8-bit numbers stored at memory location 2200 & 2201. And store the result at 2202.